Introduction to using GCC under Linux
Before leaving the payment system done a sum of #, continue to improve my C server.
I thought about implementing the CGI protocol next, but the implementation was stuck with a problem:
C-Process interaction data type problem with PHP process:
In the C process, I intend to store the server processing request data in a structure, and then pass the information in this structure to PHP, and PHP process will have a global array corresponding to it, but all the week, the structure is the C process of memory data, is not directly to the use of PHP.
At this point we also need a "protocol" to resolve the heterogeneity of process data types. The solution, of course, is simple enough to serialize the C struct, use one of the xml,json,protobuf (unused), and spend more time in implementing the process. Originally want to build a wheel, realize the JSON type of codec, feel some deviation from the theme, so consider using an open source library cJSON ;
But I did not have the C large-scale project development experience, writing is a small demo, gcc -o name source.c enough to solve the problem, there is no compilation of multiple files, the experience of organizing the project, download to the source after the face of a confused, the search compiled data are some of the more fragmented content, fragmentation system, But in his many attempts to finally successfully introduced Cjson into the project, here a little to summarize.
Around for a long time, finally came to the topic of this article: Project compilation, the main introduction of some GCC under the Linux project to compile links to the steps.
In addition, I just tested the scheme is feasible, has not been changed, the evaluation of the merits of the scheme is not enough, hope to have similar experience of the students to give some advice or something.
Compile step
Let's start with a general procedure for compiling a C source file:
Preprocessing (Preprocess): mainly in the code level of processing, including the introduction of the file, expand the macro definition, delete comments, add line numbers, etc., the resulting file to the .i end.
gcc -E test.c -o test.i
Compile (Compilation): Compile is in the code syntax level of processing, generate the corresponding assembly language code, generated .s as a suffix of the assembly language file;
gcc -S test.i -o test.s
Assembly (Assembly): Generates the executable machine code for the assembly language code, generating the .o target file as a suffix.
gcc -c test.s -o test.o
Link (linking): Connect each .o target file and resolve the library dependencies, generating a file that can be executed directly with no suffix.
gcc -o test test.o
If we use the following command directly, then the previous steps will also be executed automatically. As we often use, we gcc -o are actually doing all the steps at once.
Above the intermediate file, you can use the text viewing tool to view the contents to verify its functionality.
Static libraries and dynamic libraries
Library files are dynamic and static, and their naming specification is to lib库名.后缀 use the (blank space) option when linking to the target file and library, -l 库名 or -L /path to add a directory that specifies the precedence of the search library file.
For example: The dynamic library file name for the math library in C is the one math.h libm.so we need to add when compiling the connection file -lm . If you want to specify that the library file path is /usr/lib64/libm.so , you can add -L /usr/lib64 to specify that the library file first finds the directory.
In addition, static and dynamic library file search directory order is not the same, the following are described in detail:
Static Library
A static library file is typically a .a library file with the suffix, which adds the contents of the library file to the executable when the connection is compiled, and the static library file will no longer affect the executable after the connection is completed.
It has the advantage of being simple and rude, but if there is a change inside the library file, you need to re-compile all the executable files referencing this library file.
The general compilation steps are as follows:
gcc -c static.c -o static.o // 编译静态库文件的源文件 ar -r static.a static.o // 生成静态库文件 gcc -o main -lstatic // 连接静态库文件生成可执行文件
When you compile a connection, the static library file search Catalog order is:
- The directory specified by the-l parameter when compiling the connection;
- environment variable directory
LIBRARY_PATH ;
- Fixed catalogue
/lib、/usr/lib、/usr/local/lib , etc.;
Dynamic Library
The dynamic library file is usually .so terminated by adding only the files of the dynamic library to the executable when compiling the connection, and loading the library files only when the program is running. The advantages of this approach are very flexible, if the dynamic library file internal changes, then only important to recompile the library file.
Its general compilation steps are as follows:
gcc -c dynamic.c -fpic -o dynamic.o // 编译动态库文件的源文件 -fpic 表示编译为位置独立的代码,使之可以被放在可执行文件内存中的任何地方 gcc -shared dynamic.o -o dynamic.so // 生成动态库文件 gcc -o main -L . -ldynamic // 连接当前文件夹下的动态库文件
When you compile a connection, the dynamic library file search Catalog order is:
- The-l parameter specifies the directory when compiling the connection;
- environment variable directory
LD_LIBRARY_PATH ;
/etc/ld.so.confdirectories configured in the configuration file
- Fixed directory
/lib、/usr/lib and so on.
Cmakelists
Writing here is not the end, we have to consider if the file is very much what to do, do you want to enter n more than one source file name each time? If the software is finished, users do not want to remember these complex commands and files when they are in use.
Automation is the goal, we consider using the automated compilation tool CMake, then we will write a compilation configuration file cmakelists appropriate for the project file.
Cmakelists is a TXT file, which is like a compilation guide for a project and is for use with cmake tools. Its syntax is similar to shell, but there are many functions built in, here we introduce a few simple syntax, write a simple CMakeLists.txt .
Current file structure:
|__ CMakeLists.txt |__ test.c |__ cJSON.c |__ include | |__ cJSON.h |__ lib
The following is a compilation of a dynamic library, cmakelist the explanation in a comment.
PROJECT(test) # 项目名称 cmake_minimum_required(VERSION 2.8) # 选择一个cmake版本 SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) # 设定产生库的目录 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) # 设定产生的可执行文件的目录 ADD_EXECUTABLE(test test.c) # 这里要先声明产生的可执行文件,以便后面连接 SET(cJSON cJSON.c) # 设置文件变量 ADD_LIBRARY(cJSON SHARED ${cJSON}) # 此语句用文件变量生成一个动态链接库 TARGET_LINK_LIBRARIES(test cJSON) # 连接可执行文件与动态链接库 FIND_LIBRARY(MATH_LIB libm.so /usr/lib64) # 在/usr/lib64文件夹下找libm.so(cJSON需要) IF(MATH_LIB) TARGET_LINK_LIBRARIES(test ${MATH_LIB}) # 找到之后连接上 ENDIF() MESSAGE("cmake complete, use make to compile!") # 在命令行输出提示语句
After one hours, I finally wrote out a cmakelists file that I could use. Run cmake . && make the build of the completed project.
The directory structure at this time is (skipping the temporary files generated by CMake):
|__ CMakeLists.txt |__ test.c |__ cJSON.c |__ include | |__ cJSON.h |__ lib | |__ libcJSON.so |__ bin |__ test
Summary
This article seriously shows that the light will write code no eggs, the environment of the building/library of the use of the necessary skills, after all, can not be built on every wheel.
If you are also a C novice, this article can give you a general understanding of the compilation steps, and so on, not as I started the same confused. If you want to learn more, the key words of the article and the following reference document can also help.
If you feel that this article is helpful to you, you can click on the recommendation below to support me. Blog has been updated, welcome attention .
Reference documents (carefully selected):
GCC work process and dynamic library static library links
Linux Dynamic library File search path
CMake use examples and collation summary
Category: C/
Using GCC under Linux