文章目录
- 概要
- 整体代码
- 运行结果
概要
上一节中实现了对单个cpp文件用cmake编译。这一节升级一下
整体代码
main.cpp
#include <iostream>
#include "person.h"
using namespace std;
int main()
{
person me = person("langdaoliu", 28, "engineer");
me.printInfo();
return 0;
}
person.cpp
#include "person.h"
person::person(){
cout<<"new person"<<endl;
}
person::person (string name, int age, string work) {
this->age = age;
this->name = name;
this->work = work;
}
void person::printInfo(){
printf("name: %s age: %d work: %s\n",this->name.c_str(), this->age, this->work.c_str());
}
person.h
#pragma once
#include <iostream>
using namespace std;
class person{
private:
string name;
int age;
string work;
public:
person();
person(string name, int age, string work);
void printInfo();
};
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) # cmake最低版本
project(demo1) #项目名称
set(CMAKE_CXX_STANDARD 11) #设置C++编译版本
set(CMAKE_BUILD_TYPE "Debug") # 默认是Release模式,设置为Debug才能调试
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) #设置可执行文件生产的路径
aux_source_directory(. SRC_LISTS) #.下所有的cpp文件打包到变量SRC_LISTS中
add_executable(demo ${SRC_LISTS}) #生成可执行文件demo
运行结果