c++中函数只声明,不定义,代码中也没有该函数的调用,可以编译并运行(vs2015)。
/*
Author:gtkiller
Date:2018/03/19
*/
#include <iostream>
#include <string>
int Add(int a, int b)
{
return a + b;
}
int Sub(int a, int b);
int main()
{
int a = 3, b = 5;
int c = Add(a, b);
std::cout << c << std::endl;
system("pause");
return 0;
}
结果是可以编译并运行:
在网上找了一些解释:
如果没有函数调用的话,编译器恐怕不会产生任何代码……
如果有调用的话,会产生调用函数的代码,至于函数的实现在哪里,那不是编译器要考虑的事,链接器才需要查找函数的实现代码并与函数调用代码对上……
https://zhidao.baidu.com/question/569604188.html