How do I link to cppunit?
Asked Answered
D

4

5

I think I'm missing something really stupid here.

I have libcppunit installed: (I'm using Ubuntu 12.04)

$ apt-cache policy libcppunit-dev 
libcppunit-dev:
  Installed: 1.12.1-4
  Candidate: 1.12.1-4
  Version table:
 *** 1.12.1-4 0
        500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
        100 /var/lib/dpkg/status

$ apt-cache policy libcppunit-1.12-1 
libcppunit-1.12-1:
  Installed: 1.12.1-4
  Candidate: 1.12.1-4
  Version table:
 *** 1.12.1-4 0
        500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
        100 /var/lib/dpkg/status

And I have a simple test:

#include <iostream>

#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>

int main() {
    CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

    CppUnit::TextUi::TestRunner runner;
    runner.addTest(suite);
    runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));

    return runner.run() ? 0 : 1;
}

And I this is the compiler output:

$ g++ -lcppunit -o test.bin test.cpp 
/tmp/ccoQDuGC.o: In function `main':
test.cpp:(.text+0x36): undefined reference to `CppUnit::TestFactoryRegistry::getRegistry(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0x75): undefined reference to `CppUnit::TextTestRunner::TextTestRunner(CppUnit::Outputter*)'
test.cpp:(.text+0x8b): undefined reference to `CppUnit::TestRunner::addTest(CppUnit::Test*)'
test.cpp:(.text+0x9a): undefined reference to `CppUnit::TextTestRunner::result() const'
test.cpp:(.text+0xe2): undefined reference to `CppUnit::CompilerOutputter::CompilerOutputter(CppUnit::TestResultCollector*, std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0xf4): undefined reference to `CppUnit::TextTestRunner::setOutputter(CppUnit::Outputter*)'
test.cpp:(.text+0x150): undefined reference to `CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool)'
test.cpp:(.text+0x189): undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()'
test.cpp:(.text+0x227): undefined reference to `CppUnit::TextTestRunner::~TextTestRunner()'
collect2: ld returned 1 exit status

To make sure, the libraries do exist under /usr/lib

$ ls /usr/lib/ | grep cppunit
libcppunit-1.12.so.1
libcppunit-1.12.so.1.0.0
libcppunit.a
libcppunit.la
libcppunit.so

What am I missing that is causing this?

Dogged answered 19/4, 2012 at 3:0 Comment(1)
A work around I've found is to place "-Wl,--no-as-needed" before "-lcppunit"Dogged
A
8

You have to tell the compiler which libraries to link to after you tell it which files to compile, i.e.

g++ test.cpp -lcppunit -o test.bin 
Angulate answered 24/4, 2012 at 12:14 Comment(1)
@MartinPfeffer it is generally necessary to place link options after compile options.Angulate
V
1

I ran into the same issue(with Ubuntu 11.04)

This appears to be a bug in Ubuntu. Your workaround "-Wl,--no-as-needed" works for me and is also mentioned as a workaround in the linked bug report. I haven't dived into it enough to discover the actual cause.

Vainglorious answered 28/4, 2012 at 5:33 Comment(1)
See wiki.ubuntu.com/NattyNarwhal/ToolchainTransition. Sadly this seems to be the wrong approach.Blast
C
1

The root cause I guess is the cppunit doc tutorial file "money_example.html" which proposes to add to the Makefile.am a line

MoneyApp_LDFLAGS = $(CPPUNIT_LIBS) -ldl

instead of the correct

MoneyApp_LDADD = $(CPPUNIT_LIBS) -ldl

or even more correct

MoneyApp_LDADD = $(CPPUNIT_LIBS)

since CPPUNIT_LIBS brings in the -ldl at any rate. LDFLAGS adds the flags right after the linker executable name, LDADD adds them in the end, eliminating the error in the original post.

Cisterna answered 19/6, 2012 at 10:42 Comment(0)
T
0

add link with cppunit and dl (-lcppunit -ldl) libraries

Thinnish answered 16/1, 2014 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.