链接到多个 .obj 以对控制台应用程序进行单元测试

2022-01-11 00:00:00 linker c++ googletest

有一些问题,希望我能找到一些帮助.

Having a few issues and hope I can find some help.

我在 Visual Studio 2012 的同一个解决方案下有两个项目

I have two projects under the same solution in Visual Studio 2012

一些背景知识,我创建了一个控制台应用程序,该应用程序输出为 .exe,这是在一个项目中.

A bit of background I cam creating a console application which outputs as a .exe this is in one project.

在另一个项目中,我设置了 google 测试以在控制台应用程序项目中的类上运行单元测试.

In another project I have google test set up to run unit tests on the classes in the console application project.

如果我能够将主项目编译为静态库,则不会出现问题,因为可以链接到 .lib,但这不是一个选项.

If I was able to compile the main project into a static library there wouldn't be an issue due to could link to the .lib, however this isn't an option.

我发现链接到 .obj 是可行的,但是我需要手动将每一个输入到链接器 -> 输入中,并且由于将有大量的类,我可以看到这变得混乱.

I have found that linking to the .obj works however I need to manually enter each one into the linker -> input and due to there is going to be a large number of classes I can see this getting messy.

我该如何解决这个问题,有没有办法链接到同一个文件夹中的所有 .obj 或者我缺少一个选项?

How do I solve this issue, is there a way of linking to all .obj in the same folder or is there an option I am missing?

推荐答案

我的配置和你的完全相同,这就是我所做的:

I had the exact same configuration as yours and this is what I did:

我使用了visual studio的LIB工具把所有的.obj文件放到了一个.lib库中,然后我就可以让我的gtest项目链接到它了.

I used the LIB utility of visual studio to put all .obj files into a .lib library, and then I could make my gtest project link to it.

下命令

lib /NOLOGO /OUT:"$(TargetPath).lib" "$(ProjectDir)$(Configuration)*.obj"

在主项目的构建事件"/构建后事件"中,您可以将控制台应用程序保存为 .exe,同时将所有 obj 文件聚合到最新的库中.

into the "Build events"/"Post-build Event" of your main project, you can keep your console application as a .exe and in the same time aggregate all your obj files to an up-to-date lib.

希望对你有帮助!

顺便说一下,这个问题和这个帖子有点相似:链接器错误 - 链接两个应用程序"键入项目以使用 Google 测试,我认为在此处链接它可能很有用.

By the way, this problem is a bit similar to this post: Linker error - linking two "application" type projects in order to use Google Test and I think it could be usefull to link it here.

相关文章