2016-07-05 67 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