richard@RicharddeMacBook-Air project % cmake . -- The C compiler identification is AppleClang 17.0.0.17000013 -- The CXX compiler identification is AppleClang 17.0.0.17000013 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (0.4s) -- Generating done (0.0s) -- Build files have been written to: /Users/richard/project
这时我们发现文件夹下多了很多文件:
1 2 3
richard@RicharddeMacBook-Air project % ls cmake_install.cmake CMakeFiles hello.cpp CMakeCache.txt CMakeLists.txt Makefile
再运行make,它会读取 CMake 生成的 Makefile,并按照规则:
编译所有源代码文件(如 .cpp)为目标文件(.o)
将目标文件链接成最终的可执行程序(如你看到的 main)
处理依赖关系(比如某个文件修改后,只重新编译受影响的部分)
1 2 3 4
richard@RicharddeMacBook-Air project % make [ 50%] Building CXX object CMakeFiles/main.dir/hello.cpp.o [100%] Linking CXX executable main [100%] Built target main
我们再查看文件夹,发现多出了一个名为 main的可执行程序
1 2 3 4
richard@RicharddeMacBook-Air project % ls cmake_install.cmake CMakeLists.txt Makefile CMakeCache.txt hello.cpp CMakeFiles main
这时我们再运行这个可执行文件就成功输出了:
1 2
richard@RicharddeMacBook-Air project % ./main hello world