How can I make a shared library which is in /usr/lib/some-path linkable with the g++ -l argument when compiling?

As far as I know, to do what I want, I need to chmod 0755 the library .so file, create some kind of a link file and I need to update the library cache. I tried using ldconfig command and it worked, but not for the subdirectories of /usr/lib. I also tried ln -s /usr/lib/some-path/libmy.so /link/file/output/dir which created a link file, but g++ still couldn't find the library with the -lmy. I tied running ldconfig after ln but that did not work.

  • 1
    Just to clear any possible confusion here: by "creating a link" do you mean creating a symlink to the library in another location or do you mean linking against it during compilation? – Joseph R. Dec 13 '13 at 14:01

Why are you using ldconfig for this? You should use ln:

ln -s /usr/lib/src-con/your_library /path/to/target

where /path/to/target is where you want to create the link.


In light of your edits, I believe what you want is

g++ ... -L /usr/lib/some-path -l my ...

From the output of g++ -v --help:

-L DIRECTORY, --library-path DIRECTORY
Add DIRECTORY to library search path

  • cprogramming.com/tutorial/shared-libraries-linux-gcc.html this is the reason why I use ldconfig. Step 4 "Using ldconfig to modify ld.so" – Qualphey Dec 13 '13 at 13:39
  • And sorry for accepting your answer too early. This command line created the link, but for some reason, when I linked the library in a makefile like this -lmylib, g++ couldn't find it. I tried creating the link in the same subdirectory and in /usr/lib. – Qualphey Dec 13 '13 at 13:46
  • I absolutely remade my question, because I was unconscious when I was writing it the first time. :) – Qualphey Dec 14 '13 at 17:13
  • @VitaliusKuchalskis Please see the update. – Joseph R. Dec 14 '13 at 20:04
  • 1
    @VitaliusKuchalskis Have you tried symlinking the library in /usr/lib/ instead of the object file output directory? Also, that requirement isn't clear from your question. Could you please add it to the question text? – Joseph R. Dec 14 '13 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.