i have already installed boost libraries with bjam install, but when i'm compiling programme:
#include boost/asio.hpp
int main()
{
return 0;
}
such errors occur:
/tmp/ccVR3eeF.o: In function `__static_initialization_and_destruction_0(int, int)':
sda.cpp:(.text+0x52): undefined reference to `boost::system::generic_category()'
sda.cpp:(.text+0x5e): undefined reference to `boost::system::generic_category()'
sda.cpp:(.text+0x6a): undefined reference to `boost::system::system_category()'
/tmp/ccVR3eeF.o: In function `boost::asio::error::get_system_category()':
sda.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr_create(unsigned int&)':
sda.cpp:(.text._ZN5boost4asio6detail20posix_tss_ptr_createERj[boost::asio::detail::posix_tss_ptr_create(unsigned int&)]+0x19): undefined reference to `pthread_key_create'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()':
sda.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccVR3eeF.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()':
sda.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED2Ev[boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()]+0x15): undefined reference to `pthread_key_delete'
collect2: ld returned 1 exit status
what do i have to do? how can i build those libraries?
Boost.Asio requires the Boost.System library as well; you need to add -lboost_system to your linker command line.
As you add features to your program, you might also need other parts of Boost, like Boost.Thread, Boost.Date_Time, and so on. See http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/using.html for details.
I use netbeans so i typed '-lboost_system' in additional lines ! That was a mistake.
Additional lines are put before objects, and because of that i had the same error. Then i found out if i run from console and put -lboost_system at the VERY END everything works greate. At the end i found the right place to set in netbeans (in library not command section) and that field adds it at the end!
Remember, not only counts if you have the library in command ! THE POSITION does count :) Remember to put library at end and verify it :)
Work great:
g++.exe -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -c -g -MMD -MP -MF async_client.o.d -o async_client.o async_client.cpp
g++.exe -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -o async_client async_client.o -lws2_32 -lboost_chrono -lboost_system -lboost_thread
Doesn't work:
g++.exe -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -c -g -MMD -MP -MF async_client.o.d -o async_client.o async_client.cpp
g++.exe -D_WIN32_WINNT=0x0501 -D__USE_W32_SOCKETS -lws2_32 -lboost_chrono -lboost_system -lboost_thread -o async_client async_client.o
Related
I am new to C++ and am trying to figure out where in the compiling process my error is. Apologies, if this question is unclear, I'm not sure what information to provide.
Reference information: The directory "mbedtls/lib/" contains two .a (archive) files, "libmbedtls_SGX_t.a" and "libmbedtls_SGX_u.a".
I am using a Makefile, which runs the following commands in order. The commands seem to run error free:
cc -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -c App/Enclave_u.c -o App/Enclave_u.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/App.cpp -o App/App.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/sgx_utils/sgx_utils.cpp -o App/sgx_utils/sgx_utils.o
After running those commands, it runs this command:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread -Lmbedtls/lib/ -lsgx_uae_service_sim
However, this command produces an error:
App/Enclave_u.o: In function `Enclave_ocall_print_string':
Enclave_u.c:(.text+0x9): undefined reference to `ocall_print_string'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_free':
Enclave_u.c:(.text+0x28): undefined reference to `ocall_mbedtls_net_free'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv_timeout':
Enclave_u.c:(.text+0x54): undefined reference to `ocall_mbedtls_net_recv_timeout'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_send':
Enclave_u.c:(.text+0x71): undefined reference to `ocall_mbedtls_net_send'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv':
Enclave_u.c:(.text+0x91): undefined reference to `ocall_mbedtls_net_recv'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_usleep':
Enclave_u.c:(.text+0xa8): undefined reference to `ocall_mbedtls_net_usleep'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_nonblock':
Enclave_u.c:(.text+0xc9): undefined reference to `ocall_mbedtls_net_set_nonblock'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_block':
Enclave_u.c:(.text+0xe9): undefined reference to `ocall_mbedtls_net_set_block'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_accept':
Enclave_u.c:(.text+0x119): undefined reference to `ocall_mbedtls_net_accept'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_bind':
Enclave_u.c:(.text+0x144): undefined reference to `ocall_mbedtls_net_bind'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_connect':
Enclave_u.c:(.text+0x164): undefined reference to `ocall_mbedtls_net_connect'
collect2: error: ld returned 1 exit status
Makefile:184: recipe for target 'app' failed
I don't know why this error is being produced. My last command includes the flag -Lmbedtls/lib/ which should resolve these undefined references by linking the archive files (specifically - libmbedtls_SGX_u.a) to the executable. However, clearly this is not happening.
Do I need to link the archive files earlier in the compile process? I.e - instead of using the flag -Lmbedtls/lib/ in the final g++ command, should I be using it in both of the previous g++ commands?
Update: Adding the flag -Lmbedtls/lib/ to the previous 2 g++ commands (the ones that generated App.o and sgx_utils.o) did not change anything. The error remains.
As has been mentioned in the comment, only specifying a directory with -L without specifying the name of a library in that directory is useless.
So you have to additionally specify the library names with -l.
So instead of:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ \
-lsgx_uae_service_sim
Use:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ -lmbedtls_SGX_t -lmbedtls_SGX_u \
-lsgx_uae_service_sim
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 ?
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.
When I build a simple C++ program with glog library (Google's logging library which is not used in the code), I get "undefined reference" errors. When I remove the -lglog from the build command, the link succeeds.
Notice that the library that I added to the link is not used in the code at all and despite that it caused the build to fail. In addition, the glog and log4cpp libraries are supposed to be independent.
Can you explain this unusual behavior?
Environment: Ubuntu 14.04
Code:
//test.cpp
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
int main() {
log4cpp::Appender *appender;
appender = new log4cpp::FileAppender("default", "program.log");
return 0;
}
Working build command:
$ g++ test.cpp -llog4cpp -lpthread
Failing build command:
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
EDIT:
This command also builds successfully:
g++ test.cpp -llog4cpp -lpthread -lglog
This command fails (change the order of libs):
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
This succeeds:
$ g++ test.cpp -pthread -llog4cpp
This fails:
$ g++ test.cpp -pthread -llog4cpp -lglog
EDIT 2:
I studied the duplicate suggestions (1) and (2) to find out maybe there's something useful to me there, but it turned out irrelevant because these cases doesn't address the situation where a library that is not used in the code is added to the link and make it fail.
EDIT 3:
The files from my environment (glog libs, log4cpp libs, used log4cpp headers and test.cpp): log_test.zip.
I have been scratching my head since yesterday trying to make gtest work but I just can't fix it after reading the links below.
undefined reference to `pthread_key_create' (linker error)
error during making GTest
The compilation error displayed is this:
g++ main.o tests.o var.o -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs
-lgtest -lgmock -lpthread -o test
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
My Makefile is:
CXXFLAGS=-I ../gmock/include -I ../gmock/gtest/include
test:main.o tests.o var.o
g++ $^ -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs -lgtest -lgmock -lpthread -o $#
I am still in the process of learning Linux, compiling and linking source files.
The Error is about linking error on pthread.
You have to make the pthread flag to -pthread and the follow CXX should do the trick.
LDLIBS = -L../gmock/lib/.libs -L../gmock/gtest/lib/.libs -lgtest -lgmock
test:main.o tests.o var.o
g++ -isystem $(LDLIBS) -pthread $^ -o $#
This link have a good Makefile which addresses all your problems and it follows general standards on creating a Makefile
I am compiling a program with the following flags and getting errors (running 64 bit os):
g++ -lm -lml -lcvaux -lhighgui -lcv -lcxcore main.o BRIEF.o -o BRIEF_demo
I get a bunch of undefined references:
main.cpp:(.text+0x1f6): undefined reference to `cvInitMatHeader'
main.cpp:(.text+0x218): undefined reference to cvInitMatHeader'
main.o: In function_Z14drawQuadrangleP9_IplImageiiiiiiii8CvScalari.constprop.77':
main.cpp:(.text+0x2d5): undefined reference to cvLine'
main.cpp:(.text+0x333): undefined reference tocvLine'
main.cpp:(.text+0x398): undefined reference to cvLine'
main.cpp:(.text+0x3f2): undefined reference tocvLine'
Anyone have an idea how to circumvent this?
I suppose you are using the newest OpenCV 2.3.x. cvInitMatHeader() and cvLine() are actually defined in libcxcore.so, which I can see you are including.
My guess is that the order of the linking is wrong, so you need to adjust your command to something like:
g++ main.cpp BRIEF.cpp -o BRIEF_demo -lm -lml -lcvaux -lhighgui -lcv -lcxcore