C++ Visual Studio 2010 不会像应该的那样从依赖项的项目中自动链接静态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3795567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio 2010 not autolinking static libraries from projects that are dependencies as it should be supposed to
提问by Thomas Bonini
- Create a new solution with a C++ console command-line project
- Create a new project, a C++ static library
- Make the command-line project depend on the library
- Make sure "Link Library Dependencies" is turned on in Configuration => Linker => General (it is by default)
- Visual Studio will still not link the library.
- 使用 C++ 控制台命令行项目创建新解决方案
- 创建一个新项目,一个C++静态库
- 使命令行项目依赖于库
- 确保在配置 => 链接器 => 常规中打开“链接库依赖项”(默认情况下)
- Visual Studio 仍不会链接库。
How can I fix this? It worked in Visual Studio 2008.
我怎样才能解决这个问题?它在 Visual Studio 2008 中工作。
回答by Sean Fausett
This still works, but was changedin VS 2010:
这仍然有效,但在 VS 2010 中已更改:
"With VS2010, we stopped supporting project dependencies defining implicit references and we also introduced a new way of defining project dependencies at the project level. Since a project reference and a project dependency are close concepts, both applying to a project, it made sense to have them represented together, in a consistent way, in the project file. As you will see in the snippets below, the only difference between a project reference definition and a project dependency definition consists in metadata that defines the output assembly inclusion/exclusion into/from the main project link command line. Although we did not remove the “Project Dependencies” dialog, we recommend defining new project dependencies via the “Framework and References” dialog. You need to set the “Reference Assembly Output” property in the property page UI to false for a project dependency and to true for a project reference."
“在 VS2010 中,我们不再支持定义隐式引用的项目依赖项,我们还引入了一种在项目级别定义项目依赖项的新方法。由于项目引用和项目依赖项是接近的概念,都适用于项目,因此将它们以一致的方式一起表示在项目文件中。正如您将在下面的片段中看到的,项目引用定义和项目依赖项定义之间的唯一区别在于元数据定义了输出程序集包含/排除到/从主项目链接命令行。虽然我们没有删除“项目依赖项”对话框,但我们建议通过“框架和引用”对话框定义新的项目依赖项。您需要将属性页 UI 中的“引用程序集输出”属性设置为 false(对于项目依赖项)和 true(对于项目引用)。”
Just right-click on the console project, select "Properties->Common Properties->Framework and References->Add New Reference" and add the static library project; also check that "Link Library Dependencies" is True on the right hand side. Seems to work for debug and release builds. You learn something new every day. ;)
只需右键单击控制台项目,选择“属性->通用属性->框架和引用->添加新引用”并添加静态库项目;还要检查右侧的“链接库依赖项”是否为 True。似乎适用于调试和发布版本。你每天学习新的东西。;)
回答by jalf
They changed the UI for adding C++ project dependencies in VS2010, but oddly enough, without removing the old UI, or in any way indicating that it no longer works.
他们更改了用于在 VS2010 中添加 C++ 项目依赖项的 UI,但奇怪的是,没有删除旧的 UI,或者以任何方式表明它不再有效。
To create the dependency in VS2010, you need to use "Add New Reference" (can be found in project properties), and maybe also in the project's right-click menu (don't have VS here to check)
要在VS2010中创建依赖,需要使用“添加新引用”(可以在项目属性中找到),也可以在项目的右键菜单中(这里没有VS可以查看)
The old "Project Dependencies" dialog is basically broken now.
旧的“项目依赖项”对话框现在基本上被破坏了。
回答by YePhIcK
For MSVC 14 (2015 version) right-click on the project, then "Add->Reference..."
and check all the needed dependencies.
对于 MSVC 14(2015 版),右键单击项目,然后"Add->Reference..."
检查所有需要的依赖项。
Yes, it has changed somewhere between 2010 and 2015 versions. Fun!
是的,它在 2010 和 2015 版本之间发生了变化。乐趣!
And if you are looking to link a project that has resources in it - you need to specify the .res
file directly in the list of linker input dependencies (project's properties dialog box) as it doesn't get picked up by the above configuration.
如果您要链接一个包含资源的项目 - 您需要.res
直接在链接器输入依赖项列表(项目的属性对话框)中指定该文件,因为它不会被上述配置选中。
UPDATE
更新
Still the same (new) behavior in MSVC 2017
MSVC 2017 中的(新)行为仍然相同
回答by Nick
I believe the old UI (dependencies) affects build order for Visual Studio, when building from within the IDE, for info. The new project configuration system embeds the references in each project file so that you can build from outside the IDE (whereas in previous versions, you could not, because you would not get automatic linking for dependencies, since dependencies were only done at the solution level).
我相信旧的 UI(依赖项)会影响 Visual Studio 的构建顺序,当从 IDE 中构建时,以获取信息。新的项目配置系统在每个项目文件中嵌入了引用,以便您可以从 IDE 外部构建(而在以前的版本中,您不能,因为您不会自动链接依赖项,因为依赖项仅在解决方案级别完成)。
There are also some issues with more complex projects in the new system; specifically, all resulting binary projects need to have explicit references to every dependent library to build correctly, whereas previously they could be effectively inherited from other dependent libraries. Same underlying cause, though.
新系统中更复杂的项目也存在一些问题;具体来说,所有生成的二进制项目都需要对每个依赖库进行显式引用才能正确构建,而以前它们可以有效地从其他依赖库继承。不过,同样的根本原因。