During compilation, you must first enter the directory where your c file is located. Otherwise, you cannot find the original file. You can run the ls command to view the files in the current directory.
I. Compile a single C file
Gcc text. c generates the. out file by default, that is, the default target code file.
Gcc-c text. c will generate the target file with the same name as the source file: text. o
Gcc text. c-o liu generates an executable file named liu or gcc-o liu text. c./liu to execute the program.
Ii. Use non-system default Class Libraries
For example, the multithreading class library pthread is used.
Gcc text. c-o liu-lpthread or gcc-o liu text. c-lpthread must be placed at the end, that is, add the class library you want to use after-l.
3. Compile multiple source files, that is, compile makefile or Makefile.
The advantage of makefile is to prevent source files from being repeatedly compiled.
For example, there are many source files, one of which can be modified one day. If you do not write makefile, you need to re-compile all the files in the project. This is very troublesome. With makefile, describes the dependency between each source file. When a source file changes, you only need to recompile the associated source file.
For example, the following files are available:
One. h
One. c
Two. h
Two. c
Main. c
The one. h and two. h header files are introduced in main. c, and one. c two. c implements the functions defined in the two header files respectively.
There are two ways to compile and run the program,
I. manual compilation
Gcc-c one. c
Gcc-c two. c
Gcc-c main. c
Gcc-o main. o one. o two. o
./Main
Ii. makefile
Write the following command in any text editor and save it as makefile.
Main: main. o one. o two. o
Tab key gcc (or cc)-o main. o one. o two. o
Main. o: main. c one. h two. h
Tab key gcc (or cc)-c main. c
One. o: one. c one. h
Tab key gcc (or cc)-c one. c
Two. o: two. c two. h
Tab key gcc (or cc)-c two. c
These source files must be in the same directory and in main. introduce one in c. h and two. use "one. h "" two. h "do not use <> otherwise, the compiler will go to the system class library file and cannot find it.
Then input the make command in the terminal of the same directory, and the compilation starts. If there is no error, execute./main to run your program.
You can also streamline makefile:
There are three symbols:
1 $ @ indicates the target file
2 $ <indicates the first file
3 $ ^ indicates all dependent files
Therefore, the preceding command can be simplified:
Main: main. o one. o two. o
Tab key gcc (or cc)-o $ @ $ ^
Main. o: main. c one. h two. h
Tab key gcc (or cc)-c $ <
One. o: one. c one. h
Tab key gcc (or cc)-c $ <
Two. o: two. c two. h
Tab key gcc (or cc)-c $ <
The makefile can also be simplified:
A new symbol:. c. o
This symbol automatically searches for the associated source file and header file
Therefore, makefile can also be written as follows:
Main: main. o one. o two. o
Tab key gcc (or cc)-o $ @ $ ^
. C. o:
Tab key gcc (or cc) $ <
It is best to add the clean command to delete the generated target file and execution file. It complies with the elegant and concise principle to facilitate re-compilation.
Clean:
AB key gcc (or cc) rm main. o one. o two. o
Remember the format of makefile. As for the question, you can only say that this is a standard. makefile has many advanced usage and will be learned later.