delorie.com/djgpp/doc/ug/compiling/gcc.html   search  
Guide: GCC invocation

The program that does most of the work in building your program is gcc. It does all the work of taking your sources and making them into working programs. It uses a number of other programs, like cpp, cc1, as, and ld that you will probably never need to call directly.

To start you off, here's an example of how to turn a simple C source file into a working executable:

gcc hello.c -o hello.exe

You tell gcc what to do by providing it command line options, which are usually single letters preceeded by a dash (as in "gcc -c hello.c"). This is different from most DOS programs, which use a slash (like "DIR /W/P"). On the other hand, since dashes are used for options, you can use either slash when typing file names (like "gcc -c foo/bar.c").

Normally, gcc figures out what kind of file you're starting with my examining the file name's extension. Remember that gcc is case sensitive, so hello.c is a C program source, but HELLO.C is a C++ program source. Also, options are case sensitive, so -O means "optimize" but -o means "specify output file".

By default, gcc assumes that you want to create an executable program called a.exe. Here are the common options that you'll use to change what gcc does.

To change where the output file goes, use the -o option, like "gcc hello.c -o hello.exe". You should be careful when using this switch with other switches, because it does not change the type of file created, just its name. For example, if you typed "gcc -c hello.c -o hello.exe", you'll get an object file (that's what -c means) with the name hello.exe (that's what -o means).

If you want gcc to just compile your source, and not link it into a complete program (you'll do this if you have more than one source that makes up your program), use the -c option, like "gcc -c hello.c". This will tell gcc to stop just before the linker stage. By default, the name of the output file is the same as the source file name, but it ends in .o to indicate that it's an object file.

If you add the -v option, like "gcc -v -c hello.c", gcc will print additional messages that tell you its version numbers, where it's looking for files, and the various programs that it's running. It's neat to do this once, just to see what it does, and you'll be asked to do this if you request help on the news group.

The -l options are used to add libraries to your program. Note that order is important! You should always list libraries after all your objects. If you don't, gcc might not realize that it needs them until it's too late. The -l option is a shortcut for linking a library, because you can always just list the library on the command line (like "gcc hello.o mylib.a"). The benefit of the -l option is that you don't need to know where the library is. This is used for the standard libraries, and in fact, gcc will automatically add -lc to whatever you type in order to get the standard C library (libc.a). Note that with the -l option, you only need to specify part of the library name. gcc assumes that the library name starts with "lib" and ends in ".a", so -lfoo would refer to libfoo.a and would be found wherever the standard libraries are kept.

The -O option (upper case) means that gcc should try to optimize your program, to make it run faster. This will take longer but your program will run faster. You can specify a digit after the O, like -O2, to specify a specific level of optimization. The gcc manual describes what the various levels mean.

The -g option tells gcc to keep track of additional information about your program to help you debug it later. You should always start out using this option, and take it out only after you've worked out the bugs in your program.

For a more detailed reference to the options available to gcc, please refer to the gcc manual.


  webmaster     delorie software   privacy  
  Copyright © 1997     Updated Apr 1997