c++的代码如果使用其他文件中定义的函数,这个问题困扰了我很久,所以今天通过网上查资料和测试,可以成功的运行一个例子了。以此记录。
建立一个win32控制台程序。然后新建一个util.h头文件。头文件的内容用下面的代码:
#include <string>
using namespace std;
void print(string str);
再建立一个util.cpp文件,内容如下:
#include <iostream>
#include <string>
using namespace std;
void print(string str)
{
cout << str << endl;
}
最后建立一个Main.cpp文件,代码如下:
#include <iostream>
#include "util.h"
using namespace std;
int main()
{
print("helloworld");
return 0;
}
例子虽然做出来了,但是我在考虑,代码是如何找到util.cpp文件里面的实现代码。util.cpp这个文件名使用其他的名称也行。