我已經設置了兩個解決方案來說明我的問題,共三個項目。我在Debug模式下編譯,但即使我在Release模式下編譯,我的問題仍然存在。Visual Studio LNK1104最小示例
MyRunnerCore.lib
要求3rdParty.lib
。爲什麼呢,我能對付它呢?
這裏是一個圖片說明文件夾的結構如何:
的想法是,我建立了第三方作爲一個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
?
MSVC不支持結合靜態庫的功能。您需要明確鏈接到第三方庫,並且它必須位於爲鏈接程序設置以查找庫的路徑中。 – drescherjm