Advertisement

VERY basic linking in Linux

Started by July 05, 2001 09:09 PM
7 comments, last by cliffhawkens 23 years, 1 month ago
I''ve recently decided to actually do something with my vast amounts of free time and learn to program with Linux and SDL (I already know C/C++, but MSVC++ makes it way too easy...).

I''m using Anjuta for my IDE, and I was wondering how the heck do I link to a library??
I put the path of the libs in (/usr/lib), but then what do I put for the library''s name? libSDL? libsdl? libSDL-1.2? libSDL-1.2.so.0? I haven''t the slightest idea, and this is my first linux program; help would be greatly appreciated. (o= erydo =o)
[email=erydo@gdnmail.net" style="color: #ff0000; text-decoration:none; cursor:help;](o= erydo =o)[/email]
quote: Original post by cliffhawkens
I've recently decided to actually do something with my vast amounts of free time and learn to program with Linux and SDL (I already know C/C++, but MSVC++ makes it way too easy...).




I'm using Anjuta for my IDE, and I was wondering how the heck do I link to a library??


I put the path of the libs in (/usr/lib), but then what do I put for the library's name? libSDL? libsdl? libSDL-1.2? libSDL-1.2.so.0? I haven't the slightest idea, and this is my first linux program; help would be greatly appreciated.

(o= erydo =o)




I removed my post due to some IE problems on my part...


Edited by - Cyberdrek on July 5, 2001 10:33:51 PM
[Cyberdrek | ]
Advertisement
Hello,

you might have noticed libs in linux unlike in windows are named with the prefix "lib" and the suffix ".a" however at link time you dont need to add these.
so SDL might be (I am guessing since I havent done anything with SDL) libSDL.a , when you compile at link time you have to enter both the lib path (assuming the libs arent on /usr/lib or whatever your default lib directory is) and the libs you want to link with -L is for lib path and l is for the lib, so an example using command line to compile the file game.c you should type:

gcc game.c -o game -L/usr/lib -lSDL

or if you use a makefile, in 2 steps:


gcc -c game.c # only compile dont link
gcc -c gamefile2.c # only compile dont link
gcc game.o gamefile2.o -o Game -L/usr/lib -lSDL

I dont know about your IDE but if it only asks you about what libs you want to link against and putting libSDL.a or just SDL doesnt work, my advice is to prepend "-l" (L lowercase) to the lib name, if it asks you for a string of libs to feed to GCC then you definely need the "-l"


PS: Remember I dont know what the actual filename of the SDL lib.
And way to go for going Linux!

Edited by - kwizatz on July 5, 2001 10:46:41 PM
SDL needs to link in the X libraries and
stuft.

One way you can do this is

gcc -o foo foo.c `sdl-config --cflags --libs`
But I''m not sure how you would do it with your ide.
Try
sdl-config --cflags --libs
on the shell but you will
have to fool around with the include paths and
libraries I think. Read the faq at www.libsdl.org
they show some other ways of doing it, though not
with your ide.
quote: Original post by Kwizatz
Hello,

you might have noticed libs in linux unlike in windows are named with the prefix "lib" and the suffix ".a" however at link time you dont need to add these.
so SDL might be (I am guessing since I havent done anything with SDL) libSDL.a , when you compile at link time you have to enter both the lib path (assuming the libs arent on /usr/lib or whatever your default lib directory is) and the libs you want to link with -L is for lib path and l is for the lib, so an example using command line to compile the file game.c you should type:

gcc game.c -o game -L/usr/lib -lSDL

or if you use a makefile, in 2 steps:


gcc -c game.c # only compile dont link
gcc -c gamefile2.c # only compile dont link
gcc game.o gamefile2.o -o Game -L/usr/lib -lSDL

I dont know about your IDE but if it only asks you about what libs you want to link against and putting libSDL.a or just SDL doesnt work, my advice is to prepend "-l" (L lowercase) to the lib name, if it asks you for a string of libs to feed to GCC then you definely need the "-l"


PS: Remember I dont know what the actual filename of the SDL lib.
And way to go for going Linux!

Edited by - kwizatz on July 5, 2001 10:46:41 PM


Yeah, but .a is the suffix for a static library. When linux has such a robust dynamic library system, it''s such a shame to waste it. It works mostly the same way as static libs but cuts memory and disk usage. Anyway, .so is the suffix for dynamic libs.
quote: Original post by Kwizatz
...when you compile at link time you have to enter both the lib path (assuming the libs arent on /usr/lib or whatever your default lib directory is)...


There''s a configuration file for specifying which directories to check (in top-to-bottom order of the file) for libraries. I believe it''s /etc/ld.conf (haven''t booted up RH in a while). ld is the library loader program, which is why you modify it''s configuration file.

You should read the man/info pages on all the Linux development tools - they show you how to do a lot of things (you can query a library to find out what functions it contains using ld, or query a program to find out which library contained a function and so on). gcc, make, autoconf, automake, ld, ldconfig, sh...

Another thing is that Linux typically maintains symbolic links to libraries (and you should do the same for any libraries you create) after the following fashion:

The top-level library name is linked to a major-minor named file, which is usually linked to a major-minor-revision file, which may or may not be the actual library). ie, ls -l would give

@libSDL->libSDL-1.2
@libSDL-1.2->libSDL-1.2.0

This allows libraries to be updated and tested in the toolchain by changing one of the links to point at another file (eg the next update to SDL will redefine libSDL-1.2 to point to libSDL-1.2.1) without removing or affecting the existing library. You can even have multiple coexisting versions of a library, useful if you''re deploying to multiple distributions/"platforms". You can also maintain distro- or situation-specific versions of libraries (eg libSDL-rh7-1.2.1, libSDL-rh6-1.2.0) and add top-level links to them (librh7SDL, librh6SDL) which you can then use in your gcc command line (-lrh7SDL, -lrh6SDL).

Good luck.
Advertisement
quote:
Yeah, but .a is the suffix for a static library. When linux has such a robust dynamic library system, it''s such a shame to waste
it. It works mostly the same way as static libs but cuts memory and disk usage. Anyway, .so is the suffix for dynamic libs.


You dont link against gdi32.dll or ddraw.dll on windows, you link against gdi32.lib and ddraw.lib, these libs have simbols pointing to the dll, they are helper libs so you dont have to LoadLibrary/GetProcAddress/FreeLibrary,
the same happens on linux, if you want to link against static libs add -static to my explanation and if you want to link against shared use -shared, gcc does the work

Example:

gcc game.c -o game -lSDL -shared

and there you go.

I didnt know there was a config file, the only mayor program I wrote in linux was a cgi in C using the libmysqlclient libs, what I showed here worked for me
Thanks guys, I was able to get it to work; here''s the command line I used to compile it:

g++ main.cpp -lSDL -lpthread -o main

Pretty simple, but as this was my first compile, I had no idea what to do...thanks =o)

(o= erydo =o)

[email=erydo@gdnmail.net" style="color: #ff0000; text-decoration:none; cursor:help;](o= erydo =o)[/email]
quote: Original post by Kwizatz

Yeah, but .a is the suffix for a static library. When linux has such a robust dynamic library system, it's such a shame to waste
it. It works mostly the same way as static libs but cuts memory and disk usage. Anyway, .so is the suffix for dynamic libs.

quote:
You dont link against gdi32.dll or ddraw.dll on windows, you link against gdi32.lib and ddraw.lib, these libs have simbols pointing to the dll, they are helper libs so you dont have to LoadLibrary/GetProcAddress/FreeLibrary,
the same happens on linux, if you want to link against static libs add -static to my explanation and if you want to link against shared use -shared, gcc does the work

Example:

gcc game.c -o game -lSDL -shared

and there you go.

I didnt know there was a config file, the only mayor program I wrote in linux was a cgi in C using the libmysqlclient libs, what I showed here worked for me


That's funny. I've always linked to dynamic libs like this:

libsomelib.so

gcc test.c -o test -lsomelib

No .a file at all.

Yes, there is a config file, also you can set the search path by setting the LD_LIBRARY_PATH enviroment variable.


Edited by - gph-gw on July 16, 2001 3:05:19 AM

This topic is closed to new replies.

Advertisement