CMake 使用慣例
- 在項(xiàng)目根目錄建一個(gè) build 目錄:
mkdir build && cd build
- 在 build 目錄下執(zhí)行
cmake ..
- 同樣在 build 目錄下决帖,執(zhí)行
make
注意上面的
cmake
和make
命令都是在 build 目錄下執(zhí)行,以保證生成的相關(guān)cmake
文件在 build 目錄下依疼。
Hello World 工程
新建一個(gè)項(xiàng)目根目錄 Hello-World愉阎。
1. 新建 Hello.cpp
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "hello cmake" << std::endl;
return 0;
}
2. 新建 CMakeLists.txt
add_executable(hello hello.cpp)
3. 使用 CMake 慣例
3.1 創(chuàng)建 build 目錄
├── CMakeLists.txt
├── Hello.cpp
└── build
3.2 進(jìn)入 build 執(zhí)行 cmake ..
查看 build 目錄下生成的內(nèi)容:
-rw-r--r-- 1 djimacos staff 14K 11 24 20:13 CMakeCache.txt
drwxr-xr-x 12 djimacos staff 384B 11 24 20:14 CMakeFiles
-rw-r--r-- 1 djimacos staff 5.4K 11 24 20:13 Makefile
-rw-r--r-- 1 djimacos staff 1.6K 11 24 20:13 cmake_install.cmake
3.3 執(zhí)行 make
再次查看 build 目錄下內(nèi)容抱怔,可以看到多了生成的 hello
:
-rw-r--r-- 1 djimacos staff 14K 11 24 20:13 CMakeCache.txt
drwxr-xr-x 12 djimacos staff 384B 11 24 20:14 CMakeFiles
-rw-r--r-- 1 djimacos staff 5.4K 11 24 20:13 Makefile
-rw-r--r-- 1 djimacos staff 1.6K 11 24 20:13 cmake_install.cmake
-rwxr-xr-x 1 djimacos staff 22K 11 24 20:14 hello
3.4 運(yùn)行 ./hello
可以看到程序的輸出內(nèi)容:
hello cmake