Include library path linux

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 $ 

Источник

How to set the environmental variable LD_LIBRARY_PATH in linux

I have first executed the command: export LD_LIBRARY_PATH=/usr/local/lib Then I have opened .bash_profile file: vi ~/.bash_profile . In this file, I put:

LD_LIBRARY_PATH=/usr/local/lib export LD_LIBRARY_PATH 

Then if the terminal is closed and restarted, typing echo $LD_LIBRARY_PATH displays no result. How to set the path permanently?

Pointing it out the obvious here. If you just want to fix what you did, add a $ , e.g. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH

Читайте также:  Remote connect windows to linux

this might be silly but did you $ source ~/.bash_profile ? I tend to forget that. And then like @neckTwi said run ldconfig

12 Answers 12

You should add more details about your distribution, for example under Ubuntu the right way to do this is to add a custom .conf file to /etc/ld.so.conf.d , for example

sudo gedit /etc/ld.so.conf.d/randomLibs.conf 

inside the file you are supposed to write the complete path to the directory that contains all the libraries that you wish to add to the system, for example

remember to add only the path to the dir, not the full path for the file, all the libs inside that path will be automatically indexed.

Save and run sudo ldconfig to update the system with this libs.

Is this really the right way? Suppose you have multiple users building local libraries, and you’ve added both of them to /etc/ld.so.conf.d. Now user A can link to user B’s local libraries. Not good.

This was a lifesaver to resolve this problem: ‘ImportError: libpq.so.5: cannot open shared object file: No such file or directory error in SageMaker when trying to import psycopg2. Found the path using: $ find ~/ -name libpq.so.5 Then used @user1824407 suggestion: sudo vim /etc/ld.so.conf.d/pyscopg.conf followed by SageMaker location for libpg.so.5: /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/ then ran sudo ldconfig. Magic.

This didn’t work for me, because afterwards, even after rebooting, echo $LD_LIBRARY_PATH is empty. (Ubuntu 20.04)

Keep the previous path, don’t overwrite it:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/ 

You can add it to your ~/.bashrc :

echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/' >> ~/.bashrc 

Don’t ask me why, but this did not work, until I made this small change: mv ~/.bashrc ~/.bash_profile with otherwise the same code and same file privileges. It then worked. This is on Ubuntu 20.04.1 LTS. Without this file rename, LD_LIBRARY_PATH would still be empty on re-login. .bashrc was rwx for user, so it should execute fine. Executing it manually would also not set LD_LIBRARY_PATH (somewhat expected), and sourcing it ( . ./.bashrc ) did set it. Very odd, and I expect it’s Ubuntu 20.04.1 LTS specific.

CORRECTION: I should take my own advice and actually read the documentation. It says that this does not apply to LD_LIBRARY_PATH: Since Ubuntu 9.04 Jaunty Jackalope, LD_LIBRARY_PATH cannot be set in $HOME/.profile, /etc/profile, nor /etc/environment files. You must use /etc/ld.so.conf.d/.conf configuration files.* So user1824407’s answer is spot on.

Читайте также:  Время выключения экрана линукс

Wow, I looked at that page several times and overlooked that. Thanks for spotting it and bringing it to our attention.

Ubuntu is a special case here. Ubuntu’s view is that all users want the same libraries and that there is only one place for each library (read discussion in Launchpad bug #366728). However, on multi-user research or development systems, you want each users to be able to have their own versions of libraries. Most Linux distributions allow this and have done so for many years.

Spent 4 hours trying to figure why I can set PATH and PKG_CONFIG_PATH, but not LD_LIBRARY_PATH. I can’t even.

@JoachimWagner Yes, this is correct, and why conda and/or bioconda or virtenv is now a preferred method on invoking a particular program with special environmental variables. By using encased environments, inadvertently setting the LD_LIBRARY path will not have systemic issues/problems.

Alternatively you can execute program with specified library dir:

/lib/ld-linux.so.2 --library-path PATH EXECUTABLE 

Yeah I agree with spt025 — I never knew that trick was possible. I feel there is so much sort of hidden away stored in some archaic manpage at best . so StackOverflow is really helpful for THAT particular kind of information.

The file .bash_profile is only executed by login shells. You may need to put it in ~/.bashrc , or simply logout and login again.

For some reason no one has mentioned the fact that the bashrc needs to be re-sourced after editing. You can either log out and log back in (like mentioned above) but you can also use the commands: source ~/.bashrc or . ~/.bashrc .

Put export LD_LIBRARY_PATH=/usr/local/lib in ~/.bashrc [preferably towards end of script to avoid any overrides in between, Default ~/.bashrc comes with many if-else statements]

Post that whenever you open a new terminal/konsole, LD_LIBRARY_PATH will be reflected

  1. Go to the home folder and edit .profile
  2. Place the following line at the end export LD_LIBRARY_PATH=
  3. Save and Exit.
  4. Execute this command sudo ldconfig

You could try adding a custom script, say myenv_vars.sh in /etc/profile.d .

cd /etc/profile.d sudo touch myenv_vars.sh sudo gedit myenv_vars.sh 

Add this to the empty file, and save it.

export LD_LIBRARY_PATH=/usr/local/lib 

Logout and login, LD_LIBRARY_PATH will have been set permanently.

Awesome answer. Best choice for me to export a path every time during system initialisation and much safer than physically editing /etc/bashrc

Читайте также:  Linux process executable path

I do the following in Mint 15 through 17, also works on ubuntu server 12.04 and above:

scroll to the bottom, and add:

All users have the environment variable added.

In Ubuntu 20.04 Linux this is just not obvious and straight forward as it should be.

I will attempt to make it simple for anyone who is pulling out their hair just like I was with my Ubuntu 20.04.3 Linux.

Start by identifying the path where your library files’ folder is located. In my case, the *.so files that I was working with were located in a folder called libs and this folder’s path in my Ubuntu box is /usr/lib

So now I want to add the path /usr/lib to LD_LIBRARY_PATH such that when I run echo $LD_LIBRARY_PATH in my Ubuntu terminal I will be able to see the path /usr/lib echoed as shown below;

joseph$ echo $LD_LIBRARY_PATH :/usr/lib 

Here are the steps I used

  1. Open terminal in Ubuntu 20.04 Linux box
  2. Change path to /etc/ld.so.conf.d/ by running cd /etc/ld.so.conf.d/
  3. Create a file with a *.conf extension at the end with a text editor like e.g. vim or gedit in my case I created it as follows sudo gedit my_project_libs.conf
  4. Inside the .conf file that I created named my_project_libs.conf I added the path to my libs by adding this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib
  5. Thereafter, I then run gedit ~/.bash_profile to open the ~/.bash_profile file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want included in LD_LIBRARY_PATH
  6. I also ran gedit ~/.bashrc to open the ~/.bashrc file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want included in LD_LIBRARY_PATH
  7. When you are done adding the line in step 5, save and close.
  8. In your terminal, type the following sudo ldconfig and press enter on your keyboard. Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.

In my case, this is what I see :/usr/lib when I run echo $LD_LIBRARY_PATH in my newly opened Ubuntu terminal session

joseph$ echo $LD_LIBRARY_PATH :/usr/lib 

That’s how I got it to work for me in my Ubuntu 20.04.3 Linux box.

Источник

Оцените статью
Adblock
detector