I have seen a lot of similar posts, but tried every trick in the book and am still struggling. Everything was working fine, but after installing/removing wireshark with some components/disselectors it all got messed up. I don't remember exactly which libraries/packages got uninstalled, but probably a lot more than I noticed.
If I create a simple main.cpp file like this one:
#include <SQLAPI.h>
int main()
{
SAConnection con;
return 0;
}
and try
g++ main.cpp -lsqlapi -ldl
it gives me the following error messages:
/usr/local/lib/libsqlapi.so: undefined reference to `dlsym'
/usr/local/lib/libsqlapi.so: undefined reference to `dlerror'
/usr/local/lib/libsqlapi.so: undefined reference to `dlopen'
/usr/local/lib/libsqlapi.so: undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
I have tried to put -ldl before -lsqlapi as some have suggested that the order is important. If I use gcc instead of g++ the error is:
/usr/bin/ld: /tmp/ccwBI4tj.o: undefined reference to symbol '__gxx_personality_v0##CXXABI_1.3'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I am able to compile and run the file if SAConnection is removed.
I don't think it has anything to do with SQLAPI, because I experience similar problems with libboost. I don't have a small code example, but when I compile a project that was successfully compiled last week, I get the error:
/usr/bin/ld: debug/components/helloworld/HelloWorld.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
This project is using a Makefile that has been unchanged, so it has to be something on my system that is not correct. I have tried to reinstall build-essential.
Using Ubuntu 64 bit 13.10 with g++ version 4.8.1.
I have found the solution; setting the -Wl,--no-as-needed before -ldl. The new compile command is:
gcc main.cpp -lsqlapi -lstdc++ -Wl,--no-as-needed -ldl
Apparently it has something to do with recent versions of gcc/ld linking with --as-needed by default.
Related
I'm trying compile the simple code with C++ OpenCV but the both compilers g++ and clang++ accuse of undefined reference. I followed this steps for install OpenCV and run the last step (the tests) with success. My System: Ubuntu 18.04
The simple code to teste compilation:
#include <opencv2/core.hpp>
int main(){
cv::Mat image;
}
Output for g++ with command g++ teste.cpp -o teste -I /usr/local/include/opencv4 :
/tmp/ccqJeAzC.o: In function `cv::Mat::~Mat()':
teste.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccqJeAzC.o: In function `cv::Mat::release()':
teste.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
Output for clang++ with command clang++ teste.cpp -o teste -I /usr/local/include/opencv4 :
/tmp/teste-44c531.o: In function `cv::Mat::~Mat()':
teste.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD2Ev]+0x47): undefined reference to `cv::fastFree(void*)'
/tmp/teste-44c531.o: In function `cv::Mat::release()':
teste.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x48): undefined reference to `cv::Mat::deallocate()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
a test file named ml.cc, I have already installed the mathgl headers to /usr/local/include and libmgl.a to /usr/local/lib
#include <mgl2/mgl.h>
int main()
{
mglGraph gr;
gr.FPlot("sin(pi*x)");
gr.WriteFrame("test.png");
return 0;
}
"g++ -c ml.cc" can work,but "g++ ml.cc" does not work,the error is
/tmp/ccPzPcZt.o: In function `mglGraph::mglGraph(int, int, int)':
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x3b): undefined reference to `mgl_create_graph_gl'
ml.cc:(.text._ZN8mglGraphC2Eiii[_ZN8mglGraphC5Eiii]+0x54): undefined reference to `mgl_create_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::~mglGraph()':
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x28): undefined reference to `mgl_use_graph'
ml.cc:(.text._ZN8mglGraphD2Ev[_ZN8mglGraphD5Ev]+0x42): undefined reference to `mgl_delete_graph'
/tmp/ccPzPcZt.o: In function `mglGraph::SetFontSize(double)':
ml.cc:(.text._ZN8mglGraph11SetFontSizeEd[_ZN8mglGraph11SetFontSizeEd]+0x2a): undefined reference to `mgl_set_font_size'
/tmp/ccPzPcZt.o: In function `mglGraph::WriteFrame(char const*, char const*)':
ml.cc:(.text._ZN8mglGraph10WriteFrameEPKcS1_[_ZN8mglGraph10WriteFrameEPKcS1_]+0x2b): undefined reference to `mgl_write_frame'
/tmp/ccPzPcZt.o: In function `mglGraph::FPlot(char const*, char const*, char const*)':
ml.cc:(.text._ZN8mglGraph5FPlotEPKcS1_S1_[_ZN8mglGraph5FPlotEPKcS1_S1_]+0x30): undefined reference to `mgl_fplot'
collect2: error: ld returned 1 exit status
"g++ -L /usr/local/lib/ -l mgl ml.o" is the same error
I encoutered exactly the same problem and managed to solve it.
If you did the installation properly as you said. Then you simply need to add -lmgl AT THE END of the line! Like this:
g++ ml.o -lmgl
As luke already had mentioned, you have a linking error, thus compiling is not affected. Here is an explanation for this behaviour:
undefined reference to symbol even when nm indicates that this symbol is present in the shared library
Hope this solved your problem.
Peter
You have a linking error. To solve this you need to link against mgl. It looks like you are trying to, but its not working.
First, I would drop the space between the l and the mgl. so
g++ -L /usr/local/lib/ -lmgl ml.o
If that doesn't work, check to see if there are any other libraries you need to link against. See if g++ is throwing errors that it cannot find mgl.
Hope that helps.
I am trying to follow some sqlite C++ tutorial to be able to access a database from within C++ code on Linux. sqlite3 is installed and working, but when I try to compile the example code given on the link (Create a Table) I get the following error:
g++ build/test.o -o bin/test -pthread -L lib
build/test.o: In function `main':
/home/alexander/Projects/Test/src/test.cpp:22: undefined reference to `sqlite3_open'
/home/alexander/Projects/Test/src/test.cpp:24: undefined reference to `sqlite3_errmsg'
/home/alexander/Projects/Test/src/test.cpp:39: undefined reference to `sqlite3_exec'
/home/alexander/Projects/Test/src/test.cpp:42: undefined reference to `sqlite3_free'
/home/alexander/Projects/Test/src/test.cpp:46: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
Do I need to install something else? Or do I need to set some paths?
I have no practice in compiling straight from the shell.
In CodeBlocks it is necessary to point the paths of the “sqlite3.h” header and the “libsqlite3.so” dll.
If the dll is not specified in Linker Settings, exactly the mentioned problem occurs.
I am trying to use mongodb for the first time in c++. I just installed the latest version on Ubuntu and also installed the lastest v2.0 c++ driver code. It compiled just fine using scons. In the c++ file the below is my include.
#include <client/dbclient.h>
So..I am assuming I have to make a ref to a boost library but I don't know how to do it.
make all
Building target: rtb
Invoking: GCC C++ Linker
g++ -L/usr/local/include/ -L/home/boost -L/home/cpp/mongo-cxx-driver-v2.0/mongo -lfcgi++ -o"rtb" ./src/rtb.o
./src/rtb.o: In function `__static_initialization_and_destruction_0':
/home/boost/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/boost/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/boost/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [rtb] Error 1
Add -lboost_system to your link line.
I'm not sure to understand the undefined reference I am getting.
./cxxtest/cxxtestgen.py -o tests.cpp --error-printer DrawTestSuite.h
g++ -I./cxxtest/ -c tests.cpp
g++ -o tests tests.o Color.o
tests.o: In function `DrawTestSuite::testLinewidthOne()':
tests.cpp:(.text._ZN13DrawTestSuite16t… undefined reference to `Linewidth::Linewidth(double)'
tests.cpp:(.text._ZN13DrawTestSuite16t… undefined reference to `Linewidth::draw(std::basic_ostream<char… std::char_traits<char> >&)'
collect2: ld returned 1 exit status
make: *** [tests] Error 1// DrawTestSuite.h
DrawTestSuite.h contains the unit-test and The test function calls on Linewidth.h to execute the constructer and member function draw.
I have #include "Linewidth.h"in DrawTestSuite.h.
"Undefined reference" is a linker error when a function has been properly declared and used, but the definition has not been included when linking.
You need to link with Linewidth.o, which is the object file from compiling Linewdith.cpp and the likely location where those functions are implemented.
I'm not familiar with cxxtest to know how it expects you to specify that dependency, but I suspect it only requires a simple declaration.