How to specify libraries paths in gcc?
I am writing a program using OpenCV calls and I want to compile it and make the executable. I already read the gcc tutorial but I still would like to know exactly how to link the OpenCV libraries I am using. My command and the related output is below:
$ gcc -I/home/savio/opencv-3.0.0/include/opencv -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib \ > -lopencv_core -lopencv_highgui -lopencv_imgproc imagefilter.c -o imagefilter /tmp/ccDUEgAk.o: nella funzione "cvDecRefData": imagefilter.c:(.text+0xa6a): riferimento non definito a "cvFree_" imagefilter.c:(.text+0xaf2): riferimento non definito a "cvFree_" /tmp/ccDUEgAk.o: nella funzione "cvGetRow": imagefilter.c:(.text+0xc08): riferimento non definito a "cvGetRows" /tmp/ccDUEgAk.o: nella funzione "cvGetCol": imagefilter.c:(.text+0xc36): riferimento non definito a "cvGetCols" /tmp/ccDUEgAk.o: nella funzione "cvReleaseMatND": imagefilter.c:(.text+0xc50): riferimento non definito a "cvReleaseMat" /tmp/ccDUEgAk.o: nella funzione "cvSubS": imagefilter.c:(.text+0xdac): riferimento non definito a "cvAddS" /tmp/ccDUEgAk.o: nella funzione "cvCloneSeq": imagefilter.c:(.text+0xde5): riferimento non definito a "cvSeqSlice" /tmp/ccDUEgAk.o: nella funzione "cvSetNew": imagefilter.c:(.text+0xe52): riferimento non definito a "cvSetAdd" /tmp/ccDUEgAk.o: nella funzione "cvGetSetElem": imagefilter.c:(.text+0xefa): riferimento non definito a "cvGetSeqElem" /tmp/ccDUEgAk.o: nella funzione "cvReadIntByName": imagefilter.c:(.text+0xfa9): riferimento non definito a "cvGetFileNodeByName" /tmp/ccDUEgAk.o: nella funzione "cvReadRealByName": imagefilter.c:(.text+0x1053): riferimento non definito a "cvGetFileNodeByName" /tmp/ccDUEgAk.o: nella funzione "cvReadStringByName": imagefilter.c:(.text+0x10e6): riferimento non definito a "cvGetFileNodeByName" /tmp/ccDUEgAk.o: nella funzione "cvReadByName": imagefilter.c:(.text+0x1126): riferimento non definito a "cvGetFileNodeByName" imagefilter.c:(.text+0x113c): riferimento non definito a "cvRead" /tmp/ccDUEgAk.o: nella funzione "cvContourPerimeter": imagefilter.c:(.text+0x1170): riferimento non definito a "cvArcLength" /tmp/ccDUEgAk.o: nella funzione "cvCalcHist": imagefilter.c:(.text+0x11b2): riferimento non definito a "cvCalcArrHist" /tmp/ccDUEgAk.o: nella funzione "cvEllipseBox": imagefilter.c:(.text+0x127e): riferimento non definito a "cvEllipse" /tmp/ccDUEgAk.o: nella funzione "cvFont": imagefilter.c:(.text+0x12c8): riferimento non definito a "cvInitFont" /tmp/ccDUEgAk.o: nella funzione "main": imagefilter.c:(.text+0x1396): riferimento non definito a "cvLoadImage" imagefilter.c:(.text+0x13af): riferimento non definito a "cvGetSize" imagefilter.c:(.text+0x13c1): riferimento non definito a "cvCreateImage" imagefilter.c:(.text+0x13ed): riferimento non definito a "cvSmooth" imagefilter.c:(.text+0x1403): riferimento non definito a "cvSaveImage" imagefilter.c:(.text+0x141b): riferimento non definito a "cvReleaseImage" imagefilter.c:(.text+0x1427): riferimento non definito a "cvReleaseImage" collect2: error: ld returned 1 exit status $
How should I correctly specify the libraries? I ran the command below. It compiles and links but does not run correctly:
$ gcc imagefilter.c -o imagefilter -I/home/savio/opencv-3.0.0/include/opencv \ > -L/home/savio/opencv-3.0.0/cmake_binary_dir/lib \ > -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_core $ ./imagefilter lena_noise.BMP ./imagefilter: error while loading shared libraries: libopencv_imgcodecs.so.3.0: cannot open shared object file: No such file or directory $
Andrew Beacock’s Blog
Sometimes in Linux when you install a new software package the instructions tell you to add a directory of shared libraries to your $LD_LIBRARY_PATH environment variable in your .bashrc.
You may have noticed that if you then create a shortcut icon on your desktop to this application it won’t start because it can’t find the libraries.
A typical solution is to write a wrapper shell script to set the LD_LIBRARY_PATH and then call that application.
Well, I’ve discovered how to add them to your system’s library path allowing all environments to access them. Note: There are differences between Debian and Ubuntu (the two flavours of Linux that I’m familiar with).
Ubuntu
Create a new file in /etc/ld.so.conf.d/ called .conf
Edit the file and add a line per directory of shared libraries (*.so files), it will look something like:
Debian
Edit /etc/ld.so.conf
Add a line per directory of shared libraries (*.so files) to the bottom of the file, it will look something like:
/usr/X11R6/lib
/usr/lib/APPLICATION/lib
If you run your new application it should now work fine without you having to set any LD_LIBRARY_PATH environment variables.
If you still have problems you can obtain a list of the libraries that are on the system path by re-running the ldconfig command in verbose mode:
- Get link
- Other Apps
How do you specify the location of libraries to a binary? (linux)
For this question I’ll be using a specific example, but really this generalizes to pretty much any binary on linux that can’t seem to find its’ dependent libraries. So, I have a program that won’t run because of missing libraries:
./cart5: error while loading shared libraries: libcorona-1.0.2.so: cannot open shared object file: No such file or directory
linux-vdso.so.1 => (0x00007fff18b01000) libcorona-1.0.2.so => not found libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/libstdc++.so.6 (0x00007f0975830000) libm.so.6 => /lib/libm.so.6 (0x00007f09755af000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f0975399000) libc.so.6 => /lib/libc.so.6 (0x00007f0975040000) libz.so.1 => /lib/libz.so.1 (0x00007f0974e2b000) /lib64/ld-linux-x86-64.so.2 (0x00007f0975b36000)
oliver@human$ find / -name libcorona-1.0.2.so 2> /dev/null /usr/local/lib64/libcorona-1.0.2.so /home/oliver/installed/corona-1.0.2/src/.libs/libcorona-1.0.2.so
3 Answers 3
For a once-off, set the variable LD_LIBRARY_PATH to a colon-separated list of directories to search. This is analogous to PATH for executables, except that the standard system directories are additionally searched after the ones specified through the environment.
LD_LIBRARY_PATH=/usr/local/lib64 ./cart5
If you have a program that keeps libraries in a non-standard location and isn’t able to find them on its own, you can write a wrapper script:
#!/bin/sh if [ -n "$LD_LIBRARY_PATH" ]; then LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64 else LD_LIBRARY_PATH=/usr/local/lib64 fi export LD_LIBRARY_PATH exec /path/to/cart5 "$@"
The list of standard system directories is kept in /etc/ld.so.conf . Recent systems allow this file to include other files; if yours contains something like include /etc/ld.so.conf.d/*.conf , create a new file called /etc/ld.so.conf.d/mala.conf containing the directories you want to add. After you change /etc/ld.so.conf or an included file, run /sbin/ldconfig for your changes to take effect (this updates a cache).
( LD_LIBRARY_PATH also applies to many other unices, including FreeBSD, NetBSD, OpenBSD, Solaris and Tru64. HP-UX has SHLIB_PATH and Mac OS X has DYLD_LIBRARY_PATH . /etc/ld.so.conf has analogs on most unices but the location and syntax differs more widely.)
How to add include and lib paths to configure/make cycle?
I need a place to install libraries in a linux box I have no su access to. I’m using ~/local[/bin,/lib,/include], but I don’t know how can I tell ./configure to look for libraries there (particularly, I’m trying to compile emacs, which needs libgif, which doesn’t come in my distro). I tried adding
export PATH=$PATH:~/local/bin export LD_LIBRARY_PATH=~/local/lib export C_INCLUDE_PATH=~/local/include export CPLUS_INCLUDE_PATH=~/local/include
Note that excepted for the PATH, you overwrite your system default ones. If you make something like export C_INCLUDE_PATH=~/local/include:$C_INCLUDE_PATH , your compiler will search firstly in ~/local/include , and in $C_INCLUDE_PATH only if it didn’t found the include in the first directory.
6 Answers 6
You want a config.site file. Try:
$ mkdir -p ~/local/share $ cat ~/local/share/config.site CPPFLAGS=-I$HOME/local/include LDFLAGS=-L$HOME/local/lib . EOF
Whenever you invoke an autoconf generated configure script with —prefix=$HOME/local, the config.site will be read and all the assignments will be made for you. CPPFLAGS and LDFLAGS should be all you need, but you can make any other desired assignments as well (hence the . in the sample above). Note that -I flags belong in CPPFLAGS and not in CFLAGS, as -I is intended for the pre-processor and not the compiler.
William; A thanks, that was bang on advice about CPPFLAGS for building on a shared host. Saved much thrashing about with failed ./configure runs today, because of your note.
Set LDFLAGS and CFLAGS when you run make:
$ LDFLAGS="-L/home/me/local/lib" CFLAGS="-I/home/me/local/include" make
If you don’t want to do that a gazillion times, export these in your .bashrc (or your shell equivalent). Also set LD_LIBRARY_PATH to include /home/me/local/lib:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/me/local/lib
when I add LDFLAGS and CFLAGS to .bashrc ./configure fails with a configure: error: C compiler cannot create executables.
They are probably overriding the configure test code compiler parameters. Just use the make env prefetching. Also try and see if configure doesn’t already have an option for overriding libgif. If the autotools scripts are properly made it will alow you to spec a path where libgif is installed (e.g. override the default library location). That would be the cleaner solution.
same difference.. tneme@ws16lab07:~/Descargas/emacs-23.3$ LDFLAGS=»-L/home/tneme/local/lib» CFLAGS=»-l/home/tneme/local/include» ./configure —prefix=»/home/tneme/local» checking build system type. i686-pc-linux-gnu checking host system type. i686-pc-linux-gnu checking for gcc. gcc checking whether the C compiler works. no configure: error: in /home/tneme/Descargas/emacs-23.3′: configure: error: C compiler cannot create executables See config.log’ for more details t
just tried configuring emacs 23 with these and I didn’t get that problem: LDFLAGS=»-L/home/me/local/lib» CFLAGS=»-I/home/me/local/include» ./configure checking build system type. x86_64-unknown-linux-gnu checking host system type. x86_64-unknown-linux-gnu checking for gcc. gcc checking for C compiler default output file name. a.out checking whether the C compiler works. yes checking whether we are cross compiling. no checking for suffix of executables. checking for suffix of object files. o checking whether we are using the GNU C compiler. yes
This took a while to get right. I had this issue when cross-compiling in Ubuntu for an ARM target. I solved it with:
PATH=$PATH:/ccpath/bin CC=ccname-gcc AR=ccname-ar LD=ccname-ld CPPFLAGS="-nostdinc -I/ccrootfs/usr/include . " LDFLAGS=-L/ccrootfs/usr/lib ./autogen.sh --build=`config.guess` --host=armv5tejl-unknown-linux-gnueabihf
Notice CFLAGS is not used with autogen.sh/configure, using it gave me the error: «configure: error: C compiler cannot create executables». In the build environment I was using an autogen.sh script was provided, if you don’t have an autogen.sh script substitute ./autogen.sh with ./configure in the command above. I ran config.guess on the target system to get the —host parameter.
After successfully running autogen.sh/configure, compile with:
PATH=$PATH:/ccpath/bin CC=ccname-gcc AR=ccname-ar LD=ccname-ld CPPFLAGS="-nostdinc -I/ccrootfs/usr/include . " LDFLAGS=-L/ccrootfs/usr/lib CFLAGS="-march=. -mcpu=. etc." make
The CFLAGS I chose to use were: «-march=armv5te -fno-tree-vectorize -mthumb-interwork -mcpu=arm926ej-s». It will take a while to get all of the include directories set up correctly: you might want some includes pointing to your cross-compiler and some pointing to your root file system includes, and there will likely be some conflicts.
I’m sure this is not the perfect answer. And I am still seeing some include directories pointing to / and not /ccrootfs in the Makefiles. Would love to know how to correct this. Hope this helps someone.