© Zhou Qingqing 2001
http://www.cs.toronto.edu/~zhouqq
Last modified: 2001-09-22
Read First (1 minute)
Let's begin (15 minute)
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.
gcc main_file_name.cYou 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.
gcc -o main_file main_file_name.c
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.oThat is, use "-c" tell the compiler to generate the object file only, no link.
gcc -O file.cThis would optimize the process of compile and link.
gcc -I dir_nameThis will add "dir_name" to your header file directory.
gcc -L dir_nameThis will add "dir_name" to your lib file directory.
$>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 *.oThat 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.
[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.
gcc -g -o main_file_name main_file_name.cThere 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