0

之前可能已经问过这个问题,但是,我只在类的上下文中发现它,而事实并非如此。

实用程序.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 项目本身......任何帮助将不胜感激。

4

2 回答 2

0

看起来方法 numOfDivisors() 没有在 Utils.cpp 中定义,你可以检查一次吗?

为什么您的编译器会抱怨“G:\ProjectEuler\Problem12\Source.obj”?Source.obj 来自哪里?

您必须在一个字段中指定库路径并在另一字段中指定库名称,您是否在适当的设置下指定了两者?

于 2013-03-26T16:57:14.953 回答
0

假设该库已正确构建和链接,则错误的下一个最可能原因是该函数在库中的名称与链接到它的代码中的名称不同。

这可能是由影响名称修饰或类型名称的任意数量的项目设置引起的。从远处猜测哪个特定设置是您的情况的罪魁祸首没有任何意义。您可以比较两个项目的属性(手动或使用差异工具)并尝试找出可能导致不同修饰函数名称的差异。

于 2013-03-27T02:49:14.510 回答