?(一)CMake使用
CMake使用
1.注释
# 这是一个CMakeLists.txt文件
cmake_minimum_required(VERSION 3.10)
2.add_executable 定义工程会生成一个可执行程序
add_executable(可执行程序名 源文件名称)
# 样式1:
add_executable(app add.c div.c main.c mult.c sub.c)
(二)HelloWorld 项目
heheda@linux:~/Linux/HelloWorld$ tree
.
├── build
├── CMakeLists.txt
└── helloworld.cpp
1 directory, 2 files
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
cout<<"Hello World"<<endl;
return 0;
}
cmake_minimum_required(VERSION 3.10)
project(HELLOWORLD)
add_executable(app helloworld.cpp) # g++ helloworld.cpp -o app
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5./app
执行结果:
heheda@linux:~/Linux/HelloWorld$ cd build
heheda@linux:~/Linux/HelloWorld/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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/heheda/Linux/HelloWorld/build
heheda@linux:~/Linux/HelloWorld/build$ make
Scanning dependencies of target app
[ 50%] Building CXX object CMakeFiles/app.dir/helloworld.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
heheda@linux:~/Linux/HelloWorld/build$ ./app
Hello World
heheda@linux:~/Linux/HelloWorld/build$
?