1. 创建项目工程及代码编写(Windows平台)
首先创建项目文件(文件名hello),并用VScode打开,进行项目编程。项目结构如下:
“避坑说明:头文件名称不要设置为include,会识别不到,报错”
对应文件代码如下:
头文件 print.h
#ifndef _PRINT_H_
#define _PRINT_H_
#include <iostream>
using namespace std;
void print();
#endif
源文件 print.cpp
#include "print.h"
void print()
{
cout << "hello world!" << endl;
}
源文件 main.cpp
#include <iostream>
#include "print.h"
using namespace std;
int main()
{
print();
//cout << "hello, VScode!" << endl;
return 0;
}
CMakeLists.txt文件
#规定cmake的最低版本要求
cmake_minimum_required (VERSION 3.10)
#设置c++编译器
set(CMAKE_CXX_COMPILER "g++