我设置了两个解决方案来说明我的问题,一共三个项目。我在 Debug 模式下编译,但即使我在 Release 模式下编译,我的问题仍然存在。
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 ==========
这是带有注释的源文件,指出我在哪个项目中编辑了一些设置。
第三方类.cpp
#include "ThirdParyClass.hpp"
int ThirdParyClass::GenerateNumber()
{ return 4; }
第三方类.hpp
#pragma once
class ThirdParyClass
{ public: int GenerateNumber(); };
主要.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(); }
核心.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();
}
核心.h
#pragma once
class Core
{ public: int Run(); };
为什么链接器需要 3rdParty.lib 才能链接?
我是否缺少使链接器构建 MyRunnerCore.lib
构建时不引用 3rdParty.lib
的设置?
最佳答案
似乎链接器不知道在哪里可以找到文件ThirdParyClass.lib
。在 MyRunner
的项目设置中,将包含此文件的文件夹添加到链接器下的 Additional Library Directories
。
关于c++ - 带有最小示例的 Visual Studio LNK1104,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212132/