make CMake the ins and outs

In theory, any of a C ++ program can use g ++ to compile.

As we all know, the writing program roughly as follows:

1, to write source code editor, such as .c file.

2, a target file compiler generates compiled code, such as .o.

3, is connected with the object code executable file generated by the linker, such as .exe.

makefile

But if the source file too, will be a particularly troublesome when a compiler, so people thought, why not design a program similar batch to batch compile the source file it?

So there will make tools, which is an automated build tools , you can use a single command to achieve complete compilation.

But you need to write a rule file, make a batch basis for it to compile this file is makefile , so write the makefile is an essential skill for a programmer.

 

Unix, Make triple

Unix development process, regular operation is to install the appropriate library file compiled from the source code, so the following three commands is commonplace, commonly known as triple:

./configure

make

make install

Let's look at these three steps are done.

configure

Execute ./configure file is usually some preparation before the official compilation environment, ready to compile the required dependencies and so on. For example, the current detection system platform, the compiler needs to detect the existence of how to call, configure and generate the corresponding compile the required documents.

make

After the preparatory work is completed correctly, it is possible to make calls to perform the build. The actual execution of the task is to compile the Makefile to define the project from the source.

When the case is usually downloaded from the tar package may not contain a formal Makefile file, but another temporary files presented in the form of, for example Makefile.in, Makefile.am, Makefile.MSVC, etc., and then executed in accordance with the appropriate ./configure environment to generate the final Makefile file required.

make install

When the project is compiled normally, it will generate the corresponding output, executable file, so the file or DLL, or dynamic link o files. The last step is to copy the generated files to the appropriate system directory, this process is to install.

For example, to copy the executable file to the system PATH can take place to help copy the document to MANPATH, copy other files to the corresponding directory.

 

CMakelist.txt

For a big project, write makefile it is a complex thing, so people thought, why not design a tool, after reading all the source files, automatically generate makefile it?

So it was a cmake tool that can output a variety of makefile or project file, thus helping reduce the burden on the programmer.

However, the ensuing write is cmakelist file, which is cmake is based on the rules. So there is no short cut in the programming world, still down to earth.

 

 The basic structure of CMakeList.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#声明要求的cmake最低版本
cmake_minimun_required(VERSION 2.8)
#声明一个cmake工程,工程名为post_fusion
project(post_fusion)
# 添加c++ 11标准支持
set( CMAKE_CXX_FLAGS "-std=c++11" )
如果程序中使用了C++11标准,则需要设置告诉编译器,没有可以不用写。
#设置编译器编译模式:
set( CMAKE_BUILD_TYPE "Debug" )
对于编译用的Debug模式和调试用的Release模式,在Debug模式中,程序运行较慢,当可以在IDE中进行断点调试,而Release模式则速度较快,但没有调试信息。不设置默认是Debug模式。
# 添加引用的第三方头文件,例如添加Eigen头文件
include_directories( "/usr/include/eigen3" )
#编译生成库文件
add_library(irfusion comfunc.c post_sins_gnss.cpp)
#这条命令告诉cmake,我们想把这些源文件编译成一个叫作“irfusion”的库。在linux中,库文件分为静态库和动态库两种,静态库以.a作为后缀名,共享库以.so结尾。
#所有库都是一些函数打包后的集合,差别在于静态库每次被调用都会生成一个副本,而共享库则只有一个副本,更省空间。如果想生成共享库而不是静态库,只需要使用以下语句即可
add_library(irfusion_shared SHARED  comfunc.c post_sins_gnss.cpp)此时得到的文件就是irfusion_shared.so了。
#然后,在CMakeList.txt中添加一个可执行程序的生成命令,链接到刚才使用的库上:
add_executable(irfusion main.cpp)
target_link_libraries(irfusion irfusion_shared)

Guess you like

Origin www.cnblogs.com/MakeView660/p/11363334.html