在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:
1、编写 CmakeLists.txt。
2、执行命令“cmake PATH”或者“ccmake PATH”生成 Makefile ( PATH 是 CMakeLists.txt 所在的目录 )。
3、使用 make 命令进行编译。
1、单源文件目录
zjy@ubuntu:~/cmkTest$ ls
CMakeLists.txt main.cc
zjy@ubuntu:~/cmkTest$ cat main.cc
#include<iostream>
int main()
{
std::cout<<"hello"<<std::endl;
return 0;
}
zjy@ubuntu:~/cmkTest$ cat CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(main)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
zjy@ubuntu:~/cmkTest$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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/zjy/cmkTest
zjy@ubuntu:~/cmkTest$ ls
CMakeCache.txt cmake_install.cmake main.cc
CMakeFiles CMakeLists.txt Makefile
zjy@ubuntu:~/cmkTest$ make
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cc.o
[100%] Linking CXX executable main
[100%] Built target main
zjy@ubuntu:~/cmkTest$ ls
CMakeCache.txt cmake_install.cmake main Makefile
CMakeFiles CMakeLists.txt main.cc
zjy@ubuntu:~/cmkTest$ ./main
hello
zjy@ubuntu:~/cmkTest$
CMakeLists.txt:
关键字正确显示橙色,错误显示黑色。
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)——限定了最小版本是2.6
PROJECT(main)——设置生成执行文件的名字为main
AUX_SOURCE_DIRECTORY(. DIR_SRCS)——将源文件目录赋值给变量DIR_SRCS,原型是aux_source_directory(<dir> <variable>)
ADD_EXECUTABLE(main ${DIR_SRCS})——将变量 DIR_SRCS 中的源文件需要编译成一个名称为 main 的可执行文件
2、多源文件目录
zjy@ubuntu:~$ tree cmkTest/
cmkTest/
├── CMakeLists.txt
├── main
├── main.cc
├── CMakeLists.txt
├── print.cc
└── print.h
1 directory, 6 files
zjy@ubuntu:~$ cd cmkTest/
zjy@ubuntu:~/cmkTest$ ls
CMakeLists.txt main main.cc print
zjy@ubuntu:~/cmkTest$ cat CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(main)
add_subdirectory( print )
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
target_link_libraries( main print)
zjy@ubuntu:~/cmkTest$ cat print/CMakeLists.txt
AUX_SOURCE_DIRECTORY(. DIR_PRINT_SRCS)
add_library(print ${DIR_PRINT_SRCS})
zjy@ubuntu:~/cmkTest$ cat print/print.cc
#include<iostream>
#include"print.h"
void print()
{
std::cout<<"hello"<<std::endl;
}
zjy@ubuntu:~/cmkTest$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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/zjy/cmkTest
zjy@ubuntu:~/cmkTest$ make
Scanning dependencies of target print
[ 25%] Building CXX object print/CMakeFiles/print.dir/print.cc.o
[ 50%] Linking CXX static library libprint.a
[ 50%] Built target print
Scanning dependencies of target main
[ 75%] Building CXX object CMakeFiles/main.dir/main.cc.o
[100%] Linking CXX executable main
[100%] Built target main
zjy@ubuntu:~/cmkTest$ ls -lh
total 52K
-rw-rw-r-- 1 zjy zjy 12K Aug 2 16:49 CMakeCache.txt
drwxrwxr-x 5 zjy zjy 4.0K Aug 2 16:49 CMakeFiles
-rw-rw-r-- 1 zjy zjy 1.5K Aug 2 16:49 cmake_install.cmake
-rw-rw-r-- 1 zjy zjy 177 Aug 2 16:40 CMakeLists.txt
-rwxrwxr-x 1 zjy zjy 9.2K Aug 2 16:49 main
-rw-rw-r-- 1 zjy zjy 112 Aug 2 16:36 main.cc
-rw-rw-r-- 1 zjy zjy 5.0K Aug 2 16:49 Makefile
drwxrwxr-x 3 zjy zjy 4.0K Aug 2 16:49 print
zjy@ubuntu:~/cmkTest$ ./main
hello
hello
zjy@ubuntu:~/cmkTest$
/CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(main)
add_subdirectory( print )——增加一个子目录
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
target_link_libraries( main print)——main需要一个print链接库
/print/MakeLists.txt:
AUX_SOURCE_DIRECTORY(. DIR_PRINT_SRCS)
add_library(print ${DIR_PRINT_SRCS})——增加一个print链接库
在根目录下执行"cmake .",先解析根目录下的 MakeLists.txt,遇到add_subdirectory( print )后进入print目录解析/print/MakeLists.txt。
其他常用命令汇总:
include_directories(${PROJECT_SOURCE_DIR}/print) ——main目录与print目录在同一层,PROJECT_SOURCE_DIR代表根目录
target_link_libraries(main print) ——执行文件main链接print链接库
set(LIB_SRC hello.c) ——设置变量set(LIB_SRC hello.c hello2.c)
add_library(libhello ${LIB_SRC})
set_target_properties(libhello PROPERTIES OUTPUT_NAME "hello") ——文件是hello.c如果与外面名字有冲突,可以修改为libhello
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) ——设置执行文件输出目录
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) ——设置库文件输出目录