flash做ppt的模板下载网站有哪些,网站建设个人,asp网站访问量大,网站建设功能评价指标每一个.cpp文件经过编译之后都会生成对应的.obj文件#xff0c;然后通过链接器把它们进行链接#xff0c;最后就可以生成.exe可执行文件了。
举个例子#xff0c;假设我们有一个 Math.cpp 文件和 Log.cpp 文件#xff1a;
Math.cpp
#include iostreamvoid Log(c…每一个.cpp文件经过编译之后都会生成对应的.obj文件然后通过链接器把它们进行链接最后就可以生成.exe可执行文件了。
举个例子假设我们有一个 Math.cpp 文件和 Log.cpp 文件
Math.cpp
#include iostreamvoid Log(const char* message); // 声明int Multiply(int a, int b)
{Log(Multiply);return a * b;
}int main()
{std::cout Multiply(5, 8) std::endl;std::cin.get();
}Log.cpp
#include iostreamvoid Log(const char* message) // 定义
{std::cout message std::endl;
}直接按 F5 进行编译和链接可以正常运行。但如果我们把 Log.cpp 中的函数名改为 Logr如下
#include iostreamvoid Logr(const char* message) // 定义
{std::cout message std::endl;
}就会出现无法解析的外部命令错误这是因为链接器无法找到函数 Log 的定义。 如果我们注释掉 Math.cpp 的 Multiply 函数中对 Log 函数的调用就不会报错这是因为 Log 函数没有被任何地方调用也不可能被调用
#include iostreamvoid Log(const char* message); // 声明int Multiply(int a, int b)
{//Log(Multiply);return a * b;
}int main()
{std::cout Multiply(5, 8) std::endl;std::cin.get();
}但如果我们注释掉 Math.cpp 中 main 函数对 Multiply 函数的调用而保留 Multiply 函数中的 Log 函数就会报错这是因为虽然当前文件中 Log 函数没有被调用但它是有可能在其他文件中被调用的
#include iostreamvoid Log(const char* message);static int Multiply(int a, int b)
{Log(Multiply);return a * b;
}int main()
{//std::cout Multiply(5, 8) std::endl;std::cin.get();
}解决方法是给 Multiply 函数加上 static 表明这个函数只能在当前文件 Math.cpp 被调用。
另一个最常见的错误就是重复定义即使是看上去好像只定义了一次如下例
Log.h
void Log(const char* message)
{std::cout message std::endl;
}Log.cpp
#include iostream
#include Log.hvoid InitLog()
{Log(Initialized Log);
}Math.cpp
#include iostream
#include Log.hint Multiply(int a, int b)
{Log(Multiply);return a * b;
}int main()
{std::cout Multiply(5, 8) std::endl;std::cin.get();
}对两个cpp文件进行编译之后点击链接Build就会报错 这个 LINK 开头的错误就是链接错误它说 Math.obj 中的 Log 函数已经在 Log.obj 中定义了出现多次定义但是我明明只在 Log.h 中定义了一次呀为什么会这样呢 回想之前我们对#include的功能介绍它就是复制头文件的内容然后粘贴到它所在的位置所以实际上我们的两个 cpp 文件经过预处理之后会变成这样
Log.cpp
#include iostream
void Log(const char* message)
{std::cout message std::endl;
}void InitLog()
{Log(Initialized Log);
}Math.cpp
#include iostream
void Log(const char* message)
{std::cout message std::endl;
}int Multiply(int a, int b)
{Log(Multiply);return a * b;
}int main()
{std::cout Multiply(5, 8) std::endl;std::cin.get();
}显然我们确实多次定义了 Log 函数那有什么解决方法呢
方法一
在Log.h中定义的Log函数前加上static如下
static void Log(const char* message)
{std::cout message std::endl;
}这样Log函数在两个cpp文件就是各自为政了不会发生冲突。
方法二
在Log.h中定义的Log函数前加上inline如下
inline void Log(const char* message)
{std::cout message std::endl;
}inline的效果就是在调用函数的地方直接用函数内容进行替换再执行如
Log.cpp
#include iostream
#include Log.hvoid InitLog()
{Log(Initialized Log);
}会变成这样
#include iostream
#include Log.hvoid InitLog()
{std::cout Initialized Log std::endl;
}方法三
这是最为常用的方法就是不要在头文件中定义函数把定义搬到一个的cpp文件就行了
Log.h
void Log(const char* message)Log.cpp
#include iostream
#include Log.hvoid Log(const char* message)
{std::cout message std::endl;
}void InitLog()
{Log(Initialized Log);
}Math.cpp
#include iostream
#include Log.hint Multiply(int a, int b)
{Log(Multiply);return a * b;
}int main()
{std::cout Multiply(5, 8) std::endl;std::cin.get();
}