GCC Fast Manual


© Zhou Qingqing 2001
http://www.cs.toronto.edu/~zhouqq

Last modified: 2001-09-22



Read First (1 minute)

  1. Q: Who can benefit from this?

  2. A: I have written a Prolog Fast Manual. For the same sake, I write this GCC fast manual. I assume you have known C/C++.

  3. Q: Why do I write this ?

  4. A: GCC is an excellent & big compiler. I just hope this can provide people the most common commands of GCC as a memo.

  5. Q: Is this esp. for some OS?

  6. A: I guess no, because here is very basic commands of gcc.

Let's begin (15 minute)

  1. Basic format of gcc command

  2. gcc [options] [filenames]
    
    There are four stages while generating the executable code, i.e., pre-processing, compile, assemble and link. Different option will affect the different stage.

  3. To compile one C file with main() function

  4. If your system administrator has setup the path (commonly he has done), then you can run gcc from any place.
    gcc main_file_name.c 
    
    You will get "a.out" in the same path. That is, "a.out" is the executable file. If the a.out has no executable priority, you should set it with "chmod +x a.out" or use "umask" command.
    If you want to change the output filename, just use option "-o [output_filename]":
    gcc -o main_file main_file_name.c 
    

  5. To compile several C files

  6. gcc -c file1.c
    
    gcc -c file2.c
    
    gcc -c main_file_name.c
    
    gcc -o main_file main_file_name.o file1.o file2.o
    
    
    That is, use "-c" tell the compiler to generate the object file only, no link.

  7. To optimize program

  8. gcc -O file.c
    
    This would optimize the process of compile and link.

  9. To specify the header files directory

  10. gcc -I dir_name 
    
    This will add "dir_name" to your header file directory.

  11. To specify the lib directory

  12. gcc -L dir_name 
    
    This will add "dir_name" to your lib file directory.

  13. To write a makefile

  14. Use makefile will make your management of source files easy. An example makefile is:
    $>cat makefile
    
    
    
    # Use macro - any place with $(MACRO) will be replaced
    
    COMPILE= gcc -c
    
    LINK= gcc -o test
    
    
    
    # Don't forget to add a tab before each command line
    
    test: test_main.o test1.o
    
    	$(LINK) test_main.o test1.o
    
    
    
    test_main.o: test_main.c inc.h
    
    	$(COMPILE) test_main.c
    
    
    
    test1.o: test1.c inc.h
    
    	$(COMPILE) test1.c
    
    # Do cleaning 
    
    clean: 
    
    	rm *.o
    
    
    
    
    
    That is, output file "test" relies on object file "test_main.o" and "test1.o". "test_main.o" relies on file "test_main.c" and "inc.h"; "test1.o" relies on file "test1.c" and "inc.h". If you make any changes on file "test_main.c", then gcc will re-compile(link) "test_main.o" and "test"; if you make changes on "inc.h", all three files will be re-compiled(link). Just key in "make" after the command prompt will make GCC use the default "makefile" to build your project. Key in "make clean" to clean all the object files in the directory.

  15. To handle the compile/link error

  16. GCC warning/error format is:
    [file_name]:[line_number]: [error_message]
    
    test.c:5: parse error before `printf'
    
    test.c:4: warning: return type of `main' is not `int'
    
    If you are familiar with C/C++, it is the same method for you to correct the errors in gcc.

  17. To debug

  18. To enable gcc generate the binary code support debug. Just add option "-g":
    gcc -g -o main_file_name main_file_name.c
    
    There are several tools in unix supporting gcc debug, "dbx", "gdb", to name a few. More about debug please refer to their homepage. Be sure don't use "-g" together with "-O", since the optimizer often puts compiled code in a different order from the source code. So execution will not follow the source code order, which leads to confusion.


Referenced links


Back