1

In Ubuntu 12.04.1 LTS I created a .so library file using this code:

g++ -c -Wall -Werror -fPIC someCode.cpp
g++ -shared -o libSomeCode.so someCode.o

I need to use that library file within an executable. But when running the .exe it cannot find the .so file. So I have to copy the library to /usr/lib/. I tried using this command (without success):

export LD_LIBRARY_PATH=/home/personalFolder/Desktop/codeFolder:$LD_LIBRARY_PATH

Is there a way to avoid copying the .so to /usr/lib/?

Thanks in advance.

JMFS
  • 297
  • 1
  • 4
  • 11
  • 1
    The `LD_LIBRARY_PATH` method should work. Did you run the executable in the same shell, after setting the variable? Also, when you say `.exe`, you are talking about an executable compiled for your platform, right? Not a Windows executable..? – jogojapan Jul 18 '13 at 02:41
  • Well, it works after setting the LD_LIBRARY_PATH manually in a shell, but it fails when the statement is inside a makefile. I tried using ldconfig but it requires root permissions (and I am avoiding them) – JMFS Jul 18 '13 at 02:49
  • Can you try using `-rpath`: http://stackoverflow.com/a/695684/1538531 – Derek Jul 18 '13 at 02:52
  • Ok, but I got: cc: error: unrecognized option ‘-rpath’ – JMFS Jul 18 '13 at 02:54
  • 2
    If it's in a makefile recipe, then you have to put it on *the same instance of the shell*. This is doable by putting a '; \' at the end of the line doing the LD_LIBRARY_PATH set before invoking the program. You're better off using an rpath setting for the link of the app so it can find the library. – Anya Shenanigans Jul 18 '13 at 02:56
  • Thanks! that fixed the problem! How do I mark this question as solved? – JMFS Jul 18 '13 at 03:00
  • Did you forget `-Wl`? `-rpath` is a linker option, not a compiler option. – Derek Jul 18 '13 at 03:01
  • Thanks, but using -Wl I got cc1: error: unrecognized command line option ‘-Wl’. Oh, I forgot to ask, is there a way of using $(pwd) instead of the path manually entered? – JMFS Jul 18 '13 at 03:13
  • Please show your `Makefile` recipe, or the compilation command. BTW, I suggest to compile with `g++ -Wall -g -Werror -fPIC someCode.cpp -shared -o libSomeCode.so` till you have no bugs (and then, replace the `-g` with `-O`) – Basile Starynkevitch Jul 18 '13 at 05:27

1 Answers1

0

Adding "; \" solved my problem.

JMFS
  • 297
  • 1
  • 4
  • 11
  • 1
    please make the answer clear enough so that it stands on its own without using the comments above as context. – Brian Cain Jul 18 '13 at 03:40