Boost.test cannot find main

喜夏-厌秋 提交于 2019-12-14 01:58:39

问题


I'm working with gcc 4.8, boost 1.59 on kubuntu 12.04.

I wrote a simple main.cpp file:

#define BOOST_TEST_MODULE My_Module
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( foo )
{}

This doesn't work when I build with

g++ -std=c++11 main.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework -o test

I get a bunch of linker errors:

/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to 'main'
/tmp/cc57ppN0.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x131): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/cc57ppN0.o: In function `boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>)':
main.cpp:(.text._ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE[_ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE]+0x6d): undefined reference to `boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&)'
collect2: erreur: ld a retourné 1 code d'état d'exécution

What does undefined reference to 'main' means??? Well, I know that it is because it could not find main() but why? AFAIK the syntax of my file is correct. It should link, no?


回答1:


You need to insert the following directive at the top of main.cpp:

#define BOOST_TEST_DYN_LINK

It seems that the example in the Boost.test documentation works for static linking only: the directive above is required, however, for dynamic linking.

See e.g C++ Unit Testing With Boost.Test for further details.




回答2:


OK, I found the solution!

It seems that, since 1.34.1, boost.test no longer contains main() in dynamic (.so) version. See here. So I wanted to link with the static and I also learned that gcc prefers the dynamic libraries over the static ones for the same name!. Thus, I changed my compile command to:

g++ -std=c++11 main.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework -static -o test

...and it worked fine!

I also tested with two files ... main.cpp test1.cpp and the run executed all the test cases correctly.

Thank you, I hope this can help someone else!



来源:https://stackoverflow.com/questions/33644914/boost-test-cannot-find-main

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!