2016-07-05 96 views
1

我已經設置了兩個解決方案來說明我的問題,共三個項目。我在Debug模式下編譯,但即使我在Release模式下編譯,我的問題仍然存在。Visual Studio LNK1104最小示例

MyRunnerCore.lib要求3rdParty.lib。爲什麼呢,我能對付它呢?

這裏是一個圖片說明文件夾的結構如何:

SolutionLayout

的想法是,我建立了第三方作爲一個lib項目(正常工作)。然後,我只使用cpp文件中的lib文件構建MyRunnerCore(工作正常)。最後,我最少生成一個使用MyRunnerCore.lib(LNK1104)的控制檯應用程序。輸出窗口顯示如下:

1>------ Build started: Project: MyRunnerCore, Configuration: Release Win32 ------ 
1> Core.cpp 
1> MyRunnerCore.vcxproj -> C:\SO\MyRunner\Release\MyRunnerCore.lib 
2>------ Build started: Project: MyRunner, Configuration: Release Win32 ------ 
2> main.cpp 
2>LINK : fatal error LNK1104: cannot open file '3rdParty.lib' 
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

以下是帶有註釋的源文件,指出項目編輯過的某些設置。

ThirdPartyClass.cpp

#include "ThirdParyClass.hpp" 
int ThirdParyClass::GenerateNumber() 
{ return 4; } 

ThirdPartyClass.hpp

#pragma once 
class ThirdParyClass 
{ public: int GenerateNumber(); }; 

的main.cpp

//MyRunner Properties: 
//Project Dependencies Added MyRunnerCore 
//Include Directories Added $(SolutionDir) 
//Library Directories Added $(OutDir) 
#include <MyRunnerCore\Core.h> 
#pragma comment (lib, "MyRunnerCore.lib") 

int main() { Core c{}; return c.Run(); } 

Core.cpp

#include "Core.h" 
//MyRunnerCore Properties: 
//Added To Include Path C:\SO\3rdParty 
//Added To Library Path C:\SO\3rdParty\Debug 
#include <3rdParty\ThirdParyClass.hpp> 
#pragma comment(lib, "3rdParty.lib") 

int Core::Run() 
{ 
    ThirdParyClass tp{}; 
    return tp.GenerateNumber(); 
} 

Core.h

#pragma once 
class Core 
{ public: int Run(); }; 

爲什麼鏈接器需要3rdParty.lib鏈接?

我是否缺少一個設置讓Linker構建MyRunnerCore.lib而不參考3rdParty.lib

+1

MSVC不支持結合靜態庫的功能。您需要明確鏈接到第三方庫,並且它必須位於爲鏈接程序設置以查找庫的路徑中。 – drescherjm

回答

0

由於tsandy寫道:

Librarian -> General -> Link Library Dependencies -> Yes 

是正確的。但使用以下內容

#pragma comment (lib, ...) 

與此不相容。

圖書館有使用

Librarian -> Additional Libraries -> 3rdParty.lib;%(AdditionalDependencies) 

感謝tsandy的輸入被包括在內。

1

看來,鏈接器不知道在哪裏可以找到文件ThirdParyClass.lib。在項目設置MyRunner中,將包含此文件的文件夾添加到鏈接器下的Additional Library Directories

+1

是的,這是事實,但連接器不應該知道它。我希望鏈接器在MyRunnerCore中包含ThirdPartyClass.lib。lib,以便在構建MyRunner時不需要提供它。 – Johannes

+0

我的理解是,當'Core'編譯時,它實際上並沒有從'ThirdPartyClass.lib'複製'ThirdPartyClass'的實現。相反,它會留下一個佔位符,說「用於執行ThirdPartyClass',請參閱'ThirdPartyClass.lib'」。我不知道有什麼設置來解決這個機制。 – tsandy

+0

這是這個問題的核心,你有任何文檔鏈接? – Johannes