码迷,mamicode.com
首页 > 其他好文 > 详细

cmake的语法及使用用例

时间:2015-02-04 21:53:11      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

cmake是个跨平台编译工具,类似于autoconf。通过编译本地环境的makefile或者workplace来编译程序。

它的优点是简单高效。


语法:

CMakeList.txt文件是由注释、命令和空白字符组成。

注释是由 # 开始,到行结尾。

命令是由:命令名(空格分隔的参数)组成。

例如:command (args….)

args是以空格分隔的参数例表(如果参数中包含空格,则要加双引号)

除了用于分隔参数的空白字符(空格、换行号、tabs)都是被忽略不计的。任何包含在双引号中的字符都做为一个参数。一个反斜杠用于转换码。

命令名是大小写不敏感的。


详细的语法参考:

英文原文地址:http://www.cmake.org/cmake/help/syntax.html

翻译的地址:http://blog.csdn.net/kl222/article/details/7519340


相关命令介绍:

cmake_minimum_required(VERSION 3.0) #定义需要的最小版本
project(testhello)          #定义工程名字
set(PROJECT_ROOT_PATH "${CMAKE_SOURCE_DIR}/../") #设置变量, PROJECT_ROOT_PATH:工程目录得变量
SET(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin/") #设置可执行程序的输出目录
SET(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib/")  #设置库的输出目录
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/../include/") #设置头文件目录

LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/lib/") #设置库的连接目录
ADD_SUBDIRECTORY(src)  #增加cmake的子目录

ADD_LIBRARY(hello STATIC "${SOURCE_1}") #库的生成, STATIC是静态库, SHARED是动态库
FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/main/*.cpp") #设置文件变量,可支持简单正则
ADD_EXECUTABLE(main "${SOURCE_1}")  #可执行程序生成
TARGET_LINK_LIBRARIES(main hello)  #程序的库依赖

看一个复杂的例子(搞定这个一般的需求都可以搞定):

有三个文件:
1)hello.h
#include<iostream>
#include<stdio.h>


using namespace std;
void SayHello();
2)hello.cpp
#include "hello.h"

void SayHello()
{
        cout << "say hello !" << endl;
}
3)main.cpp
#include "hello.h"
int main()
{
        SayHello();
        return 0;

}
文件目录:
使用tree命令:
[root@dadfaf]# tree
.
|-- cmake
|   |-- bin
|   |   `-- main
|   |-- CMakeLists.txt
|   |-- lib
|   |   `-- libhello.a
|   `-- src
|       |-- CMakeLists.txt
|       |-- hello
|       |   `-- CMakeLists.txt
|       `-- main
|           |-- bin
|           `-- CMakeLists.txt
|-- hello
|   `-- hello.cpp
|-- include
|   `-- hello.h
|-- main
|   `-- main.cpp
`-- src
    |-- hello
    `-- main
先看cmake目录下的CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(testhello)
set(PROJECT_ROOT_PATH "${CMAKE_SOURCE_DIR}/../")
SET(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin/")
SET(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib/")
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/../include/")

LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/lib/")<pre name="code" class="plain">cmake_minimum_required(VERSION 2.6)
#FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/hello/hello.cpp")
set(SOURCE_1 "${PROJECT_ROOT_PATH}/hello/hello.cpp")
ADD_LIBRARY(hello STATIC "${SOURCE_1}")

ADD_SUBDIRECTORY(src)

再看看cmake/src目录下 的CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
ADD_SUBDIRECTORY(hello)
ADD_SUBDIRECTORY(main)
再看看cmake/src/hello目录下的CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
#FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/hello/hello.cpp")
set(SOURCE_1 "${PROJECT_ROOT_PATH}/hello/hello.cpp")
ADD_LIBRARY(hello STATIC "${SOURCE_1}")
最后看看cmake/src/main目录下的CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
FILE(GLOB SOURCE_1 "${PROJECT_ROOT_PATH}/main/*.cpp")
ADD_EXECUTABLE(main "${SOURCE_1}")
TARGET_LINK_LIBRARIES(main hello)

cmake文件写好了,开始编译:
cd cmake
cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/huhuang.hu/test/cmaketest/cmake

make
Scanning dependencies of target hello
[ 50%] Building CXX object src/hello/CMakeFiles/hello.dir/home/huhuang.hu/test/cmaketest/hello/hello.cpp.o
Linking CXX static library ../../lib/libhello.a
[ 50%] Built target hello
Scanning dependencies of target main
[100%] Building CXX object src/main/CMakeFiles/main.dir/home/huhuang.hu/test/cmaketest/main/main.cpp.o
Linking CXX executable ../../bin/main

执行程序:
./bin/main 
say hello !


cmake的语法及使用用例

标签:

原文地址:http://blog.csdn.net/hhh3h/article/details/43492771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!