author:张一极
主程序位于main.cpp:
.├── CMakeLists.txt├── main.cppx
int main(int argc, char *argv[]){ std::cout << "Hello World!" << std::endl; return 0;}编译描述文件CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)project (hello_world)add_executable(hello_world main.cpp)
cmake_minimum_required(VERSION 3.5)上述语句,定义了最低版本为3.5的cmake
用cmake --version可以获取当前系统cmake版本
project (hello_world)xxxxxxxxxxadd_executable(hello_world main.cpp)#第一个参数是编译生成的文件名,第二个参数是编译文件也可以直接使用项目名作为第一个参数:
xxxxxxxxxxadd_executable(${PROJECT_NAME} main.cpp)xxxxxxxxxx$root@2080ti_76:~/home/ai/edge/hello_world/# 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 ...会多出一堆东西:
xxxxxxxxxx CMakeCache.txt├── CMakeFiles│ ├── 3.15.0│ │ ├── CMakeCCompiler.cmake│ │ ├── CMakeCXXCompiler.cmake│ │ ├── CMakeDetermineCompilerABI_C.bin│ │ ├── CMakeDetermineCompilerABI_CXX.bin│ │ ├── CMakeSystem.cmake│ │ ├── CompilerIdC│ │ │ ├── a.out│ │ │ ├── CMakeCCompilerId.c│ │ │ └── tmp│ │ └── CompilerIdCXX│ │ ├── a.out│ │ ├── CMakeCXXCompilerId.cpp│ │ └── tmp│ ├── cmake.check_cache│ ├── CMakeDirectoryInformation.cmake│ ├── CMakeOutput.log│ ├── CMakeTmp接下来流程:
1.创建一个新文件夹:mkdir build
2.进入这个文件夹:cd build
3.执行:cmake ..
4.退回根目录:cd ..
5.执行:make
6.得到可执行文件:./hello_world
7.output : helloworld
xxxxxxxxxx(caffe2) root@b5cd6e1:~/c_inference/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 donexxxxxxxxxx//退回根目录Scanning dependencies of target hello_world[ 50%] Building CXX object CMakeFiles/hello_world.dir/main.cpp.o[100%] Linking CXX executable hello_world[100%] Built target hello_worldxxxxxxxxxx//根目录编译生成可执行文件Hello world!