其他分享
首页 > 其他分享> > CMake快速入门

CMake快速入门

作者:互联网

大致搞明白Makefile编写方法后,我觉得应该弄明白CMake了。还是从Hello World开始吧,一共三份文件构成这个项目:main.cpp、hello.cpp、hello.hpp。按照资料说明,要在对应的文件夹里面写一个 CMakeLists.txt 文件:

PROJECT(HELLO)
SET(SRC_LIST main.cpp hello.cpp)
MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

保存后,运行: cmake .

$ cmake .
-- This is BINARY dir /media/yeping/Works/demos/d0001-hello
-- This is SOURCE dir /media/yeping/Works/demos/d0001-hello
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yeping/Works/demos/d0001-hello

$ make
Scanning dependencies of target hello
[ 33%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

$ ./hello
Hello, World!

非常OK!

当然,CMakeLists.txt 还可以简化如下:

PROJECT(HELLO)
ADD_EXECUTABLE(hello main.cpp hello.cpp)

运行一下:

$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yeping/Works/demos/d0001-hello

$ make
[100%] Built target hello

$ ./hello
Hello, World!

那是相当OK!

标签:CMake,入门,--,done,cpp,Works,快速,hello,dir
来源: https://blog.csdn.net/quicmous/article/details/112484705