Search code examples
c++macosgccshared-librariesunittest++

Configuring a library to be included with C++ test


I would like to utilize the UnitTest++ library in a testing file. However, I am having some difficulty getting the library to be included at compile time. So here is my current directory structure:

tests/
  UnitTests++/
    libUnitTest++.a
    src/
      UnitTests++.h
  unit/
    test.cpp

I have just used the UnitTest++ getting started guide to just get the library setup. Here is test.cpp:

// test.cpp
#include <UnitTest++.h>

TEST(FailSpectacularly)
{
 CHECK(false);
}

int main()
{
 return UnitTest::RunAllTests();
}

And I am currently trying to compile with:

gcc -lUnitTest++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp

I am currently getting a bunch output with ld: symbol(s) not found at the end. So how would I be able to get the UnitTest++ library properly included when this program is compiled? I am on a Mac and I'd also like for there to be an easy way for people on a Linux machine to run these same tests.

Whew, I hope this provides enough information, if not please let me know.


Solution

  • I was able to build it in the following manner

    gcc -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++ -lstdc++
    

    or

    g++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++
    

    that links to libstdc++ automatically.


    GCC documentation says:

    -llibrary

    -l library

    Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

    It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified.

    Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

    I guess that's why the library symbols are not found when you first specify -lUnitTest++ and then test.cpp