Linker error while building unit tests with Boost - c++

I'm using gcc 4.8.2 with Boost 1.59. I run on Kubuntu 12.04 LTS. I'm just trying to write a simple unit test application and I'm experiencing linker error.
Here is my code:
main.cpp
#define BOOST_TEST_MODULE My Module
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
test1.cpp
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE( suite1 );
BOOST_AUTO_TEST_CASE( case1 )
{
BOOST_TEST_MESSAGE( "Hello!" );
}
BOOST_AUTO_TEST_SUITE_END();
When I build I do: g++ -std=c++11 -o test main.cpp test1.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework. And I get this result:
/tmp/ccmNLUMx.o: In function `__static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0x15e): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::basic_cstring<char const>)'
test1.cpp:(.text+0x1b9): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/ccmNLUMx.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>)':
test1.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
Do you have any idea about what I'm doing wrong? Thanks!
Dominique
EDIT
What is given here could not solve my problem... :-(

I had the same problem. My solution is, change the include path:
#include <boost/test/unit_test.hpp> // wrong
#include <boost/test/included/unit_test.hpp> // this work
I had found the solution in the first example of boost unit test. I test it with boost 1.60 and gcc 4.8.5

Adding -D_GLIBCXX_USE_CXX11_ABI=0 to compile options resolved this for me

Related

boost: mingw: windows 10: undefined reference

How do I add boost logging to a c++ project?
I'm trying to add Boost to my home development environment for the first time. All I want to do right now is be able to use boost's logging implementation to log some errors I'm currently printing to stdout to a file. I started by getting the lambda sample code to build and now I'm trying to add the logging sample code but I'm getting this error:
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:597: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
My code is:
/* for logging */
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
namespace logging = boost::log;
void init(){
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
If I comment out 'logging::add_file_log("sample.log");' it builds just fine
Full build output:
22:15:12 **** Rebuild of configuration Debug for project foo ****
Info: Internal Builder is used for build
g++ -std=c++1y "-IC:\\boost_1_62_0" "-IC:\\boost_1_62_0\\stage\\lib" -O0 -g3 -Wall -c -fmessage-length=0 -DBOOST_LOG_USE_NATIVE_SYSLOG -lboost_filesystem -lboost_system -lboost_thread-mt -lboost_log_setup -lboost_log -o foo_main.o "..\\foo_main.cpp"
g++ -static-libgcc -static-libstdc++ -static "-LC:\\boost_1_62_0\\stage\\lib" -lboost_regex-mgw53-mt-d-1_62 -LC:/boost_1_62_0/stage/lib/ -lboost_regex-mgw53-mt-d-1_62 -o foo.exe foo_main.o -lboost_atomic-mgw53-mt-d-1_62 -lboost_chrono-mgw53-mt-d-1_62 -lboost_container-mgw53-mt-d-1_62 -lboost_context-mgw53-mt-d-1_62 -lboost_coroutine-mgw53-mt-d-1_62 -lboost_date_time-mgw53-mt-d-1_62 -lboost_exception-mgw53-mt-d-1_62 -lboost_filesystem-mgw53-mt-d-1_62 -lboost_graph-mgw53-mt-d-1_62 -lboost_iostreams-mgw53-mt-d-1_62 -lboost_locale-mgw53-mt-d-1_62 -lboost_log_setup-mgw53-mt-d-1_62 -lboost_log-mgw53-mt-d-1_62 -lboost_iostreams-mgw53-mt-d-1_62 -lboost_math_tr1f-mgw53-mt-d-1_62 -lboost_math_tr1l-mgw53-mt-d-1_62 -lboost_math_tr1-mgw53-mt-d-1_62 -lboost_prg_exec_monitor-mgw53-mt-d-1_62 -lboost_program_options-mgw53-mt-d-1_62 -lboost_system-mgw53-mt-d-1_62 -lboost_random-mgw53-mt-d-1_62 -lboost_serialization-mgw53-mt-d-1_62 -lboost_signals-mgw53-mt-d-1_62 -lboost_system-mgw53-mt-d-1_62 -lboost_test_exec_monitor-mgw53-mt-d-1_62 -lboost_thread-mgw53-mt-d-1_62 -lboost_timer-mgw53-mt-d-1_62 -lboost_type_erasure-mgw53-mt-d-1_62 -lboost_unit_test_framework-mgw53-mt-d-1_62 -lboost_wave-mgw53-mt-d-1_62 -lboost_wserialization-mgw53-mt-d-1_62
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `make_absolute':
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:597: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost3log10v2s_mt_nt55sinks17text_file_backend30set_file_name_pattern_internalERKNS_10filesystem4pathE':
C:\boost_1_62_0/libs/log/src/text_file_backend.cpp:1264: undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem6existsERKNS0_4pathE':
...enter code here
C:\boost_1_62_0/./boost/filesystem/operations.hpp:872: undefined reference to `boost::filesystem::detail::dir_itr_close(void*&)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem18directory_iteratorC1ERKNS0_4pathE':
C:\boost_1_62_0/./boost/filesystem/operations.hpp:903: undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem18directory_iterator9incrementEv':
C:\boost_1_62_0/./boost/filesystem/operations.hpp:939: undefined reference to `boost::filesystem::detail::directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*)'
C:\boost_1_62_0\stage\lib\libboost_log-mgw53-mt-d-1_62.a(text_file_backend.o): In function `ZN5boost10filesystem4pathaSINS0_15directory_entryEEENS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIT_E4typeEEERS1_E4typeERKS8_':
C:\boost_1_62_0/./boost/filesystem/path.hpp:202: undefined reference to `boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)'
collect2.exe: error: ld returned 1 exit status
22:15:18 Build Finished (took 5s.607ms)
I am doing this on a windows 10 machine running Eclipse Version: Neon.1a Release (4.6.1). I am using a mingw compiler, gcc (GCC) version 5.3.0.
I built my boost libraries using this command:
b2 toolset=gcc cxxflags=-std=c++1y -a
The absolute path to my boost_filesystem library is:
C:\boost_1_62_0\stage\lib\libboost_filesystem-mgw53-mt-d-1_62.a

Build library libtorrent debian and link it to program c++

I always have problems when building libraries and linking them, so I hope someone can give me a hand.
I downloaded libtorrent from here and I've built it like they explain here in the building with autotools section (skipping step 1). The building process was successfull I think, but when I did make check the output was:
============================================================================
Testsuite summary for libtorrent-rasterbar 1.0.5
============================================================================
# TOTAL: 0
# PASS: 0
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
Maybe it should say: total X?
I did a little program where I added #include <libtorrent/session.hpp> and when I compile with g++ file.cpp -o file it says libtorrent/session.hpp: No such file or directory.
Should I add some flags to g++ such as -lpthread for other projects and thinks like that?
Thanks
UPDATE:
When installing without building using sudo apt-get install libtorrent-rasterbar-dev and compiling my main.cpp file I get this error:
g++ main.cpp -o file
In file included from /usr/include/libtorrent/session.hpp:49:0,
from main.cpp:2:
/usr/include/libtorrent/config.hpp:46:2: error: #error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in order for asio's declarations to be correct. If you're linking dynamically against libtorrent, define BOOST_ASIO_DYN_LINK otherwise BOOST_ASIO_SEPARATE_COMPILATION. You can also use pkg-config or boost build, to automatically apply these defines
#error you must define either BOOST_ASIO_SEPARATE_COMPILATION or BOOST_ASIO_DYN_LINK in your project in \
UPDATE 2:
Modified the main.cpp file to add the the following above the libtorrent #include directives:
#ifndef BOOST_ASIO_DYN_LINK
#define BOOST_ASIO_DYN_LINK
#endif
But then I have this problem:
$ g++ main.cpp
/tmp/ccM2ItFb.o: In function `main':
main.cpp:(.text+0x57): undefined reference to `libtorrent::default_storage_constructor(libtorrent::file_storage const&, libtorrent::file_storage const*, std::string const&, libtorrent::file_pool&, std::vector<unsigned char, std::allocator<unsigned char> > const&)'
main.cpp:(.text+0xb9): undefined reference to `libtorrent::session::~session()'
main.cpp:(.text+0x105): undefined reference to `libtorrent::session::~session()'
/tmp/ccM2ItFb.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x162): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x16e): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x17a): undefined reference to `boost::system::system_category()'
main.cpp:(.text+0x192): undefined reference to `boost::asio::error::get_netdb_category()'
main.cpp:(.text+0x19e): undefined reference to `boost::asio::error::get_addrinfo_category()'
main.cpp:(.text+0x1aa): undefined reference to `boost::asio::error::get_misc_category()'
/tmp/ccM2ItFb.o: In function `boost::asio::error::get_system_category()':
main.cpp (.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccM2ItFb.o: In function `libtorrent::session::session(libtorrent::fingerprint const&, int, unsigned int)':
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x3c): undefined reference to `libtorrent::rel_clocktime_pools_nolog_resolvecountries_deprecated_dht_ext_()'
main.cpp:(.text._ZN10libtorrent7sessionC2ERKNS_11fingerprintEij[_ZN10libtorrent7sessionC5ERKNS_11fingerprintEij]+0x75): undefined reference to `libtorrent::session::init(std::pair<int, int>, char const*, libtorrent::fingerprint const&, int, unsigned int)'
/tmp/ccM2ItFb.o: In function `void boost::checked_delete<libtorrent::torrent_info const>(libtorrent::torrent_info const*)':
main.cpp:(.text._ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_[_ZN5boost14checked_deleteIKN10libtorrent12torrent_infoEEEvPT_]+0x1a): undefined reference to `libtorrent::torrent_info::~torrent_info()'
collect2: error: ld returned 1 exit status
Also tried compiling using: g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp with the same result.
UPDATE 3:
I was able to solve the problem. I had to compile using:
g++ -I /usr/include/ -L /usr/local/lib -lboost_system -lpthread -lboost_thread main.cpp and add also the link library -ltorrent-rasterbar
Sounds like you've compiled the library but not actually installed it anywhere. An #include directive looks in /usr/include by default, but the libtorrent headers are somewhere in the directory where you built the library. You'll need to either install the library's files into the system directories, or give the compiler an -I option pointing to the libtorrent build directory. (You'll probably also need an -L option when linking, for the same reason.)

Boost.test cannot find main

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?
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.
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!

HDF5 - C++ - open a file to read the contents failed

I tried writing a really short script just to open an hdf5 file but it does not work.
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
I'm pretty sure that I included the hdf5 library and the path to the includes.
But nevertheless I get the error message from the linker:
Invoking: GCC C++ Linker
g++ -L/usr/local/pub/lib64 -L/usr/local/pub/lib -L/lib64 -L/usr/lib64 -o "HDF5_CPP" ./openfile.o
./openfile.o: In function `main':
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5check_version'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileAccPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileCreatPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::H5File(std::string const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::~H5File()'
collect2: error: ld returned 1 exit status
make: *** [HDF5_CPP] Error 1
can anyone help?
Thank You!
For those using CMake, here is an example:
(the undefined reference problem is solved in the last line)
find_package(HDF5 COMPONENTS C CXX HL REQUIRED)
link_directories( ${HDF5_LIBRARY_DIRS} )
include_directories( ${HDF5_INCLUDE_DIRS} )
add_executable( convert_to_hdf5 src/convert_to_hdf5.cpp )
target_link_libraries( convert_to_hdf5 ${HDF5_CXX_LIBRARIES} )
I'd just like to leave a note to everyone else who reaches this place in the future, struggling with the same problem:
If you're choosing to use g++ with flags to compile your code instead of the hdf5-provided h5c++ script, make sure the flags you use are from h5c++ -show instead of h5cc -show, as the latter is for the straight C version.

Linking against boost_thread fails under Ubuntu 11.10

I already updated to the new Ubuntu 11.10.The problem is now, that the software I am working on is not linking any more. I found this wiki entry which seems to describe the problem. None the less I am unable to fix it. To reproduce the problem I wrote the following small test program:
#include <iostream>
#include <boost/thread.hpp>
void blubb() {
std::cout << "hello world" << std::endl;
}
int main() {
boost::thread t(&blubb);
t.join();
}
Than I use the following command to compile:
g++ -o test -lboost_thread test.cc
The output I get is the following:
/tmp/cc0O0dAC.o: In function `main':
test.cc:(.text+0x49): undefined reference to `boost::thread::join()'
test.cc:(.text+0x55): undefined reference to `boost::thread::~thread()'
test.cc:(.text+0x70): undefined reference to `boost::thread::~thread()'
/tmp/cc0O0dAC.o: In function `boost::detail::thread_data_base::thread_data_base()':
test.cc:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc0O0dAC.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (* ()> >, boost::thread::dummy*>::type)':
test.cc:(.text._ZN5boost6threadC2IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE]+0x30): undefined reference to `boost::thread::start_thread()'
/tmp/cc0O0dAC.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cc:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc0O0dAC.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status
I also tried to add -lpthread and -pthread in several orders with no success. This command works smoothly on an older Ubuntu version. What am I doing wrong (I have similar problems with PCRE and Google Protocol Buffers).
Thanks in advance for any help!
Ok I mamaged to fix it, it is ridiculous:
as written in the question
g++ -o test -lboost_thread test.cc
does not work, but
g++ -o test test.cc -lboost_thread
does... So not only the order of libraries matter, but also the source files must be written before the dependencies...