簡體   English   中英

使用靜態庫鏈接時鏈接錯誤

[英]Linking error while using static library linkage

以前可能會問過這個問題,但是,我僅在類的上下文中才發現它,而事實並非如此。

實用程序

#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_

實用程序

#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)" (?numOfDivisors@@YAH_J@Z) referenced in function _main   G:\ProjectEuler\Problem12\Source.obj    Problem12

為什么找不到實現“ numOfDivisors”的代碼? 此外,我給了它包含它的.lib-依賴於Utils項目本身...任何幫助將不勝感激。

看起來Utils.cpp中沒有定義方法numOfDivisors(),您可以檢查一次嗎?

為什么您的編譯器抱怨“ G:\\ ProjectEuler \\ Problem12 \\ Source.obj”? Source.obj來自哪里?

您必須在一個字段中指定庫路徑,在另一字段中指定庫名稱,是否在適當的設置下都指定了庫路徑?

假設該庫已正確構建和鏈接,則該錯誤的下一個最可能的原因是該函數在庫中的名稱不同於在鏈接到該代碼的代碼中的名稱。

這可能是由影響名稱修飾或類型名稱的許多項目設置引起的。 從遠處猜測實際上沒有任何意義,這是您所遇到的特殊情況。 您可以比較兩個項目的屬性(手動或使用diff工具),並嘗試找出可能導致裝飾名稱不同的差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM