Compiling normal C programs in Linux

Programming, for all ages and all languages.
Post Reply
OSMAN

Compiling normal C programs in Linux

Post by OSMAN »

Helloween!

What is the easiest way to compile a normal C-program linked with linux-libraries?

If I look at the gcc manual I see thousands of unnecessary options.
So, would you tell me a little about some necessary options like
-c, -o, -f and so on which do you think might help?

To this moment I've needed to specify the library paths in my makefile; wouldn't that have to be automatic feature of Gcc when I use #includes in C?

Btw, can gcc output ms-exe or com, and if can, how?
AR

Re:Compiling normal C programs in Linux

Post by AR »

MinGW and Cygwin produce Windows executables.

Building standard apps in Linux is extremely simple (you should have easily been able to find a tutorial on this):

Code: Select all

gcc mysource.c -o myapp
./myapp
Standard libraries (libgcc, libc, libstdc++) should be automatically linked but you will need to specify others manually using the library option (ie. -l<Name>)

A more complex build involving multiple C files is:

Code: Select all

gcc -c mysrc1.c -o mysrc1.o
gcc -c mysrc2.c -o mysrc2.o
gcc mysrc1.o mysrc2.o -o myapp
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Compiling normal C programs in Linux

Post by Candy »

OSMAN wrote: What is the easiest way to compile a normal C-program linked with linux-libraries?

If I look at the gcc manual I see thousands of unnecessary options.
There are no options that are unnecessary. You might not need them, but that doesn't mean that I don't need them, or that you won't need them in the future.
Btw, can gcc output ms-exe or com, and if can, how?
By enabling the linker to output that kind of file. You can enable that in its configuration. LD is contained in binutils.
AGI1122

Re:Compiling normal C programs in Linux

Post by AGI1122 »

If there is a makefile included then it's even easier. Usually just type "make" and it compiles for you.

There is also some programs that include a configuration generator that creates the stuff that the makefile uses.

For instance to compile sarien for linux I type this:

Code: Select all

./configure
Then this:

Code: Select all

make
Of course you have to have both gcc and make installed for this.

Cross platform takes a bit more work... I had to install some mingw32 stuff and I usually have to modify the makefile to make it use mingw32, but then I can compile windows executables from linux.
AR

Re:Compiling normal C programs in Linux

Post by AR »

"configure" is a shell script created by "autoconf" from a configuration file that you still have to write to begin with (using "m4").
Post Reply