C++ 使用静态库链接时发生链接错误
以前可能有人问过这个问题,但是,我发现它只存在于类的上下文中,而事实并非如此 Utils.hC++ 使用静态库链接时发生链接错误,c++,visual-studio,static-libraries,linkage,C++,Visual Studio,Static Libraries,Linkage,以前可能有人问过这个问题,但是,我发现它只存在于类的上下文中,而事实并非如此 Utils.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);
#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_
为什么它找不到实现“numof除数”的代码?我已经给了它一个包含它的.lib,而且-将依赖关系放在Utils项目本身上。。。
任何帮助都将不胜感激。看起来您的Utils.cpp中没有定义numOfDivisors()方法,您能检查一下吗 为什么编译器抱怨“G:\ProjectEuler\Problem12\Source.obj”?Source.obj来自哪里
您必须在一个字段中指定库路径,在另一个字段中指定库名称,是否在适当的设置下指定了这两个字段 假设库已正确构建和链接,则下一个最可能的错误原因是函数在库中的名称与链接到它的代码中的名称不同
这可能是由影响名称装饰或类型名称的任意数量的项目设置引起的。在你的案例中,从远处猜测哪种特定环境是罪魁祸首实际上没有任何意义。您可以比较这两个项目的属性(手动或使用diff工具),并尝试找出可能导致不同修饰函数名的差异。您的库是编译为
C
还是编译为C++
?我在哪里可以检查?据我所知,我可以在“属性”->“链接器”->“输入”->“附加依赖项”。至少它在Visual2010中对我有效。
#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;
}
#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