Visual Studio 2012与静态库链接错误 [英] Visual Studio 2012 link errors with static libraries

查看:362
本文介绍了Visual Studio 2012与静态库链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从2005年升级到2012年(我很确定),在构建我的解决方案时遇到了链接错误。 google搜索几个小时之后,我跑出了想法。我已经建立了数十个项目,所以我很确定我已经做了一切正确的事情,但公平地说,已经有几年了。



所以,作为测试,我设立了一个新项目。我把它叫做App,一个Windows应用程序(.exe)。我创建了一个名为Core的第二个项目,并将其标记为其配置类型中的静态库(.lib)。两者都是解决方案的一部分。在Core中,我添加了Test.cpp和Test.h,它包含一个具有简单功能的小类。然后,在Window的应用程序WinMain.cpp文件中,我有WinMain(),我通过CTest测试调用这个测试类; test.Result();



随着这一点,我将Project的项目依赖项设置为Core,最后添加到App的Additional Include Directories到Core代码的路径Test.cpp / .h在哪里。



我收到以下链接错误,无法为我的生活找出原因。有没有人知道2012年以前版本可能错过了哪些步骤?非常感谢您的帮助!

  1> ------ Build started:Project:App,配置:Debug Win32 ------ 
1> WinMain.obj:error LNK2019:未解析的外部符号public:__thiscall CTest :: CTest(void)(?0CTest @@ QAE @ XZ)函数_WinMain @ 16
1> WinMain.obj:error LNK2019:未解析的外部符号public:__thiscall CTest ::〜CTest(void)(?? 1CTest @@ QAE @ XZ)在函数_WinMain @ 16
1> WinMain.obj:error LNK2019:未解析的外部符号public:int __thiscall CTest :: Result(void)(?Result @ CTest @@ QAEHXZ)在函数_WinMain @ 16
1> D :\Work\Test_Linker_Stupidity\App\Debug\App.exe:致命错误LNK1120:3未解决的外部文件
==========构建:0成功,1失败,1对-to-date,0 skipped ========


解决方案

Visual Studio 2005做了一些魔术与项目依赖,它会自动任何.lib输出(我不幸的是开发人员帮助实现它)。这似乎已被删除,因为我怀疑Visual Studio 2010当旧的Visual C ++构建系统被替换为MSBuild。



但是,静态库的自动链接依赖关系功能仍可以通过项目引用找到:




  • 右键单击App项目并选择References ...

  • 单击添加新参考。

  • 检查静态库项目,然后按OK。

  • Build。 li>


您现在应该会看到自动链接的静态库。请注意,项目引用也意味着项目依赖。



如果您更喜欢使用项目依赖关系,则需要将静态库添加到App项目中的链接器附加依赖关系属性,就像您对任何其他静态库输入一样。



编辑:另外,您将在项目参考中看到一个名为Link Library Dependencies的属性。这将控制引用的项目的.lib输出是否链接到(默认为true)。


I recently upgrade to 2012 from 2005 (I'm pretty sure) and ran into link errors when building my solution. I ran out of ideas after hours of searching google and putsing around. I've set up dozens of projects, so I'm pretty certain I've done everything right, but to be fair, it's been a few years.

So, as a test I set up a new project. I called it App, a Windows application (.exe). I created a second project called Core and flagged it as a static library (.lib) in it's Configuration Type. Both are a part of the solution. In Core I added Test.cpp and Test.h which contain a small class that has a simple function in it. Then, inside the Window's applications WinMain.cpp file I have WinMain() where I call into this test class via CTest test; test.Result();

Along with this I've set the project App's Project Dependencies to be Core and finally added to App's Additional Include Directories the path to the Core code where Test.cpp/.h live.

I get the following link errors and cannot for the life of me figure out why. Does anyone know what step I may have missed or what changed in 2012 from previous versions? Thank you very very much in advance for the help!

1>------ Build started: Project: App, Configuration: Debug Win32 ------
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall CTest::CTest(void)" (??0CTest@@QAE@XZ) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: __thiscall CTest::~CTest(void)" (??1CTest@@QAE@XZ) referenced in function _WinMain@16
1>WinMain.obj : error LNK2019: unresolved external symbol "public: int __thiscall CTest::Result(void)" (?Result@CTest@@QAEHXZ) referenced in function _WinMain@16
1>D:\Work\Test_Linker_Stupidity\App\Debug\App.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========

解决方案

Visual Studio 2005 did some magic with project dependencies where it would automatically link in any .lib outputs (I unfortunately was the developer that helped implement it). This appears to have been removed since, I suspect, Visual Studio 2010 when the old Visual C++ build system was replaced with MSBuild.

However, the "automatic linking of static library dependencies" feature can still be found via project references:

  • Right click on the App project and select "References..."
  • Click "Add New Reference".
  • Check the static library project and press OK.
  • Build.

You should now see the static library being automatically linked in. Note that project references also imply a project dependency.

If you prefer to use a project dependency instead, you'll need to add the static library to the linker's additional dependencies property on the "App" project, like you would for any other static library input.

Edit: also, you'll see a property called "Link Library Dependencies" on the project reference. This controls whether or not the .lib output of the referenced project gets linked in or not (defaults to true).

这篇关于Visual Studio 2012与静态库链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆