打开vs 创建无需代码的项目
点击文件>>新建文件>>Visual C++>> c++文件/头文件 ,创建c++代码
这里创建的文件是未保存的以及名字是系统生成的,通过ctrl+s修改名字保存进指定的文件夹中。
创建如下三个文件
//-------sel_output.h---------
#ifndef SEL_OUTPUT
#include<iostream>
#include<stdlib.h>
using namespace std;
#define SEL_OUTPUT
#define OUTPUT_HELLO "hello"
#define OUTPUT_WORLD "world"
void sel_output(int);
#endif
//--------sel_output.cpp-------
#include"sel_output.h"
void sel_output(int n) {
if (n == 1)
cout << OUTPUT_HELLO << endl;
else
cout << OUTPUT_WORLD << endl;
}
//---------Start.cpp-----------
#include"sel_output.h"
int main() {
int n;
cout << "输入n:" << endl;
cin >> n;
sel_output(n);
system("pause");
return 0;
}
打开Developer command Prompt
D:\Microsoft Visual Studio\2019\Community>cd C:\Users\lenovo\Desktop\demo
C:\Users\lenovo\Desktop\demo>dir
驱动器 C 中的卷没有标签。
卷的序列号是 CA48-F34B
C:\Users\lenovo\Desktop\demo 的目录
2022/02/04 15:22 <DIR> .
2022/02/04 15:22 <DIR> ..
2022/02/04 15:19 140 sel_output.cpp
2022/02/04 15:22 201 sel_output.h
2022/02/04 15:19 138 Start.cpp
3 个文件 479 字节
2 个目录 186,552,991,744 可用字节
C:\Users\lenovo\Desktop\demo>cl
用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.29.30133 版
版权所有(C) Microsoft Corporation。保留所有权利。
用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]
C:\Users\lenovo\Desktop\demo>cl /EHsc Start.cpp sel_output.cpp /link
用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.29.30133 版
版权所有(C) Microsoft Corporation。保留所有权利。
Start.cpp
sel_output.cpp
正在生成代码...
Microsoft (R) Incremental Linker Version 14.29.30133.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Start.exe
Start.obj
sel_output.obj
cl /help
查看帮助
最终生成: 点击Start.exe即可运行
预处理(#define) >> 编译 (生成.obj) >> 链接(所有obj+库文件==>.exe)