Boost thread error: undefined reference - c++

#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout <<
"Hello world, I'm a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}
I ran tried to compile this program, and got these errors:
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`typeinfo for boost::thread_resource_error'
./src/thread.o: In function `condition_variable':
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33:
undefined reference to `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33:
undefined reference to `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: \
undefined reference to `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:54:
undefined reference to `vtable for boost::detail::thread_data_base'
./src/thread.o: In function `thread<void (*)()>':
/usr/include/boost/thread/detail/thread.hpp:188:
undefined reference to `boost::thread::start_thread()'
./src/thread.o: In function `~thread_data':
/usr/include/boost/thread/detail/thread.hpp:40:
undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to
`boost::detail::thread_data_base::~thread_data_base()'
Can any one tell me why i am getting this error?

compile with mt tag i.e -lboost_thread-mt

I had the same question, but -lboost_thread-mt is now deprecated see this answer on askubuntu.com. Instead what you now want in your makefile (at least for linux) is:
-lpthread -lboost_thread ...
Boost has simply given you the responsibility to link to your system's thread library.

Many boost libraries are fully implemented in header files. Boost.thread is not. It seems that it is not linking in the boost thread library. Check your linker search paths. Or, as the Stargazer712's comment on the OP says, check the installation. You should see something like libboost_thread-gcc-xxx-1_nn.o in your lib directory. If so, try referencing it explicitly in your link step (something like -L<path_to_lib> -lboost-thread-gcc-xx-1_nn). If not, then you apparently don't have a complete installation.

Instead of
g++ -pthread -lboost_thread X.cpp
Try
g++ X.cpp -pthread -lboost_thread

I had a similar problem with centos 6.5 when compiling povray 3.7 and this solved it - just add -lboost_thread-mt in your Makefile.

add compile option
-L<path_to_lib> -lboost-thread-gcc-xx-1_nn
gregg's answer is right!

I had the same error. I fixed it compiling with -lboost_thread

Related

Undefined reference to nlopt_... symbols

I am fairly new to Fortran and this may sound like a silly question. I encounter an error when compiling the Fortran code that is posted as an example in the NLOPT Wiki.
I compile in Ubuntu 18.04 LTS using the following command:
gfortran example-nlopt.f90 -o example-nlopt -I/usr/local/include/
The following error is produced in the terminal:
/tmp/ccbAim6b.o: In function `MAIN__':
example-nlopt.f90:(.text+0x26): undefined reference to `nlo_create_'
example-nlopt.f90:(.text+0x42): undefined reference to `nlo_get_lower_bounds_'
example-nlopt.f90:(.text+0x67): undefined reference to `nlo_set_lower_bounds_'
example-nlopt.f90:(.text+0x8a): undefined reference to `nlo_set_min_objective_'
example-nlopt.f90:(.text+0xca): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x10e): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x12d): undefined reference to `nlo_set_xtol_rel_'
example-nlopt.f90:(.text+0x164): undefined reference to `nlo_optimize_'
example-nlopt.f90:(.text+0x305): undefined reference to `nlo_destroy_'
collect2: error: ld returned 1 exit status
Based on what I saw in nlopt's documentation (https://nlopt.readthedocs.io/en/latest/NLopt_Installation/#changing-the-installation-directory) it looks like you just need to specify the library to link against. Maybe try this:
gfortran -I/usr/local/include/ -L/usr/local/lib example-nlopt.f90 -o example-nlopt -lnlopt -lm
This assumes you have the libnlopt.so in /usr/local/lib, if not then point to its location with the -L option.

Linking a DLL using xerces gives undefined symbols

I'm creating a shared library/DLL using cygwin which makes use of Xerces. When I call the xercesc functions from the main application everything is fine, but when I try to put some code into the library, then I get undefined symbols for all the static stuff that xerxesc defines.
For example:
std::string fromXMLString(XMLCh *oXMLString)
{
std::string result;
xercesc::DOMImplementation *impl = xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
char *temp = xercesc::XMLString::transcode(oXMLString);
result = temp;
xercesc::XMLString::release(&temp);
return result;
}
Linking:
g++ -shared -Wl,-soname,cygsupport.so -L /usr/local/lib -l xerces-c -o cygsupport.so obj/helper/xml_helper.o
When linking the library, I get:
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::DOMImplementationRegistry::getDOMImplementation(wchar_t const*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::transcode(wchar_t const*, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::release(char**, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLString::release(wchar_t**, xercesc_3_1::MemoryManager*)'
/usr/local/include/xercesc/internal/XSerializable.hpp:37: undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
...
Finally after several days on looking into this issue I found the solution. It is as simple as stupid.
My original linker commandline looked like this:
g++ -shared -o mylib.so -L/usr/local/lib -lxerces-c objects...
Googling on this problem didn't yield anything usefull, so finally I decided to create a fresh new sample project with eclipse and suddenly it worked. The only difference was in the commandline to the linker. When I applied the same order to my main project it suddenly compiled.
g++ -L/usr/local/lib -shared -o mylib.so objects... -lxerces-c
Note that in the above line, the objects come before the library, and apperently this makes the difference. I thought that the ordering of the libs only applies to the libraries, but apparently also the objects must be ordered appropriately.

ANTLR grammar linking error

I'm trying to compile a simple grammar which I created with ANTLR but I keep getting linking errors. In the end I want to include the ANTLR grammar in a C++ project, but for now I would be happy if compiling and linking in C works. I first tried:
g++ -oX -I/usr/local/include -I../src -L/usr/local/lib -lantlr3c \
../src/RtfLexer.cpp \
../src/RtfParser.cpp
Then I tried:
gcc -oX -lantlr3c -I../src ../src/RtfLexer.cpp ../src/RtfParser.cpp
but I keep getting:
/tmp/ccJgJxMH.o: In function `RtfLexerNewSSD':
RtfLexer.cpp:(.text+0xb0): undefined reference to `antlr3LexerNewStream'
/tmp/ccVZ2Vco.o: In function `RtfParserNewSSD':
RtfParser.cpp:(.text+0x7d): undefined reference to `antlr3ParserNewStream'
RtfParser.cpp:(.text+0xfa): undefined reference to `ANTLR3_TREE_ADAPTORNew'
RtfParser.cpp:(.text+0x10e): undefined reference to `antlr3VectorFactoryNew'
/tmp/ccVZ2Vco.o: In function `plaintext(RtfParser_Ctx_struct*)':
RtfParser.cpp:(.text+0x8c4): undefined reference to `antlr3RewriteRuleTOKENStreamNewAE'
RtfParser.cpp:(.text+0x992): undefined reference to `antlr3RewriteRuleTOKENStreamNewAE'
RtfParser.cpp:(.text+0xa60): undefined reference to `antlr3RewriteRuleTOKENStreamNewAE'
RtfParser.cpp:(.text+0xb2e): undefined reference to `antlr3RewriteRuleTOKENStreamNewAE'
RtfParser.cpp:(.text+0xbfc): undefined reference to `antlr3RewriteRuleTOKENStreamNewAE'
[more to follow]
So it seems that the library can not be found, but g++ and gcc don't complain. I re-installed the library, re-generated the grammar, no luck so far. Am I missing something obvious here?
Thanks in advance!
Put the libraries at the end of the compiler command:
g++ -oX -I../src ../src/RtfLexer.cpp ../src/RtfParser.cpp -lantlr3c

Compiling C++ source file using Boost.Thread

I am trying to learn how to use the C++ Boost.Thread library. I have installed the Boost libraries on my Ubuntu 11.10 system. I am following the book "The Boost C++ Libraries" by Schaling - specifically example 6.1 on page 66. I am trying to compile the following code example:
#include <boost/thread.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void thread()
{
for(int i = 0; i < 5; ++i)
{
wait(1);
std::cout << i << std::endl;
}
}
int main()
{
boost::thread t(thread);
t.join();
}
However, when I compile this with the following from the command line:
$ g++ example61.cpp -o example61 -I /usr/local/include
I get the following output:
/tmp/cc6bVu1F.o: In function `main':
example6.cpp:(.text+0x9d): undefined reference to `boost::thread::join()'
example6.cpp:(.text+0xae): undefined reference to `boost::thread::~thread()'
example6.cpp:(.text+0xc6): undefined reference to `boost::thread::~thread()'
/tmp/cc6bVu1F.o: In function `boost::detail::thread_data_base::thread_data_base()':
example6.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc6bVu1F.o: In function `void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)':
example6.cpp:(.text._ZN5boost11this_thread5sleepINS_10posix_time7secondsEEEvRKT_[void boost::this_thread::sleep<boost::posix_time::seconds>(boost::posix_time::seconds const&)]+0x35): undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/tmp/cc6bVu1F.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)':
example6.cpp:(.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/cc6bVu1F.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
example6.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc6bVu1F.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 don't know how to interpret this. Can anyone help? Thank you so much!
That is a linking error. It means your code is correct and you include the correct headers, but the compiler doesn't link against the boost threading library. To fix this, you need to compile like this:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread
If you've installed the Boost threading library to a non-standard path, you must also add it to the search path:
g++ example61.cpp -o example61 -I /usr/local/include -lboost_thread -L/usr/local/lib
You need to link with the library. Some Boost libraries are implemented entirely in the header files and do not need a library. But others, like thread, are implemented partly in headers and partly in compiled library code.
I believe that you need to add -lboost_thread-mt to your compile command.
Boost thread are not a template only library. You need to add a -lboost_thread while linking /compiling.
Most of the libraries in boost are implemented in headers. They can simply be included like you have done. Boost thread on the other hand, is of such a nature that you need to depend on its compiled units, only the declaration of its function are readily available to you in the header. So the compiler, or more correctly the linker, which is responsible for linking your calls to the declared functions /classes need to know where to look for these symbols. By invoking the compiler with a -lboost_thread you tell it to link to the library (-l) boost thread.
Following your comments I share with you compilation string for pocketcpp compilation tool:
g++ -static -I"\boost" "$(FULL_CURRENT_PATH)" -L"\MinGW\lib" -lboost_thread -lboost_system -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
Good luck,
I commented above, but wanted to share the full command line here.
g++ -std=c++11 thread_example.cpp -lboost_thread -lboost_system
[I'm using thread_example.cpp as the source filename; please replace with your own]

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