1

I am using Netbeans as IDE and have been trying to build a piece of code which uses boost library, but I am getting below error

g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/tcpproxy_server.o.d" -o build/Debug/GNU-Linux-x86/tcpproxy_server.o tcpproxy_server.cpp mkdir -p dist/Debug/GNU-Linux-x86 g++ -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function __static_initialization_and_destruction_0(int, int)': /usr/include/boost/system/error_code.hpp:221: undefined reference toboost::system::generic_category()' /usr/include/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()' /usr/include/boost/system/error_code.hpp:223: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::system::error_code::error_code()': /usr/include/boost/system/error_code.hpp:322: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::asio::error::get_system_category()': /usr/include/boost/asio/error.hpp:230: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::thread_exception::thread_exception(int, char const*)': /usr/include/boost/thread/exceptions.hpp:51: undefined reference toboost::system::system_category()' collect2: error: ld returned 1 exit status

So I did some online search for error and found out that I have to add "-lboost_system" in my compile command.

I added it in "project->properties->build->c++ compiler-> Additional Options" but still the same error.

g++ -lboost_system -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function __static_initialization_and_destruction_0(int, int)': /usr/include/boost/system/error_code.hpp:221: undefined reference toboost::system::generic_category()' /usr/include/boost/system/error_code.hpp:222: undefined reference to boost::system::generic_category()' /usr/include/boost/system/error_code.hpp:223: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::system::error_code::error_code()': /usr/include/boost/system/error_code.hpp:322: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::asio::error::get_system_category()': /usr/include/boost/asio/error.hpp:230: undefined reference toboost::system::system_category()' build/Debug/GNU-Linux-x86/tcpproxy_server.o: In function boost::thread_exception::thread_exception(int, char const*)': /usr/include/boost/thread/exceptions.hpp:51: undefined reference toboost::system::system_category()' collect2: error: ld returned 1 exit status

I found out that I have to add it in the end of compile line, for example :

"g++ tcp_proxy.cpp -o tcpproxy -lboost_system"

This I tried and its working, but netbeans is adding "Addition Options" at the start

like:

g++ -lboost_system -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o

Is there any way I can configure netbeans to add my option in end ?

Talan
  • 11
  • 4
  • You can refer to this thread http://stackoverflow.com/questions/9672072/netbeans-ide-for-c-how-to-specify-command-line-arguments – Abdalla Essam Ali Mar 05 '16 at 17:16
  • gcc command like is parsed from right to left. So if you want -l to be applied to whatever you compile you need to put it to the right of it, – user3528438 Mar 05 '16 at 17:29
  • @AbdallaEssamAli, buddy thanks for your reply, but I don't want to access command line "Arguments", I request you to please read the question thoroughly, I need compile/build command to be modified in such a way that "Addition options" I add much come at the end of final build command – Talan Mar 06 '16 at 06:55
  • @user3528438, thanks sir, but I already mentioned that I know about the gcc command parsing and via command on terminal I have it working .. but how can I do that in netbeans, please read above comment also – Talan Mar 06 '16 at 06:57

1 Answers1

0

So, I did some more search and found a question with somewhat my problem

link to the post :

C++ Boost on linux via Netbeans remote developement: undefined reference to boost::filesystem::path::codecvt()

This says that I can use following way to link a library:

Project Properties > Linker > Libraries > Add Library > Select the .a files.

This somewhat solves my issue, I am getting success with this try now:

g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/tcpproxy_server.o.d" -o build/Debug/GNU-Linux-x86/tcpproxy_server.o tcpproxy_server.cpp mkdir -p dist/Debug/GNU-Linux-x86

g++ -o dist/Debug/GNU-Linux-x86/tcp_proxy build/Debug/GNU-Linux-x86/tcpproxy_server.o -lboost_system

But I am still not sure If this is the right approach OR why adding it in addition compiler options not working.

Community
  • 1
  • 1
Talan
  • 11
  • 4
  • It's because the first of those commands, `g++ -c ...`, tells g++ to *compile* and not *link*. The second one, `g++ ...` without `-c`, tells g++ to *link*. `-l` (e.g. `-lboost_system`) is a *linker* option, not a *compiler* option. So putting it in *c++ compiler-> Additional Options* a) has no effect in compilation and b) does not tell the linker to link the library. Libraries are used only in linkage. – Mike Kinghan Mar 06 '16 at 09:19
  • Thanks @MikeKinghan, for the insight :) – Talan Mar 06 '16 at 09:24