Advertisement

Newbie... compiling

Started by August 25, 2004 12:11 AM
2 comments, last by matt_j 20 years, 2 months ago
OK, I have a small program which needs to be compiled for linux... something which I don't know how to do, but is the platform I have to compile it for. It consists of 2 source files. It reqires socket and MySQL libraries. What is the simplest process to compile this? With Borland and MSVC, I know how to link different libraries, but have no idea how the make tools work in linux.
Use an IDE like KDevelop, just create a new hello world C++ project, and add your files (using the automake manager on the right). In the automake manager you can also add libraries.

Libraries on linux are named like this :

libfoo.so.1.2.3 (the library named foo version 1.2.3)

If you want to link your app to this library you need to pass -lfoo to the compiler. Like I said above you can easily do this with KDevelop's automake manager.

For sockets you don't need to link anything. You will have to figure out the mysql libs for yourself (should be in the docs).
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Advertisement
To compile the program (assuming you're using gcc), you would do something like
gcc -c sourcefile1.c
(the -c indicates that it should create an object file called sourcefile1.o). Do this with both files, then link them together.

To link libraries in linux, you would do something like this:
gcc -o myprog sourcefile1.o sourcefile2.o -lLibrary1 -lLibrary2


If your libraries are not in the standard location (/usr/lib is the only default as far as I know) you would use the -L flag to specify where to look:
gcc -L/path/to/libraries -o myprog sourcefile1.o sourcefile2.o -lLibrary1 -lLibrary2


Creating a makefile is similar to just writing out which commands you would use to compile by hand (as above), a sample makefile would be:
CC=gccLIBS=-lLibrary1 -lLibrary2objects=sourcefile1.o sourcefile2.oall : myprogmyprog : $(objects)        $(CC) -o myprog $(objects) $(LIBS)

You would put that in a file called 'Makefile' then use the command 'make' to compile your program (and make will perform the individual steps).

edit: formatting
Also, you may want to do just what George2 said and use a fancy IDE, it could make things easier.
Thanks!

This topic is closed to new replies.

Advertisement