How to specify the path to lib in gcc - gcc

I have the problem about linking libraries.
"見つかりません" means "Not found"
edit LD_LIBRARY_PATH
$ LD_LIBRARY_PATH="/home/myname/.local/boost/lib" g++ main.cc -lboost_serialization
/home/myname/.linuxbrew/bin/ld: -lboost_serialization が見つかりません
collect2: error: ld returned 1 exit status
edit LIBRARY_PATH
$ LIBRARY_PATH="/home/myname/.local/boost/lib" g++ main.cc -lboost_serialization
/home/myname/.linuxbrew/bin/ld: -lboost_serialization が見つかりません
collect2: error: ld returned 1 exit status
adding the -L option (successfully compiled)
$ g++ main.cc -lboost_serialization -L "/home/myname/.local/boost/lib"
$
Indeed I have successfully compiled my program with the -L option.
But I want to compile without this (like 1 or 2, not 3).
In this case, what do I have to check or modify in my environmental variables?
note: my gcc is 5.3.0 (Homebrew gcc 5.3.0).

Related

gnu ld: symbol remains undefined, even when it is defined

Trying to link a program which (for unknown reasons) generates a reference to "xmemdup".
I found the source code for xmemdup.c, and compiled it, and included the .o file in the link, but ld still claims it is undefined.
$ gcc -Wall -g -O2 -pipe -DBINDIR=\"/usr/bin\" -c -o rsh.o rsh.c
This worked, generating rsh.o
$ ll rsh.o ../publib-0.40/alloc/xmemdup.o
-rw-r--r-- 1 perryh perryh 2344 Dec 29 22:25 ../publib-0.40/alloc/xmemdup.o
-rw-r--r-- 1 perryh perryh 68656 Dec 30 00:47 rsh.o
Proves that the .o files I am about to pass to the linker do exist. BTW xmemdup.o is an object file, not a library, so it should get included unconditionally even if it were not needed to resolve a reference.
$ gcc -Wall -g -O2 -pipe -DBINDIR=\"/usr/bin\" -o rsh rsh.o ../publib-0.40/alloc/xmemdup.o
/usr/bin/ld: symbol lookup error: /usr/bin/ld: undefined symbol: xmemdup
collect2: error: ld returned 127 exit status
and yet the linker can't see the definition of xmemdup in xmemdup.o

ld cannot find shared library even with -L specified

I'm trying to compile a C file against a shared library at a specific location (/home/user/cuda_sync_analyzer-install/lib/libInsertTimingInstr.so), but ld cannot seem to find it -
$ gcc -g -I/home/user/tools/cuda_sync_analyzer/src/InsertTimingInstr \
-L/home/user/cuda_sync_analyzer-install/lib/ \
callback.c -o callback -lInsertTimingInstr.so
/usr/bin/ld: cannot find -lInsertTimingInstr.so
collect2: error: ld returned 1 exit status
I also tried
$ gcc -g -I/home/user/tools/cuda_sync_analyzer/src/InsertTimingInstr \
-L/home/user/cuda_sync_analyzer-install/lib/ \
-lInsertTimingInstr.so callback.c -o callback
/usr/bin/ld: cannot find -lInsertTimingInstr.so
collect2: error: ld returned 1 exit status
as well as setting LD_LIBRARY_PATH
$ export LD_LIBRARY_PATH=/home/user/cuda_sync_analyzer-install/lib/
$ gcc -g -I/home/user/tools/cuda_sync_analyzer/src/InsertTimingInstr \
callback.c -o callback -lInsertTimingInstr.so
/usr/bin/ld: cannot find -lInsertTimingInstr.so
collect2: error: ld returned 1 exit status
The file libInsertTimingInstr.so is present in the directory -
$ ls -l /home/user/cuda_sync_analyzer-install/lib/ | grep Timing
-rw-r--r-- 1 user user 51992 Mar 18 18:04 libInsertTimingInstr.so
but ld cannot seem to find it
This command line: gcc ... -lInsertTimingInstr.so ... asks the linker to link against libInsertTimingInstr.so.a or libInsertTimingInstr.so.so, neither of which exists.
You want: gcc ... -lInsertTimingInstr ... (i.e. drop the lib prefix and the .so suffix).
Alternatively, you could do this: gcc ... /home/user/cuda_sync_analyzer-install/lib/libInsertTimingInstr.so ...

Fix: /usr/bin/ld: cannot find -ly collect2: error: ld returned 1 exit status

I can't compile yacc i lex:
$ cc gramatyka.c gramatyka.h semantyka.c -ly -ll
output error:
/usr/bin/ld: cannot find -ly collect2: error: ld returned 1 exit status
How to fix it?
Most probably you are missing the -L path/to/dir/with/liby in your LDFLAGS (in case liby is in the same folder, just prepend -L. before -ly).
The reason you are getting this error is because compiler is not able to link this library. Add the complete path using "-L" option if you are not sure about default location of this library.

Linking to libusb

I know this is something so simple I'm going to hate myself for having to ask it, but my head is aching from repeated hits on the desktop. I've read dozens of stackoverflow and google results which suggest that the following should work:
$ ls /usr/local/lib/libusb*
/usr/local/lib/libusb-1.0.a /usr/local/lib/libusb-1.0.so /usr/local/lib/libusb-1.0.so.0.1.0
/usr/local/lib/libusb-1.0.la /usr/local/lib/libusb-1.0.so.0
$ gcc -I ~/libusb-1.0.18/libusb -c test.c
$ gcc -L/usr/local/lib -o test test.o -llibusb
/usr/bin/ld: cannot find -llibusb
collect2: error: ld returned 1 exit status
$ gcc -L/usr/local/lib -o test test.o -llibusb-1.0
/usr/bin/ld: cannot find -llibusb-1.0
collect2: error: ld returned 1 exit status
Why is that not correct? One of those should have worked and I've tried many, many more variations.
For completeness I'm running Ubuntu 14.04 (fresh VM installation).
I built libusb from source (~/libusb-1.0.18) with:
./configure --disable-udev
make
sudo make install
The leading lib and trailing .so are automatically filled in by the linker, so you should not specify either on the command line. Your command should be:
gcc -L/usr/local/lib -o test test.o -lusb-1.0

make library not found

I'm trying to compile a program using a third party library, Omnet++ in my case. Apparently "make" does not find a library, but the path it uses is correct as you can see (in the sense that I can see the library under omnet++ source tree)
pv135168:basic Bob$ opp_makemake
Creating Makefile in /Users/Bob/Code/network_sim/basic... Makefile created, running "make depend" to add dependencies... opp_makedep -Y --objdirtree -I. -f Makefile -P\$O/ -- ./*.cc
pv135168:basic Bob$ make
g++ -c -g -Wall
-fno-stack-protector -m32 -DHAVE_PCAP -DXMLPARSER=libxml
-DWITH_PARSIM -DWITH_NETBUILDER -I.
-I/Users/Bob/Code/omnetpp-4.1/include -o out/gcc-debug//txc1.o txc1.cc g++ -m32 -Wl,-rpath,/Users/Bob/Code/omnetpp-4.1/lib -Wl,-rpath,. -o out/gcc-debug//basic out/gcc-debug//txc1.o -Wl,-all_load
-L"/Users/Bob/Code/omnetpp-4.1/lib/gcc"
-L"/Users/Bob/Code/omnetpp-4.1/lib" -u _tkenv_lib -lopptkenvd
-loppenvird -lopplayoutd -u _cmdenv_lib -loppcmdenvd -loppenvird
-loppsimd -lstdc++
ld: library not found for -lopptkenvd
collect2: ld returned 1 exit status make: *** [out/gcc-debug//basic]
Error 1 pv135168:basic Bob$
It's looking in the following directories for a file called libopptkenvd.dylib or libopptkenvd.a:
/Users/Bob/Code/omnetpp-4.1/lib/gcc
/Users/Bob/Code/omnetpp-4.1/lib
Is that file in one of those directories (or in the standard directories like /usr/lib)? I don't see an indication of that in your output.

Resources