c++ 在客户端的GCC使用
c++ 在客户端的GCC使用
翻译自:
GCC是GNU工具箱的一个功能,GNU还包括如下功能:
- GNU compiler collection (GCC)
- GNU make
- GNU Debugger (GDB)
- ....
GCC目前在所有的linux系统都支持
本文在osx系统实现,linux类似, windows需要(MinGW or Cygwin GCC)
查看gcc版本
gcc --version
gcc --help
编译c语言
gcc hello.c
设置输出文件名为hello
gcc -o hello hello.c
编译c++
g++ -o hello hello.cpp
g++ -wall -g -o Hello Hello.cpp
-o 说明输出文件名
-Wall 输出所有warning
-g: 生成附带的debug 信息为gdb debugger用
分离compile和link
g++ -c -Wall -g Hello.cpp
g++ -g -o Hello Hello.o
-c compile产生.o文件, -g利用Hello.o输出应用
compile并link多个源文件
g++ -o myprog.exe file1.cpp file2.cpp
更常见的方式
g++ -c file1.cpp
g++ -c file2.cpp
g++ -o myprog.exe file1.o file2.o
compile into a shared Library
为了把C/C++文件编译到共享库中(win中是.dll,unix是.so),使用-shared选项
GCC编译流程
预处理
cpp hello.c > hello.i
编译The compiler compiles the pre-processed source code into assembly code for a specific processor.
gcc -S hello.i
The -S option specifies to produce assembly code, instead of object code.
The resultant assembly file is "hello.s".
Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o"
as -o hello.o hello.s
Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file "hello.exe".
ld -o hello.exe hello.o ...libraries...
Headers (.h), Static Libraries (.lib, .a) and Shared Library (.dll, .so)
- A static library has file extension of ".a" (archive file) in Unixes or ".lib" (library) in Windows。the machine code of external functions used in your program is copied into the executable.
- A shared library has file extension of ".so" (shared objects) in Unixes or ".dll" (dynamic link library) in Windows. When your program is linked against a shared library, only a small table is created in the executable.most operating systems allows one copy of a shared library in memory to be used by all running programs, thus, saving memory
Searching for Header Files and Libraries (-I, -L and -l)
When compiling the program, the compiler needs the header files to compile the source codes; the linker needs the libraries to resolve external references from other object files or libraries. The compiler and linker will not find the headers/libraries unless you set the appropriate options
For each of the headers used in your source (via #include directives), the compiler searches the so-called include-paths for these headers. The include-paths are specified via -Idir option (or environment variable CPATH).
Try list the default include-paths in your system used by the "GNU C Preprocessor" via
cpp -v
Try running the compilation in verbose mode (-v) to study the library-paths (-L) and libraries (-l) used in your system:
gcc -v -o hello.exe hello.c
GCC Environment Variables
1.PATH: For searching the executables and run-time shared libraries (.dll, .so).
2.CPATH: For searching the include-paths for headers. It is searched after paths specified in -I
3.LIBRARY_PATH: For searching library-paths for link libraries. It is searched after paths specified in -L
make
Running make without argument starts the target "all" in the makefile. A makefile consists of a set of rules. A rule consists of 3 parts: a target, a list of pre-requisites and a command, as follows:
target: pre-req-1 pre-req-2 ...
command
Use "tab" to indent the command (NOT spaces).
Comment & Continuation
A comment begins with a # and lasts till the end of the line. Long line can be broken and continued in several lines via a back-slash ().
target1 [target2 ...]: [pre-req-1 pre-req-2 ...]
[command1
command2
......]
Phony Targets
A target that does not represent a file is called a phony target. For example, the "clean" in the above example, which is just a label for a command. The standard phony targets are: all, clean, install
Variables
variable begins with a $ and is enclosed within parentheses (...) or braces {...}.
Automatic Variables
Automatic variables are set by make after a rule is matched.
\(@: the target filename. \)*: the target filename without the file extension.
$<: the first prerequisite filename....
Virtual Path - VPATH & vpath
You can use VPATH (uppercase) to specify the directory to search for dependencies and target files. For example,
Search for dependencies and targets from "src" and "include" directories
The directories are separated by space
VPATH = src include
You can also use vpath (lowercase) to be more precise about the file type and its search directory. For example,
vpath %.c src
vpath %.h include