I'm trying the following command:
gcc -o test test.o -lblas
which gives the error:
/usr/bin/ld: test.o: undefined reference to symbol 'sqrtf##GLIBC_2.2.5'
/usr/bin/ld: note: 'sqrtf##GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try adding it to the linker command line
/lib64/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
However, when I add -lm at the end of the command, there is a whole bunch of error messages:
test.o: In function `main':
test.cpp:(.text+0xe9): undefined reference to `std::cout'
test.cpp:(.text+0xee): undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&, char const*)'
[...]
collect2: error: ld returned 1 exit status
I have the same problem with you when I tried to link other libraries.
Here is my solutions:
install libtool;
add the library path to LD_LIBRARY_PATH;
I use root to make.
I don't know the exactly reason why it works on my Ubuntu. But I think you can try these steps.
Related
There is a error when compiling opencv program program simple includes #include <opencv2/core.hpp>
I compiled it with this command
g++ test.cpp -o app `pkg-config --cflags --libs opencv`
compiling opencv in c++
This is full error
gcc -I/usr/local/lib test.cpp
test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
1 | #include <opencv2/core.hpp>
I compiled and install the opencv by looking at this page https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line
The code is from https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html
Also my opencv so files are located at /usr/local/lib
Error also says
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
but I searched /usr directory there is no opencv.pc file
Update
Compiling program after updating my compile command This error throws
$g++ -I/usr/local/include/opencv4/ test.cpp
/usr/bin/ld: /tmp/ccuJvWgF.o: in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
$
Update 2
when now compiling
g++ -L/usr/local/lib/libopencv_core.so -I/usr/local/include/opencv4/ test.cpp
it throws this error. There are many opencv so files in /usr/local/lib do I need to include specific opencv so files to compile the code in the link
in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit sta
With the -L option you should specify the library search path.
Then with -l options you specify the library names you'd like to link against.
So in your case I'd expect to see -L/usr/local/lib -lopencv_core. Note that the -l name has the lib prefix and file extension omitted. (You may need more OpenCV libraries.)
Seeing your struggles, I think it would be good to read a general tutorial about compiling and linking C/C++ programs (on your platform).
I had a similar problem when I moved some code from OpenCV2 to OpenCV4, it appears you are using some OpenCV4 as well. My fix was to not include opencv2/ but to include opencv4/opencv2/. Not exactly sure what about that made it work, I did that way too long ago, but it worked since.
I have written a C++ file in nano text editor (UNIX command line) and I am trying to execute it with this command: c++ main.cpp. When I run it, no error messages display, however, there's also no successful messages; simply nothing appears... I also tried clang which gave me some more details, click here to view the error message.
My code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}
Clang Output:
/usr/bin/ld: /tmp/main-ad7332.o: in function `main':
main.cpp:(.text+0x11): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: main.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: main.cpp:(.text+0x36): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/main-ad7332.o: in function `__cxx_global_var_init':
main.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
No output whatsoever is the expected outcome. Running c++ only compiles the provided source file. To run your program, you will have to explicitly run the produced executable, which by default is a.out.
With regards to the error messages you received from clang, make sure that you are using clang++ and remembered to include the iostream header, which declares std::cout.
I'm trying to run some example code from this repository
When going to the example folder (/binacpp/example) and run i.e.
./example.run.sh
It works.
However when I update the file (even with just an extra space in a comment) and try to recompile the program by typing:
make example
Then I get this error:
Making example
g++ -I../lib/libcurl-7.56.0/include -I../lib/jsoncpp-1.8.3/include -I../lib/libwebsockets-2.4.0/include -I../lib/libbinacpp/include \
example.cpp \
-L../lib/libcurl-7.56.0/lib \
-L../lib/libwebsockets-2.4.0/lib \
-L../lib/libbinacpp/lib \
-lcurl -lcrypto -lwebsockets -lbinacpp -o example
/tmp/ccCycCcN.o: In function `ws_depth_onData(Json::Value&)':
example.cpp:(.text+0xd85): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0xdf0): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0xf79): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0xfe4): undefined reference to `Json::Value::asString[abi:cxx11]() const'
/tmp/ccCycCcN.o: In function `ws_klines_onData(Json::Value&)':
example.cpp:(.text+0x1255): undefined reference to `Json::Value::asString[abi:cxx11]() const'
/tmp/ccCycCcN.o:example.cpp:(.text+0x1317): more undefined references to `Json::Value::asString[abi:cxx11]() const' follow
/tmp/ccCycCcN.o: In function `main':
example.cpp:(.text+0x22c4): undefined reference to `BinaCPP::init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
example.cpp:(.text+0x242d): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0x24aa): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0x25e5): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0x2659): undefined reference to `Json::Value::asString[abi:cxx11]() const'
example.cpp:(.text+0x27d2): undefined reference to `Json::Value::asString[abi:cxx11]() const'
/tmp/ccCycCcN.o:example.cpp:(.text+0x28a2): more undefined references to `Json::Value::asString[abi:cxx11]() const' follow
collect2: error: ld returned 1 exit status
Makefile:25: recipe for target 'example' failed
make: *** [example] Error 1
I looked this error up and I'm guessing this is a linker error like described in this post. However I'm coming from Python and new to C++ and just don't see what is wrong. I see that the .h files are included and in
binacpp_websocket.h
I see
typedef int (*CB)(Json::Value &json_value );
Why am I getting the error?
NOTE: When trying to run the command "make example" I got the error:
Making example
g++ -I../lib/libcurl-7.56.0/include -I../lib/jsoncpp-1.8.3/include -I../lib/libwebsockets-2.4.0/include -I../lib/libbinacpp/include \
example.cpp \
-L../lib/libcurl-7.56.0/lib \
-L../lib/libwebsockets-2.4.0/lib \
-L../lib/libbinacpp/lib \
-lcurl -ljsoncpp -lcrypto -lwebsockets -lbinacpp -o example
/usr/bin/ld: cannot find -ljsoncpp
collect2: error: ld returned 1 exit status
Makefile:25: recipe for target 'example' failed
make: *** [example] Error 1
Somewhere in the repo (I cannot find it now) it said that "-ljsoncpp" shouldn't be there. After removing this the compiler did run but it gave the linker error.
I would like to compile the following program that makes alignment of fasta sequences, the program is available in:
http://compbio.cs.princeton.edu/mems/
I have downloaded a zip file called sparseMEM and downloaded the g++ in Ubuntu Linux, in the directory created there is a Makefile archive, so I tried to compile with make. The problem is that I got the following error:
g++ -lpthread -O3 -DSIXTYFOURBITS mummer.o qsufsort.o sparseSA.o fasta.o -o mummer
mummer.o: In function `main':
mummer.cpp:(.text.startup+0x480): undefined reference to `pthread_create'
mummer.cpp:(.text.startup+0x4a8): undefined reference to `pthread_join'
sparseSA.o: In function `sparseSA::MEM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<match_t, std::allocator<match_t> >&, int, bool, int)':
sparseSA.cpp:(.text+0x3bbc): undefined reference to `pthread_create'
sparseSA.cpp:(.text+0x3be0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [mummer] Error 1
how I can make it run?
also if somebody knows how to test this program it would be great
thanks
For Linux you need -pthread instead of -lpthread.
I'm using Ubuntu and tried using synaptic to install everything that had the word "GLUT" in it and also SDL and opengl . But still a simple program fails to compile . It shows this :
opengl1.cpp:(.text+0xe): undefined reference to `glClear'
opengl1.cpp:(.text+0x1a): undefined reference to `glBegin'
opengl1.cpp:(.text+0x2e): undefined reference to `glVertex2i'
opengl1.cpp:(.text+0x33): undefined reference to `glEnd'
opengl1.cpp:(.text+0x38): undefined reference to `glFlush'
/tmp/ccnwQeLu.o: In function `MyInit()':
opengl1.cpp:(.text+0x4c): undefined reference to `glGetString'
opengl1.cpp:(.text+0x57): undefined reference to `std::cout'
opengl1.cpp:(.text+0x5c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x6c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0x78): undefined reference to `glGetString'
opengl1.cpp:(.text+0x83): undefined reference to `std::cout'
opengl1.cpp:(.text+0x88): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)'
opengl1.cpp:(.text+0x98): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
opengl1.cpp:(.text+0xc0): undefined reference to `glClearColor'
opengl1.cpp:(.text+0xdf): undefined reference to `glColor3f'
opengl1.cpp:(.text+0xec): undefined reference to `glPointSize'
opengl1.cpp:(.text+0xf8): undefined reference to `glMatrixMode'
opengl1.cpp:(.text+0xfd): undefined reference to `glLoadIdentity'
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccnwQeLu.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
/tmp/ccnwQeLu.o: In function `__static_initialization_and_destruction_0(int, int)':
opengl1.cpp:(.text+0x1cb): undefined reference to `std::ios_base::Init::Init()'
opengl1.cpp:(.text+0x1d0): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccnwQeLu.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
If I use g++ instead of gcc I get this:
vim opebgl1.cpp
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm
and then get this :
/tmp/ccCJBuIl.o: In function `MyInit()':
opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D'
/tmp/ccCJBuIl.o: In function `main':
opengl1.cpp:(.text+0x14a): undefined reference to `glutInit'
opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode'
opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize'
opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition'
opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow'
opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc'
opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop'
collect2: ld returned 1 exit status
So what do I really need to start working with opengl in Ubuntu?
Add "-lstdc++ -lGL" to your linker flags.
Or try to compile it like this:
g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm
(edit: added -lGLU, removed -lstdc++)
You should link it with glut and GLU as well:
g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU
Files ended with .cpp must be compiled with g++. The rest of the errors are related to the linking process, and they should not happen if you build your application with the command I suggested above. If they do, make sure you have installed libglut and libglu.
Make sure you are using g++ to build your C++ files to avoid the linker errors pertaining to the C++ standard library.
To resolve the GLUT symbols you probably just need to add -lglut to your final build command.
While compiling the code you must mention some parameters according to your includes.
You already used -lGL, -lm etc.
To use gluOrtho2D the parameter to be used is -lGLU
and for other functions that begin with glut use -lglut