简体   繁体   English

如何链接到同一VC ++ 2010解决方案中项目产生的.lib

[英]How to link to .lib produced by project in same VC++ 2010 solution

I have a VC++ 2010 solution with multiple project, one of which is a .lib produced by my desire to put some common code in a single place. 我有一个带有多个项目的VC ++ 2010解决方案,其中一个是我希望将一些通用代码放在一个地方的.lib。

I'm now trying to get another of the projects, which is a normal console application, to link to this library. 我现在正在尝试让另一个项目(该项目是普通的控制台应用程序)链接到该库。 However, VS is complaining that it can't find the .lib even though I added it as an additional dependency. 但是,VS抱怨说即使我将它添加为附加依赖项,也找不到.lib。

I was kind of hoping that VS would make it easy on me since the .lib is in the same solution, albeit in a different project, but sadly, no luck. 我有点希望VS可以简化我的工作,因为.lib在相同的解决方案中,尽管在不同的项目中,但是可惜没有运气。

EDIT I must be blind but I can't seem to find the 'additional paths' option Tim mentioned. 编辑我一定是瞎子,但我似乎找不到蒂姆提到的“其他路径”选项。 I added the lib project to the project dependencies of the console project, but it still isn't working. 我将lib项目添加到了控制台项目的项目依赖项中,但仍然无法正常工作。

EDIT2 Edwin's suggestion did the trick after I actually added the .cpp and .h files to the .lib project. 在我实际上将.cpp和.h文件添加到.lib项目之后, EDIT2 Edwin的建议成功了。 I really hate it when I miss something so obvious. 当我错过如此明显的事情时,我真的很讨厌它。

In the properties_>Common Properties->Framework & dependecies of your executable you can add projects on which your executable is dependant of. 在可执行文件的properties_>“公共属性”->“框架和依赖项”中 ,您可以添加可执行文件所依赖的项目。 After that you only need to adjust additional include paths of C++->General to let it find your include-file. 之后,您只需要调整C ++-> General的 其他包含路径即可让它找到您的包含文件。

EDIT: I have an exe-project named AppTest and a lib-project Dummy. 编辑:我有一个名为AppTest的exe项目和一个lib项目Dummy。

I added the project Dummy to Framework & dependecies of AppTest. 我将项目Dummy添加到AppTest的Framework&Dependecies中。

Then i adjusted for all configurations the include paths of AppTest to point to directory of Dummy. 然后,我针对所有配置调整了AppTest的包含路径,以指向Dummy目录。

Then i included dummy.h in apptest.cpp and a call to a function from dummy.lib. 然后,我在apptest.cpp中包含了dummy.h,并从dummy.lib中调用了一个函数。

Build the solution. 构建解决方案。 That's all. 就这样。

There are three (maybe more :-) ways of linking a library in Visual C++. 在Visual C ++中,有三种(可能更多:-)链接库的方法。 You can explicitly include .lib file in the project by doing Add Existing Item and pointing to the lib file. 通过执行“添加现有项”并指向lib文件,可以在项目中显式包括.lib文件。 That way you don't need to specify the path. 这样,您无需指定路径。 You can add a file in Options/Linker/Input and specify path in Additional Library Directories. 您可以在选项/链接器/输入中添加文件,并在其他库目录中指定路径。 And finally, you can use #pragma comment( lib, "my.lib") and specify the path as before. 最后,您可以使用#pragma comment(lib,“ my.lib”)并像以前一样指定路径。 I'm not sure, but I think the above pragma also allows to put the full path as well. 我不确定,但我认为上述实用性原则也可以提供完整的方法。

在依赖lib的项目中更改“其他路径”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM