Originally Posted by nkoplm
g++ compileme.cpp -l/usr/lib/libOgreMain
although im not sure why using
did not fix this problem as well.
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
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:
gcc -lmath -o myprogram myprogram.c
(this compiles and links in one command).
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:
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
pkg-config --libs OGRE
And this for the compiler options:
pkg-config --cflags OGRE
You can use this in your Makefile or a shell script that does the compiling and linking this way:
# 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:
shell$ pkg-config --libs OGRE
But the correct (or recommended) compiler options are certainly not easily guessed right:
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.