g++ undefined reference with a shared library [duplicate] - c++

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 6 years ago.
I'm trying to compile a file with a shared library using the following command:
g++ -L. -lsubmit main.cpp
It outputs:
/tmp/ccRFpx1v.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `Submit_test()'
collect2: error: ld returned 1 exit status
I have main.cpp and libsubmit.so in working directory. Here's my main.cpp
void Submit_test();
int main()
{
Submit_test();
}
Here's nm -D --demangle libsubmit.so:
...
0000000000000e0e T Submit_test()
...
How should I compile that code?

This is probably a dup of how-to-know-if-one-shared-library-depends-on-another-shared-library-or-not
TLDR is that you should put linked libs after object/source files.

Related

Cuda compile error when "cuMemGetAddressRange" exists [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
Here comes the sample codes:
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
int main() {
unsigned char* cu_test;
cudaMalloc((void**)&cu_test, 3200);
CUdeviceptr pbase;
size_t psize;
CUresult res = cuMemGetAddressRange(&pbase, &psize, (CUdeviceptr)cu_test);
printf("cu_img_yuv size: %ld", psize);
return 0;
}
While it throws error when compiling whatever the cuda version is(tested from 11.3 to 11.5):
$ nvcc main.cu -o main
/tmp/tmpxft_0000e288_00000000-11_main.o: In function `main':
tmpxft_0000e288_00000000-6_main.cudafe1.cpp:(.text+0x54): undefined reference to `cuMemGetAddressRange_v2'
collect2: error: ld returned 1 exit status
Can someone help pointing out what the problem is plz?
The message:
error: ld returned 1 exit status
denotes that it is a linking error ( see ld ).
To see this is the case, you can run
nvcc -c main.cu -o main.o
and you will not get any error. This is the source-code compilation step!
Solution:
You need to explicitly specify linkage with the CUDA driver stub library:
nvcc main.cu -o main -lcuda
That is because cuMemGetAddressRange() is part of the CUDA Driver API, not the CUDA runtime API.
NOTE: you did not cudaFree() the allocated memory, you might want to fix this!
Edit: (credit to #talonmies comment) You do not have to explicitly link against the CUDA runtime library (-lcudart), because nvcc will automatically link against it.

gcc can't find reference to function DoIt() when linking [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am currently becoming crazy. It seems like there is a problem with gcc and it can't open include files can't find the function DoIt() when linking. I tried compiling this code in code blocks and it didn't work so I tried it with G++ in the console and it still didn't work. So I think it's a problem with gcc.
This is my code
main.cpp
#include <iostream>
#include "source.h"
int main()
{
std::cout<<"That works"<<std::endl;
DoIt();
while(true)
{
}
return 0;
}
source.cpp
#include "source.h"
#include <iostream>
void DoIt()
{
std::cout<<"That works too"<<std::endl; //Currently doesn't work
}
source.h
void DoIt();
And this is what I wrote in the terminal
g++ main.cpp -std=c++11 -o result
This is the error message when i run it
/tmp/ccG6X4Bw.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `DoIt()'
collect2: error: ld returned 1 exit status
I have no clue why it doesn't work
By default, gcc will try to compile and link. Linking without the code from source.cpp will cause the linker to be unable to link the call of DoIt to its code. If you just wish to compile, pass -c to gcc.
From the man page:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.
...
-c Compile or assemble the source files, but do not link.
The linking stage simply is not done.

Can't link a dynamic lib including iostream [duplicate]

This question already has answers here:
Using only g++ works, but not "g++ -c" and ld
(4 answers)
Closed 6 years ago.
ld is acting weirdly and I want to understand what's going on. In a mycode.cpp, I have the following:
#include <algorithm>
#include "mycode.hpp"
// awesome stuff here
I compile it with g++ -fPIC -c mycode.cpp and link it with ld -Bshareable -o libmylib.so mycode.o. Works like a charm.
Then I want to call cout in mycode.cpp. Actually, even before adding this cout, if I just add #include <iostream> in the code above, while linking I get the error
mycode.o: In function `__static_initialization_and_destruction_0(int, int)':
mycode.cpp:(.text+0x50): undefined reference to `__dso_handle'
ld: mycode.o: relocation R_X86_64_PC32 against undefined hidden symbol `__dso_handle' can not be used when making a shared object
ld: final link failed: Bad value
If I link it with g++ -shared, it works, but that's not the point. I do not understand what's wrong here, and I am looking for insights.
EDIT: I understand one must call g++ instead of ld directly. My problem is, I want to understand what's under the hood: why including iostream break things while algorithm is already here (so ld knows stdc++)
link it with ld -Bshareable -o libmylib.so mycode.o. Works like a charm.
It only works by accident.
User-level code should never be linked directly with ld, and should always use the appropriate compiler driver (g++ here) to perform the link. Anything else, and you'll fail with weird link-time or runtime errors (just as you did here).

compile C in CentOS face error: undefined reference to [duplicate]

This question already has an answer here:
Why am I getting a gcc "undefined reference" error trying to create shared objects?
(1 answer)
Closed 7 years ago.
I compiled C in CentOS and face this error:
cc -c -o obj/BT.o BT.c -I./include -I/usr/dialogic/inc
cc -o BT obj/BT.o obj/util.o -I./include -I/usr/dialogic/inc -L/usr /dialogic/lib
obj/BT.o: In function 'main':
BT.c:(.text+0x52): undefined reference to `gc_Start'
collect2: ld returned 1 exit status
make: *** [BT] Error 1
I already linked to library -L/usr /dialogic/lib but i don't know why it does not link when create binary file
Please, any one can suggest me something ?
check if you had saved your.c file and also check of spelling of the functions' definition and their calls .

Trying to run OpenGL on Linux, have the necessary libraries but won't run? (C++) [duplicate]

This question already has answers here:
Linker error : undefined reference to symbol 'glOrtho'
(2 answers)
Closed 8 years ago.
I am new to Linux and am currently using Linux Mint. I followed the instructions on this page, however whenever I go to compile the example code (given on the web page), the build fails.
g++ ~/Desktop/test.cpp -lglut gives me the following:
/usr/bin/ld: /tmp/cc3aKYsD.o: undefined reference to symbol 'glOrtho'
/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Why does this keep occurring? I apologize for such a beginner question, but this is irritating.
You need to link with the OpenGL library as well:
g++ ~/Desktop/test.cpp -lglut -lGL