开发者

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开发者_StackOverflow社区()
{
 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.


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


Compile test.cpp to get test.o

and use

g++ test.o libUnitTest++.a -o ./exectest

to get the ./exectest executable

libUnitTest++.a is just an archive of all the object files of UnitTest++. You just need to link all the object files (your test object file + libUnitTest++.a)

Try editing the makefile that came with unittest++ and make it suitable for your case


The message ld: symbol(s) not found means you haven't compiled the library. So you need to go in the UnitTest++ folder, compile and install it.

I've never worked on a MAC, but in linux, libraries are usually compiled and installed with:

./configure
make
make install

In the UnitTest++ link you posted, you should simply:

make install

Then you will have the UnitTest++.so library in the libraries folder of your OS. Now the library can be linked with your program with the -lUnitTest++ command.


Usually you have to put -Lsomething before the -lsomething that requires it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜