我已经设置了两个解决方案来说明我的问题,共三个项目。我在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