c++文件源代码:main.cc
#include <stdio.h>
#include <stdlib.h>
class MyCalc
{
public:
int a;
int b;
int getAdd();
};
int MyCalc::getAdd()
{
return a+b;
}
int main(int argc, char *argv[])
{
printf("====Welcome cpp test=======\n");
MyCalc mc;
mc.a=10;
mc.b=90;
printf("Add res = %d \n", mc.getAdd() );
return 0;
}
当前目录下的CMakeLists.txt
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo1)
# 指定生成目标
add_executable(Demo main.cc)
然后在当前目录下新建一个文件夹build (任意定义,推荐名称build)
然后进入build目录下终端运行命令: cmake ..
(cmake后跟着点点,代表运行当前目录前一级的Lists配置,输出的缓存文件在当前build目录)
终端显示:
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.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/bnis/Desktop/TestCode/build
看到build目录下生成很多中间文件,其中有个是Makefile,然后执行make
[ 50%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
得到最终输出执行文件Demo
若需要重新cmake,需要清空build文件夹。在build当前目录运行 rm -rf *