编译过程,make,makefile,cmake,qmake,cmakelist总结

来源:互联网 发布:仿手机淘宝 html5 编辑:程序博客网 时间:2024/05/01 22:57

一、关于C语言的编译过程

预编译编译链接

1.-E预编译

gcc –E 参数代表预编译
gcc -o a1.c -E a.c
预编译a.c,预编译之后的文件名叫a1.c
预编译功能之一:将#include包含的头文件做简单的文本替换
在C语言中#开头的语句又叫预编译指令
预编译功能之二:将代码中的注释删除

2.-c编译

编译的作用是把文本的c语言编译为二进制的指令
gcc -o a.o -c a1.c
编译a11.c,编译后的文件名叫a.o

3.链接

C语言写的程序是需要依赖各种库的,所以编译之后还需要把库链接到最终的可执行程序中去
gcc没有单独的链接参数
gcc -o a a.o
链接a.o,最后生成的文件叫a

二、关于make

make是一个自动化编译工具,你可以使用一条命令实现完全编译。但是你需要编写makefile,make命令会去找到makefile文件并执行。

三、关于makefile

1. 一个规则    目标:依赖       命令2. 两个函数    查找文件:src = $(wildcard ./*.c)     匹配替换:obj = $(patsubst %.c, %.o, $(src))3. 三个自动变量    $@: 目标    $^: 全部的依赖    $<: 依赖中的第一个

三、关于cmake

CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make package)、生成源码包(make package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。如果有嵌套目录,子目录下可以有自己的CMakeLists.txt。

四、关于qmake

qmake能够自动生成Makefile、Microsoft Visual Studio 专案文件和 xcode 专案文件。不管源代码是否是用Qt写的,都能使用qmake,因此qmake能用于很多软件的构建过程。他会根据.pro文件的配置情况来生长Makefile。

五、关于cmakelist.txt(转载自http://blog.csdn.net/gxuan/article/details/7701035)

CMakeListserv.txt的写法

1.要求CMake根据指定的源文件生成可执行文件

add_executable(hello main.cpp)
add_executable(demo main.cpp main.h main.rc)这奖使用main.cpp源文件,main.h文件,main.rc文件构造可执行文件。至于如何使用这些文件,CMake比我们都清楚。

2.调试CMakeLists.txt的办法

MESSAGE(“俺们正在生成项目文件”):会显示一个警告框。
MESSAGE(STATUS “俺们正在创建项目文件”):遇到这条指令,会把文字显示在状态栏里面(一闪而过,不容易发现)。
MESSAGE(FATAL_ERROR “严重错误,俺们搞不定啦”):这条指令会提示出错,并退出。

3.使用标准模块

cmake提供了很多标准模块,扩展名都是txt我们可以直接包含进来。就像使用C语言的#include指令一般。比如:
INCLUDE(FindBoost)
一句话,就告诉了CMake“我们的程序需要Boost”。

4.使用变量 .

SET( MY_SOURCES main.cpp widget.cpp)
MESSAGE(STATUS “my sources: MYSOURCES")使SET(){VAR}获得变量的值。可以使用FOREACH()来迭代一份列表:
FOREACH(next_ITEM MYSOURCES)MESSAGE(STATUSnextitem:{next_ITEM}”)
ENDFOREACH(next_ITEM ${MY_SOURCES})
CMake中的命令是大小写无关的。变量名和参数名是大小写相关的。

5.测试平台相关信息 .

办法一(这个代码没有检验过哦)
IF (UNIX)
MESSAGE(“这个是UNIX操作系统”)
ENDIF (UNIX)

IF (MSVC)
MESSAGE(“这个需要做VC的项目文件”)
ENDIF (MSVC)
办法二(这个测试过)
IF ({CMAKE_SYSTEM_NAME} STREQUAL “Windows”)  
 SET(option WIN32)  
 SET(win32_LIBRARIES comctl32.lib shlwapi.lib shell32.lib odbc32.lib odbccp32.lib  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib)  
 #SET(defs -DUNICODE -D_UNICODE)  
ENDIF (
{CMAKE_SYSTEM_NAME} STREQUAL “Windows”)

6.要求CMake根据指定的源文件生成库文件 .

ADD_LIBRARY: Add a library to the project using the specified source files.

• ADD_LIBRARY(libname [SHARED | STATIC | MODULE] [EXCLUDE_FROM_ALL] source1 source2 … sourceN)Adds a library target. SHARED, STATIC or MODULE keywords are used to set the library type. If the keyword MODULE appears, the library type is set to MH_BUNDLE on systems which use dyld. On systems without dyld, MODULE is treated like SHARED. If no keywords appear as the second argument, the type defaults to the current value of BUILD_SHARED_LIBS. If this variable is not set, the type defaults to STATIC.

If EXCLUDE_FROM_ALL is given the target will not be built by default. It will be built only if the user explicitly builds the target or another target that requires the target depends on it.

7.添加查找头文件的路径

INCLUDE_DIRECTORIES: Add include directories to the build.

INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 …)Add the given directories to those searched by the compiler for include files. By default the directories are appended onto the current list of directories. This default behavior can be changed by setting CMAKE_INCLUDE_DIRECTORIES_BEFORE to ON. By using BEFORE or AFTER you can select between appending and prepending, independent from the default. If the SYSTEM option is given the compiler will be told that the directories are meant as system include directories on some platforms.

8.添加库文件的搜索路径 .

分类: CMake相关 2007-11-04 00:50 421人阅读 评论(0) 收藏 举报
LINK_DIRECTORIES: Specify directories in which to search for libraries.
LINK_DIRECTORIES(directory1 directory2 …)

9.显式指定链接时需要的库文件

分类: CMake相关 2007-11-04 00:54 491人阅读 评论(0) 收藏 举报
为每个目标分别指定需要链接的库文件(指定部分目标专用的库文件)
TARGET_LINK_LIBRARIES: Link a target to given libraries.
TARGET_LINK_LIBRARIES(target library1

10.显式实施宏定义 .

分类: CMake相关 2007-11-04 00:58 496人阅读 评论(0) 收藏 举报
用法演示一(文本宏):
ADD_DEFINITIONS(-DDEBUG)
用法演示二(常量宏)
ADD_DEFINITIONS(-DVERSION=1)
ADD_DEFINITIONS: Adds -D define flags to the command line of C and C++ compilers.
ADD_DEFINITIONS(-DFOO -DBAR …)Adds flags to command line of C and C++ compilers. This command can be used to add any flag to a compile line, but the -D flag is accepted most C/C++ compilers. Other flags may not be as portable.

阅读全文
0 0