I need to link a shared library (LuaSocket) I'm compiling against another special shared library, liblua5.1, that isn't in one of the normal locations. To do this I'm modifying the Makefile.
I cannot figure out what I'm doing wrong, but this particular step that I modified fails:
LIBRARY_PATH=/media/sda2/crank/lib gcc -O -shared -fpic -l liblua5.1 -o socket.so.2.0.2 [...]
(where [...] is a list of .o files that just got built). When I build, I get the error
/usr/lib/gcc/arm-poky-linux-gnueabi/4.8.1/../../../../arm-poky-linux-gnueabi/bin/ld: cannot find -lliblua5.1
collect2: error: ld returned 1 exit status
make: *** [socket.so.2.0.2] Error 1
Inspection of the LIBRARY_PATH confirms that the needed library is there:
# ls /media/sda2/crank/lib/
lgre.so libgre.so libgreio.a liblua.so liblua5.1.so libsbexternal.so
What am I doing wrong?
Change -l liblua5.1 to -llua5.1.
Also, instead of setting LIBRARY_PATH, why not use the -L option? Example: -L/media/sda2/crank/lib.
Related
I usually use gcc to compile my C program, it works ok, but when I tried to compile static library with -static parameter it always failed.
Although I tried some solutions on google, but it still didn't get fixed.
My command is as follows:
gcc mycode.c -static -L . -lurl -lcap -o mycode
The error message is:
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
but when I remove -static it works very well.
GCC's -static linkage option directs the linker to ignore shared libraries
during the linkage. So it must find static versions of all the libraries required
by the linkage, including those that are linked by default, such as libc.
You have not installed the static version of libc (which would be /usr/lib/???/libc.a), so:
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
libc.a is installed by the libc development package. The name of the libc
development package and how to install it depends on your distro. E.g. On Debian
or Ubuntu, the package to install is libc6-dev; on Fedora it is glibc-develop.
But before you go to do that, hang on a tick. You said:
I tried to compile static library with -static parameter it always failed.
gcc mycode.c -static -L . -lurl -lcap -o mycode
That sounds rather as if you just wanted to link your program with one or both
static libraries liburl.a, libcap.a, located in ./, and thought you should
do it by passing -static to the linkage.
There is no need to pass -static to link your program with ./liburl.a and/or
./libcap.a. The options:
-L . -lurl -lcap
will direct the linker to search in ./ for either of the files liburl.so (shared library)
or liburl.a (static library) and if it finds one or other of them it will link your
program with that library. If it finds both of them in ./, then it will choose the
shared library liburl.so. So unless you have ./liburl.so as well as ./liburl.a
then:
-L . -lurl
by itself will link your program against ./liburl.a.
And likewise for -lcap. No need for -static. The default shared library libc.so
will be linked automatically. The linker has no problem at all linking your program
with some static libraries and some shared ones. That is what is already happening
with your successful linkage:
gcc mycode.c -L . -lurl -lcap -o mycode
assuming that liburl.a and libcap.a are the only candidates for resolving
-lurl and -lcap in ./.
And even if you do have both ./liburl.a and ./liburl.so - and/or ./libcap.a and ./libcap.so - there is still no
need for a solution as drastic as a fully static linkage. You can just explicitly
tell the linker to find a particular static library if that's what you want, like:
gcc mycode.c -L . -l:liburl.a -l:libcap.a -o mycode
I am working with c++ code for a physics simulation, which uses a lot of external libraries (like GSL and cern`s ROOT). Trying to recompile project I encountered problems with linking. When running compilation of final file via:
g++ -fno-inline -O2 -fpic -o main.out ${ROOTINCS} main.o ext.o ${ROOTLIBS} $(objects2)
with :
objects2= many .o files made by us
ROOTLIBS=-L/usr/local/lib/root -lTree -lRIO -lNet -lHist -lMathCore -lCore -lGraf -lGraf3d -lGpad -lMatrix -lThread -lCint -lPhysics -lPostscript -lRint -lSpectrum -lg
ROOTINCS=-pthread -m64
I get annoying error:
/usr/bin/ld: /usr/local/lib/root/libHist.so: undefined reference to symbol 'gRandom'
/usr/local/lib/root/libMathCore.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
The problem is nm -C run on libMathCore states 'gRandom' is declared there. Also -lMathCore is present in my command line.
When I run ld to check if it understands the flag:
ld -L/usr/local/lib/root -lMathCore --verbose 2>/dev/null
it does not complain and tries to link properly.
According to https://stackoverflow.com/a/24675715/3602168 order of libraries is correct in my linking (libHist uses libMathCOre and therefore is stated first).
Compilation runs under g++ 4.8.2 on ubuntu 14.04, 64 bit
Converting comment to answer:
Have you tried moving $(objects2) before ${ROOTLIBS}? I think the issue may be that you have libraries specified before the object files that use them.
I have successfully installed this library. My code compiles without issue but I'm getting this error, which I believe is during linking:
suvamsh#vai:~/gpujpeg-lib/src$ nvcc test.cu
/tmp/tmpxft_00002869_00000000-13_test.o: In function 'encoder()':
tmpxft_00002869_00000000-1_test.cudafe1.cpp:(.text+0x23d): undefined reference to `gpujpeg_set_default_parameters(gpujpeg_parameters*)'
collect2: ld returned 1 exit status
I have even set $LD_LIBRARY_PATH to location of the library .so file.
Any help would be greatly appreciated!
As indicated by #talonmies,
You will have to explicitly specify the library to the compiler and supply the path where you installed it, if it is in a "non-standard" place.
Try the following command
$ nvcc test.cu -o test -L"path/to/GPUJPEGLibrary" -l"name_of_GPUJPEGLibrary"
Note that if the name of the .so file is libxyz.so, the "name_of_GPUJPEGLibrary" should be "xyz".
For example, if your .so file is /usr/local/lib/libgpujpeg.so, the cmdline should be
$ nvcc test.cu -o test -L"/usr/local/lib" -l"gpujpeg"
When I try to compile static, I am getting the following error:
gcc defrag.c -o abc.exe --static
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
However, the same thing compiles fine without static:
gcc defrag.c -o abc.exe
Question: Why did the compilation failed when static is specified?
The error is occurring becuase "--static" says that all subsequent libraries in your link command must be static ... but you only have a dynamic libc on your system.
Recommended solution:
gcc defrag.c -o abc -lc --static -lmystaticlib
If you're just trying to create a static exe for the sake of having a static exe - I'd recommend "don't". Shared libraries are Good. For many different reasons.
Here's a good link:
Linux static linking is dead?
I am trying to cross compile samba server.
./config works just fine, but when I want to build with make the prompt says:
Linking shared library bin/libsmbclient.so.0
/opt/qnx630/host/linux/x86/usr/bin/ntoarm-ld: cannot find -lpthread
collect2: ld returned 1 exit status
make: *** [bin/libsmbclient.so.0] Error 1
Anyone knows a solution?
had this same problem w/mingw. got libpthreadGC2.a from ftp://sourceware.org/pub/pthreads-win32/dll-latest/lib/x86/, put it in C;\mingw\lib and renamed it to libpthread.a. i didn't know gcc's -l switch means "apprend 'lib' to the front and '.a' to the back" e.g. -lpthread = libpthread.a
Make sure that the pthread library is in the library search path of the linker.