skip to Main Content

I’m failing to link CppUnit (libcppunit.so) with GCC on Ubuntu/Linux. What am I doing wrong?

For reproducibility, I used Docker as follows:

docker run -it -w /opt/myproject --name cxx ubuntu:20.04 /bin/bash
$ apt-get update && apt-get install -y build-essential libcppunit-dev vim

I created a simple /opt/myproject/test.cpp:

#include <iostream>
#include <string>
#include <list>
#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <netinet/in.h>

using namespace std;
using namespace CppUnit;

int main(int argc, char* argv[]) {
    CPPUNIT_NS::TestResult testresult;

    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    ofstream xmlFileOut("test_cppunit.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    xmlOut.write();

    return collectedresults.wasSuccessful() ? 0 : 1;
}

I’m using g++ as follows:

$ ls /usr/lib/x86_64-linux-gnu/libcppunit.so 
/usr/lib/x86_64-linux-gnu/libcppunit.so
$ g++ -c -o test.o test.cpp
$ g++ -v -lcppunit -o test test.o
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-Av3uEd/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=/tmp/ccctVWox.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o test /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lcppunit test.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
/usr/bin/ld: test.o: in function `main':
test.cpp:(.text+0x2f): undefined reference to `CppUnit::TestResult::TestResult(CppUnit::SynchronizedObject::SynchronizationObject*)'
/usr/bin/ld: test.cpp:(.text+0x43): undefined reference to `CppUnit::TestResultCollector::TestResultCollector(CppUnit::SynchronizedObject::SynchronizationObject*)'
/usr/bin/ld: test.cpp:(.text+0x5c): undefined reference to `CppUnit::TestResult::addListener(CppUnit::TestListener*)'
/usr/bin/ld: test.cpp:(.text+0x6b): undefined reference to `CppUnit::BriefTestProgressListener::BriefTestProgressListener()'
/usr/bin/ld: test.cpp:(.text+0x84): undefined reference to `CppUnit::TestResult::addListener(CppUnit::TestListener*)'
/usr/bin/ld: test.cpp:(.text+0x93): undefined reference to `CppUnit::TestRunner::TestRunner()'
/usr/bin/ld: test.cpp:(.text+0xce): undefined reference to `CppUnit::TestFactoryRegistry::getRegistry(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: test.cpp:(.text+0xf2): undefined reference to `CppUnit::TestRunner::addTest(CppUnit::Test*)'
/usr/bin/ld: test.cpp:(.text+0x15c): undefined reference to `CppUnit::TestRunner::run(CppUnit::TestResult&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: test.cpp:(.text+0x1cd): undefined reference to `CppUnit::CompilerOutputter::CompilerOutputter(CppUnit::TestResultCollector*, std::ostream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: test.cpp:(.text+0x1fa): undefined reference to `CppUnit::CompilerOutputter::write()'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `CppUnit::XmlOutputter::XmlOutputter(CppUnit::TestResultCollector*, std::ostream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: test.cpp:(.text+0x292): undefined reference to `CppUnit::XmlOutputter::write()'
/usr/bin/ld: test.cpp:(.text+0x2a1): undefined reference to `CppUnit::TestSuccessListener::wasSuccessful() const'
/usr/bin/ld: test.cpp:(.text+0x2c0): undefined reference to `CppUnit::XmlOutputter::~XmlOutputter()'
/usr/bin/ld: test.cpp:(.text+0x2de): undefined reference to `CppUnit::CompilerOutputter::~CompilerOutputter()'
/usr/bin/ld: test.cpp:(.text+0x2ed): undefined reference to `CppUnit::TestRunner::~TestRunner()'
/usr/bin/ld: test.cpp:(.text+0x2fc): undefined reference to `CppUnit::BriefTestProgressListener::~BriefTestProgressListener()'
/usr/bin/ld: test.cpp:(.text+0x30b): undefined reference to `CppUnit::TestResultCollector::~TestResultCollector()'
/usr/bin/ld: test.cpp:(.text+0x31a): undefined reference to `CppUnit::TestResult::~TestResult()'
/usr/bin/ld: test.cpp:(.text+0x410): undefined reference to `CppUnit::XmlOutputter::~XmlOutputter()'
/usr/bin/ld: test.cpp:(.text+0x437): undefined reference to `CppUnit::CompilerOutputter::~CompilerOutputter()'
/usr/bin/ld: test.cpp:(.text+0x446): undefined reference to `CppUnit::TestRunner::~TestRunner()'
/usr/bin/ld: test.cpp:(.text+0x45e): undefined reference to `CppUnit::BriefTestProgressListener::~BriefTestProgressListener()'
/usr/bin/ld: test.cpp:(.text+0x476): undefined reference to `CppUnit::TestResultCollector::~TestResultCollector()'
/usr/bin/ld: test.cpp:(.text+0x48e): undefined reference to `CppUnit::TestResult::~TestResult()'
collect2: error: ld returned 1 exit status

Please note that the output of g++ shows that -L/usr/lib/x86_64-linux-gnu is implicitly included.

2

Answers


  1. Chosen as BEST ANSWER

    The order of options matters. -> See here: https://stackoverflow.com/a/12574400/343314

    This works:

    g++ -v -o test test.o -lcppunit 
    

    This doesn't:

    g++ -v -lcppunit -o test test.o
    

  2. If CppUnit is a library you are probably missing in your Makefile a line, like

    -<path to lcppunit/>lcppunit
    

    like -lpthread to use pthreads

    If not, you may add to add cppUnits objects individually in the -o line of Makefile

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search