CMake入門

CMake入門

CMake是一個跨平臺的安裝編譯工具,能夠用簡單的語句來描述全部平臺的安裝(編譯過程)。他可以輸出各類各樣的makefile或者project文件,能測試編譯器所支持的C++特性,相似UNIX下的automakec++

1、單個文件

main.ccbash

#include <stdio.h>
#include <stdlib.h>

double power(double base, int exponent)
{
    int result = base;
    int i;

    if (exponent == 0)
    {
        return 1;
    }

    for (i = 1; i < exponent; ++i)
    {
        result = result * base;
    }
    return result;
}

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }
    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);
    double result = power(base, exponent);
    printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

當前目錄下的CMakeLists.txt(CMakeLists 不區分大小寫)app

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 項目信息
project(Demo01)
# 指定生成目標
add_executable(Demo01 main.cc)

執行命令ide

cmake . # 執行cmake,自動生成對應的makefile
make # 生成二進制文件Demo01

編譯輸出工具

➜  cmakedemo cmake .
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/fanghao/Desktop/cmakedemo
➜  cmakedemo make
Scanning dependencies of target Demo01
[ 50%] Building CXX object CMakeFiles/Demo01.dir/main.cc.o
[100%] Linking CXX executable Demo01
[100%] Built target Demo01
➜  cmakedemo ./Demo01 5 4
5 ^ 4 is 625

2、多個文件

修改項目結構以下:測試

➜  cmakedemo tree
.
├── CMakeLists.txt
├── MathFunction.cc
├── MathFunctions.h
└── main.cc

0 directories, 4 files

CMakeLists.txt也應當添加多個文件ui

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 命令不區分大小寫
# 項目信息
project(Demo02)

#把源文件變成變量
aux_source_directory(. DIR_SRCS)

# 指定生成目標
add_executable(Demo02 ${DIR_SRCS})

3、子模塊

修改項目結構以下:code

➜  cmakedemo tree
.
├── CMakeLists.txt
├── main.cc
└── math
    ├── CMakeLists.txt
    ├── MathFunction.cc
    └── MathFunctions.h

1 directory, 5 files

cmakedemo目錄下的CMakeLists.txtorm

# 版本最低要求
cmake_minimum_required(VERSION 2.8)
# 命令不區分大小寫
# 項目信息
set(NAME Demo03)
project(${NAME})

# 添加math子目錄,會執行math目錄下的cmake
add_subdirectory(math)

# 指定生成目標
add_executable(${NAME} main.cc)

# 添加連接庫
target_link_libraries(${NAME} MathFunctions)

math目錄下的CMakeLists.txtci

# 查找當前目錄下的全部源文件
# 並將名稱保存到 DIR_LIB_SRCS 變量
aux_source_directory(. DIR_LIB_SRCS)
# 生成連接庫
add_library (MathFunctions ${DIR_LIB_SRCS})

此時的輸出,注意編譯了子模塊

➜  cmakedemo cmake .
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchai
ns/XcodeDefault.xctoolchain/usr/bin/cc-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchai
ns/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolch
ains/XcodeDefault.xctoolchain/usr/bin/c++-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolch
ains/XcodeDefault.xctoolchain/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: /Users/fanghao/Desktop/cmakedemo
➜  cmakedemo make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunction.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo03
[ 75%] Building CXX object CMakeFiles/Demo03.dir/main.cc.o
[100%] Linking CXX executable Demo03
[100%] Built target Demo03
➜  cmakedemo ./Demo03 3 4
3 ^ 4 is 81

生成的文件

➜  cmakedemo tree -L 2
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.11.3
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── Demo03.dir
│   ├── Makefile.cmake
│   ├── Makefile2
│   ├── TargetDirectories.txt
│   ├── cmake.check_cache
│   ├── feature_tests.bin
│   ├── feature_tests.c
│   ├── feature_tests.cxx
│   └── progress.marks
├── CMakeLists.txt
├── Demo03
├── Makefile
├── cmake_install.cmake
├── main.cc
└── math
    ├── CMakeFiles
    ├── CMakeLists.txt
    ├── Makefile
    ├── MathFunction.cc
    ├── MathFunctions.h
    ├── cmake_install.cmake
    └── libMathFunctions.a

6 directories, 22 files

能夠注意到生成了libMathFunctions.a

3、第三方庫

glewglfw爲例

cmake_minimum_required(VERSION 3.9)
project(helloworld)

set(CMAKE_CXX_STANDARD 11)

# 自動尋找庫
find_library(OPENGL opengl)

# 添加頭文件
set(GLEW_H /usr/local/Cellar/glew/2.1.0/include/GL)
set(GLFW_H /usr/local/Cellar/glfw/3.2.1/include/GLFW)
include_directories(${GLEW_H} ${GLFW_H})

# 添加目標連接
set(GLEW_LINK /usr/local/Cellar/glew/2.1.0/lib/libGLEW.2.1.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/3.2.1/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})

# 執行編譯命令
set(SOURCE_FILES main.cc)
add_executable(helloworld ${SOURCE_FILES})

4、小結

cmake做爲跨平臺的項目管理工具,相比make更加簡潔易用。

相關文章
相關標籤/搜索