LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-19-2009, 02:09 AM   #1
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Rep: Reputation: 15
problem with g++ linker. cannot find library.


hello,

I started up a small coding project today to test out OGRE graphics rendering engine, but I'm having trouble getting the compiler to work properly. I am wondering if any of you have had this issue before and may know what is happening.

When i try to compile, the linker is unable to find the library, yet it is clearly (as far as i can tell) available.

here is a link to a screen shot of what is going wrong.
http://img190.imageshack.us/img190/2...eenshotuui.png

In the console, you can see that the libOgreMain.so file exists on my file system, but in the GCC linker output window, it is saying that it cannot find the file.

why cant gcc find the library?
 
Old 06-19-2009, 03:43 AM   #2
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Original Poster
Rep: Reputation: 15
I found the solution.
If anyone else comes across this later, the problem was the -l argument I was passing to g++ included the extension which it should not have.
in other words something like this:
Code:
g++ compileme.cpp -l/usr/lib/libOgreMain.so
should have been
Code:
g++ compileme.cpp -l/usr/lib/libOgreMain
although im not sure why using
Code:
 -L/usr/lib
did not fix this problem as well.
 
Old 06-19-2009, 06:05 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by nkoplm View Post
Code:
g++ compileme.cpp -l/usr/lib/libOgreMain
although im not sure why using
Code:
 -L/usr/lib
did not fix this problem as well.
The -L option works like a search path for libraries, just like $PATH in the shell is a search path for executable files.

And just like the shell has a default search path, the linker also has a default library search path, with should include /usr/lib. So you should not even need to have to use a -L/usr/lib option. The reason why that did not work for you is that you use a full path with the -l option.

Normally with the -l option the "extension" is left out from the file name, as well the lib prefix, and the directory.

The most basic example is probably the "math" library (often used to have the sqrt() (square root) function available). The "math" library is in the file: /usr/lib/libmath.so. To link in the math library you would use this command line:
Code:
gcc -lmath -o myprogram myprogram.c
(this compiles and links in one command).

No -L was needed here, because /usr/lib is already in the library search path. The reason why it did not (seem to) work for you without the -L option is that you were specifying the full path of the library file.

So for the OGRE library, this should work for you:
Code:
g++ compileme.cpp -lOgreMain
The reason why the "extension" should not be given, I suppose, is that the linker can then decide whether to use either the static version or the shared object ("dynamic") version of the library. And I guess the reason for leaving out the "lib" prefix of the file name is just historical.

Nowadays library packages come with a pkg-config file on most (all?) linux distributions, which makes it easier to have the correct compiler and linker command line options for g++/gcc. For example this command should output the options for the linking against the OGRE library:a
Code:
pkg-config --libs OGRE
And this for the compiler options:
Code:
pkg-config --cflags OGRE
.

You can use this in your Makefile or a shell script that does the compiling and linking this way:
Code:
# Command to only compile (-c option)
g++ `pkg-config --cflags OGRE` -c yourprogram.cpp

# To only link:
g++ `pkg-config --libs OGRE` yourprogram.o yourprogram
Using "pkg-config" will help compile/link your program on other distributions where sometimes libraries or header files are not in the default search paths. The distribution makes sure that "pkg-config <lib-name>" will output the correct -L and -I (and other) options for that specific distribution or library package.

Also it makes it very easy not to leave out compile/link options that may be important for a specific library. Like, the linker flags for OGRE as output by pkg-config on my (ubuntu) system is quite trivial:
Code:
shell$ pkg-config --libs OGRE
-lOgreMain
But the correct (or recommended) compiler options are certainly not easily guessed right:
Code:
shell$ pkg-config  --cflags OGRE
-DOGRE_GUI_GLX -DOGRE_CONFIG_LITTLE_ENDIAN -I/usr/include/OGRE
To see which libraries supported by pkg-config are installed on your system, run: pkg-config --list-all.

Hope this helps.

P.S. The -I works for header files, just like -L for libraries in the sense that the -I option can be used to add a directory to the search path for header files. Though -I is often not needed since the header search path include the most common directories.

Last edited by Hko; 06-19-2009 at 06:16 AM.
 
  


Reply

Tags
compile, g++, gcc, libs, link, linker, pkgconfig


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tiff library - linker error nimra Programming 3 10-08-2011 04:54 AM
A linker problem with a couple of library needed by VirtualBox crisostomo_enrico Solaris / OpenSolaris 4 09-14-2008 07:43 PM
problem to find my libgfortran.so.0 library mihalisla Linux - Newbie 4 06-29-2006 03:32 AM
Linker problem: can't find a file, but the file exists atlep Programming 5 08-16-2004 07:15 AM
flash problem, can't find apropriate library. schatoor Linux - Software 1 05-24-2003 05:38 AM


All times are GMT -5. The time now is 08:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration