cmake简单使用

1.编写hello_world.cpp

#include <iostream>

using namespace std;

int main( int argc,char** argv )
{
      cout<<"hello world"<<endl;
      return 0;

}

放置在code/test

2.在路径下写CMakeLists.txt

project(say)
add_executable(sayHello hello_world.cpp)

~/code/test$ ls

CMakeLists.txt  CMakeLists.txt~  hello_world.cpp  sayHello.out

3.~/code/test$ cmake .
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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/turtlebot/code/test

4.~/code/test$ make
Scanning dependencies of target sayHello
[100%] Building CXX object CMakeFiles/sayHello.dir/hello_world.cpp.o
Linking CXX executable sayHello

[100%] Built target sayHello

5.~/code/test$ ./sayHello

hello world

~/code/test$ ls
CMakeCache.txt  cmake_install.cmake  CMakeLists.txt~  Makefile

CMakeFiles      CMakeLists.txt       hello_world.cpp  sayHello

注意  CMakeLists.txt       hello_world.cpp  sayHello只有这仨有用,其他可以删

ending....

补充:

~/code/test$ mkdir build

~/code/test$ cd build

~/code/test/build$ cmake ..

~/code/test/build$ make

要删不用的文件直接rm build即可

猜你喜欢

转载自blog.csdn.net/qq_33414553/article/details/80638266