CMakeLists.txt的写法

参考:http://blog.csdn.net/cust_hf/article/category/345853

CMakeListserv.txt的写法

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

Demo1:

add_executable(hello main.cpp)

    这将从main.cpp源码文件创建一个叫“hello”(Windows下叫“hello.exe”)的可执行文件。你可以根据自己的需要将C和C++文件混合。在同一个CMakeLists.txt可以有多个可执行文件和库。同一个源码文件可以用于不同的目的,源码可以从其他目标中为每个目的独立的编译。 

Demo2:

add_executable(demo main.cpp main.h main.rc)

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

 

(2):调试CMakeLists.txt的办法
这个是调试CMakeLists.txt的一个手段啦。不得不学习哦。 

演示1如下:
MESSAGE("俺们正在生成项目文件")
会显示一个警告框。

演示2如下:
MESSAGE(STATUS "俺们正在创建项目文件")
遇到这条指令,会把文字显示在状态栏里面(一闪而过,不容易发现)。

演示3如下:
MESSAGE(FATAL_ERROR "严重错误,俺们搞不定啦")
这条指令会提示出错,并退出。

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

(4):使用变量 .
SET( MY_SOURCES main.cpp widget.cpp)
MESSAGE(STATUS "my sources: ${MY_SOURCES}")使用SET()命令来为变量设置值。如果你列出了一个以上的字符串,变量将是串列表。列表是一列由分号隔开的字符串。如果只设置个一项,那么这项只有一个值。可以通过${VAR}获得变量的值。可以使用FOREACH()来迭代一份列表:

FOREACH(next_ITEM ${MY_SOURCES})
   MESSAGE(STATUS "next item: ${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                        <debug | optimized> library2                        ...)Specify a list of libraries to be linked into the specified target. The debug and optimized strings may be used to indicate that the next library listed is to be used only for that specific type of build

为所有目标统一指定需要的库文件(指定所有目标都用的库文件)
LINK_LIBRARIES: Link libraries to all targets added later.


  LINK_LIBRARIES(library1 <debug | optimized> library2 ...)This is an old CMake command for linking libraries. Use TARGET_LINK_LIBRARIES unless you have a good reason for every target to link to the same set of libraries.


Specify a list of libraries to be linked into any following targets (typically added with the ADD_EXECUTABLE or ADD_LIBRARY calls). This command is passed down to all subdirectories. The debug and optimized strings may be used to indicate that the next library listed is to be used only for that specific type of build.

(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.

 

 

  • 5
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: CMake是一个跨平台的构建工具,用于帮助开发者生成各种不同编译工具的构建脚本。在编写CMakeLists.txt文件时,可以使用多个源文件进行编译。 首先,在CMakeLists.txt文件中使用`add_executable`或`add_library`命令来指定生成的可执行文件或库文件的名称,并将其与源文件关联起来。 例如,如果我们有两个源文件main.cpp和helper.cpp,我们可以这样写: ```cmake cmake_minimum_required(VERSION 3.12) project(MyProject) add_executable(MyExecutable main.cpp helper.cpp) ``` 这个例子中,我们使用`add_executable`命令来生成一个名为MyExecutable的可执行文件,并将main.cpp和helper.cpp这两个源文件与之关联。 如果我们的项目中有更多的源文件,我们可以继续在`add_executable`或`add_library`命令后面继续添加源文件的名称。 ```cmake add_executable(MyExecutable main.cpp helper.cpp file1.cpp file2.cpp) ``` 当我们构建项目时,CMake会自动将这些源文件编译成目标文件,并链接到生成的可执行文件或库文件中。 此外,如果项目中有多个文件夹,并且每个文件夹中都有一些源文件,可以使用`add_subdirectory`命令将这些文件夹添加到构建过程中。 ```cmake add_subdirectory(folder1) add_subdirectory(folder2) ``` 这样可以让CMake在构建过程中进入这些文件夹并执行相应的CMakeLists.txt文件。 总结起来,CMakeLists.txt文件中的多文件编译可以通过`add_executable`或`add_library`命令与源文件进行关联,以及使用`add_subdirectory`命令将多个文件夹添加到构建过程中。 ### 回答2: cmakelists.txt是用于配置CMake编译和构建项目的脚本文件,它使用简单的语法来描述项目的文件结构和依赖关系。如果需要编译多个源文件,可以按照以下步骤编写CMakeLists.txt: 1. 设置项目名称和最低CMake版本: ```cmake cmake_minimum_required(VERSION 3.10) project(MyProject) ``` 2. 添加源文件到项目中,可以使用SET命令将需要编译的源文件路径存储在变量中: ```cmake set(SOURCES src/main.cpp src/other.cpp) ``` 3. 添加可执行文件目标,并将源文件与目标进行关联: ```cmake add_executable(MyExecutable ${SOURCES}) ``` 4. 如果需要链接外部库,可以使用target_link_libraries命令来指定链接的库: ```cmake target_link_libraries(MyExecutable MyLibrary) ``` 完整示例CMakeLists.txt文件: ```cmake cmake_minimum_required(VERSION 3.10) project(MyProject) set(SOURCES src/main.cpp src/other.cpp) add_executable(MyExecutable ${SOURCES}) target_link_libraries(MyExecutable MyLibrary) ``` 以上是最基本的多文件编译的CMakeLists.txt的编写方式,根据实际项目需求,还可以添加更多的配置选项和设置,例如编译选项、包含路径、链接库等。具体的写法会因项目而异。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值