之前可能已经问过这个问题,但是,我只在类的上下文中发现它,而事实并非如此。
实用程序.h
#ifndef _UTILS_H_
#define _UTILS_H_
#include <cmath>
//is 'x' prime?
bool isPrime(long long int x);
//find the number of divisors of 'x' (including 1 and x)
int numOfDivisors(long long int x);
#endif //_UTILS_H_
实用程序.cpp
#include "Utils.h"
bool isPrime(long long int x){
if (x < 2){
return false;
}
long double rootOfX = sqrt( x );
long long int flooredRoot = (long long int)floor ( rootOfX );
for (long long int i = 2; i <= flooredRoot; i++){
if (x % i == 0){
return false;
}
}
return true;
}
int numOfDivisors(long long int x){
if (x == 1){
return 1;
}
long long int maxDivisor = (x / 2) + 1;
int divisorsCount = 0;
for (long long int i = 2; i<=maxDivisor; i++){
if (x % i == 0){
divisorsCount++;
}
}
divisorsCount += 2; //for 1 & x itself
return divisorsCount;
}
这两个文件已在调试模式下使用 Visual Studio 2012 编译为静态库。现在我尝试在一个单独的项目中使用它们,我们称之为 MainProject:
1. 将“Utils.vcproj”添加到 MainProject 解决方案中。
2. 使 MainProject 依赖于 Utils
3. 在“Properties”->“Linker”->“Input”->“Additional Dependencies”中输入 Utils.lib 的路径
这是使用 Utils 的主要内容:
#include <iostream>
#include "..\Utils\Utils.h"
using namespace std;
int main(){
cout << "num of divisors of " << 28 << ": " << numOfDivisors(28) << endl;
//this part is merely to stop visual studio and look at the output
char x;
cin >> x;
return 0;
}
这是我得到的错误:
Error 1 error LNK2019: unresolved external symbol "int __cdecl numOfDivisors(__int64)" (?numOfDivisors@@YAH_J@Z) referenced in function _main G:\ProjectEuler\Problem12\Source.obj Problem12
为什么找不到实现“numOfDivisors”的代码?我已经给了它包含它的 .lib,此外 - 依赖于 Utils 项目本身......任何帮助将不胜感激。