Article Directory
GCC is used in linux compiler, Clang is used on mac compiler both command is almost the same as
paper operating environment for mac
Compiler directives
gcc/clang -g -O2 -o test test.c -I... -L... -l...
-g debugging information in the output file
-O capital O, to optimize the output file do command, 2 for the file compiler optimization level of the second, more thorough, if the debugger, then you do not need -O
-o output file
-I specifies the location of the header files, use third-party libraries or create your own,
-L specifies the library location lib
-l means link, specify which libraries are not necessarily all use
Compilation
vim main.c
create a main function file, c program must be the main function as the entry point
# include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
Pretreatment
gcc -E main.c -o main.i
-E represents a pre-generated files
specific contents:
- Macro definition instruction #define xxxx
- Conditional compilation #if #else # endif ...
- #Include header file contains instructions to copy the source code and header files together
- Special symbol processing parameters, for example, a method of processing spaces
FAQ:
can not define global variables in the header file, because the definition of global variables will make all files that contain the header files of the existence of the sections of the code, which means that the same file will define a global variable in the header file, which will inevitably cause conflict
First, preprocessing the source file, the process is a process mainly definitions # number of commands or statements (such as macros, the include #, pre-compiled instructions #ifdef etc.), generating * .i file. I.e., header files and macros binding determination and other operations, to copy the source code and header files together, so that the compiler to compile
Compile
gcc -S main.i -o main.s
-S let the compiler to generate assembly language output
Details:
compiling source code segment refers to the syntax analysis and optimization process to produce assembly code by lexical analysis and syntax analysis, which is translated into an equivalent intermediate code or assembly code. Local optimization, control flow analysis and loop optimization, and global optimization analysis data stream.
Lexical analysis, parsing and semantic analysis, generate * .s assembly files
compilation
gcc -c main.s -o main.o
-c tells the compiler, assembler specified source file (that is, compile the source file), but do not link
Corresponding assembly instructions translated into machine instructions to generate a binary relocatable object file .o
link
vi add.c add method to create a file
#ifndef __MY_LIBRARY__
#define __MY_LIBRARY__
int add(int a, int b){
return (a+b);
}
#endif
Then compile and use libtool, generate a static library contains add.o
gensees-iMac:study gensee$ vi add.c
gensees-iMac:study gensee$ clang -g -c add.c
gensees-iMac:study gensee$ ls add.o
add.o
gensees-iMac:study gensee$ ./add.o
gensees-iMac:study gensee$ libtool -static -o libmylib.a add.o
Generate a lib file
Ex: Use this command to view ar composed of static libraries
gensees-iMac:study gensee$ ar -t /Users/gensee/Desktop/study/libmylib.a
__.SYMDEF SORTED
add.o
ar is linux command to view more https://www.runoob.com/linux/linux-comm-ar.html
Create a main function file main.c, and compiled to .o
#include <stdio.h>
#include "add.h"
int main()
{
int sum = add(1,5);
printf("%d\n",sum);
return 0;
}
clang -g -c main.c
Here, too, we need to create a header file to reference add.h
use vi add.h
#ifndef __MY_LIBRARY__
#define __MY_LIBRARY__
int add(int a, int b);
#endif
Then we link
clang -o main main.o -L . -lmylib
gensees-iMac:study gensee$ clang -o main main.o -L . -lmylib
gensees-iMac:study gensee$ ./main
6
gensees-iMac:study gensee$
We can see the end result of the implementation of the add method 6
Step execution method
The premise is compiled mylib
clang -g -o main mian.c -I . -L . -lmylib
-I. Represents the current directory headers
-L. Represents the current directory lib
gensees-iMac:study gensee$ clang -g -o main main.c -I . -L . -lmylib
gensees-iMac:study gensee$ ./main
6
gensees-iMac:study gensee$
Link mode
Link system libraries, library tripartite, divided into dynamic and static link link
Statically linked and dynamically linked biggest difference between the two is that the timing is not the same link, the link is still before the executable form, and is dynamically linked during program execution.
Static links: source files used in the compilation of library functions and the resulting object files .o merge to produce an executable file. Each has a copy of the executable program, a waste of space for all the required target files, and updates need to be recompiled form an executable program.
Dynamic Link: Solving the static linking waste of space, updated difficult issues, only need to be loaded once, stored in memory, each program can use.
Instruction View URL: http://c.biancheng.net/view/2375.html