I set the flag -Wl,-R/usr/local/lib in the link phase of g++ and
everything works fine now.
Thanks very much for the help Mr. Freyn and Dr. Kirby.
Alex
On 4/17/2011 4:54 AM, Axel Freyn wrote:
Hi Alex,
On Sat, Apr 16, 2011 at 09:55:48PM -0700, Alex Chen wrote:
After I set the compiler flag, I get the following compilation error:
Building file: /home/Projects/Cache.cpp
Invoking: GCC C++ Compiler
g++ -I"/home/Projects/include" -O0 -g3 -Wall -c -fmessage-length=0
-Wno-pragmas -m64 -fPIC -R"/usr/local/lib" -MMD -MP -MF"Cache.d"
-MT"Cache.d" -o"Cache.o" "/home/Projects/Cache.cpp"
g++: unrecognized option '-R/usr/local/lib'
(Whether there is a space between -R and /usr/local/lib does not matter,
they all produce the error.)
If I use that flag for the linker, I get a different linker error:
Building target: libC.so
Invoking: GCC C++ Linker
g++ -L"/home/alexchen/Projects/lib" -R/usr/local/lib -shared -o"libC.so"
./Disk.o ./Cache.o -luuid -latomic_ops
g++: unrecognized option '-R/usr/local/lib'
Finished building target: libC.so
By the way, I am using Eclipse C++ plug-in so the flag is set via the
project's compiler/linker 'Miscellaneous' setting.
You passed the "-R" to the compiler, but you should pass it to the
linker.
Also in the second example: "g++" ist NOT the linker (it will call the
linker "ld" itself).
So to correct the problem: you have to tell the compiler that
"-R/usr/local/lib" is an option for the linker. That is done using
"-Wl,..."
So, the command
g++ -I"/home/Projects/include" -O0 -g3 -Wall -c -fmessage-length=0 -Wno-pragmas -m64 -fPIC -Wl,-R"/usr/local/lib" -MMD -MP -MF"Cache.d" -MT"Cache.d" -o"Cache.o" "/home/Projects/Cache.cpp"
should work.
(See also http://gcc.gnu.org/faq.html#rpath)
Axel
On 4/16/2011 8:54 PM, Alex Chen wrote:
Thanks for the information, Dr. Kirby.
Is that -R flag for the compiler or the linker?
I thought the linker's -L flag will tell it to look for libraries there. I
guess it does not embed the path in the final library file.
The issue is that the run time linker does not know to look in
/usr/local/lib for
libraries, so unless the path is hard-coded into the executable, the
libraries
will not be found.
Some programs are compiled in such a way they automatically look in
/usr/local/lib. You should find that by recompiling all your sources with
the
compiler flag "-R /usr/local/lib" will work.
Setting
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
will probably "solve" your problem, in that your code will probably find
the
libraries. But it's not a very good solution.
You can change the places the run time linker looks for libraries. You
could
make it always look in /usr/local/lib, but such a change is a bad idea
IMHO.
Dave