這可能以前曾被問過,但是,我發現它僅在類的上下文中,而事實並非如此。使用靜態庫鏈接時鏈接錯誤
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);
#endif //_UTILS_H_
Utils.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.在「屬性」 - >「鏈接」 - >「輸入」 - >「附加依賴」把路徑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)" ([email protected]@[email protected]) referenced in function _main G:\ProjectEuler\Problem12\Source.obj Problem12
爲什麼不能找到一個實現 「numOfDivisors」 的代碼?我已經給它包含它的.lib,此外 - 把對Utils項目本身的依賴... 任何幫助,將不勝感激。
您的庫是以'C'還是'C++'編譯的? – SomeWittyUsername 2013-03-26 17:00:29
我可以在哪裏查看? – BegemoD 2013-03-26 17:17:10