简体   繁体   English

在Visual Studio Post-Build事件中使用lnk.exe从.lib文件中删除特定对象

[英]Remove specific object from .lib file with lnk.exe in Visual Studio Post-Build event

I have a C++ project where I use Google Test to write my unit tests. 我有一个C ++项目,使用Google Test编写单元测试。 The project has been around for a while and is quite messy, so I just added a line at the beginning of the main function that starts the unit tests and then exits the program. 该项目已经存在了一段时间,并且非常混乱,因此我只是在main函数的开头添加了一行,以开始单元测试,然后退出程序。 I would then comment and uncomment this line for switching between the unit tests and the real application. 然后,我将注释和取消注释该行,以在单元测试和实际应用程序之间切换。

This worked okey when only I used my code, but now I'm trying to solve this in a proper way, with two projects and .exe files, one for the real application and one for the tests, like toussa's answer here: Visual Studio C++: Unit test exe project with google test? 仅当我使用我的代码时,此方法很有效,但现在我试图以一种正确的方式解决此问题,其中包含两个项目和.exe文件,一个用于实际应用程序,一个用于测试,例如toussa在这里的答案: Visual Studio C ++:单元测试exe项目与Google测试?

And his explanation for doing so here: Linking to multiple .obj for unit testing a console application 以及他对此的解释: 链接到多个.obj以对控制台应用程序进行单元测试

The problem is that all the .obj-files are put into this library, including the main function. 问题是所有.obj文件都放入此库中,包括主函数。 This makes it impossible for me to link with my testmain, as _main is already defined. 因为已经定义了_main,所以这使我无法与testmain链接。 I tried adding the "/REMOVE" option to the command, so it looks like this: 我尝试在命令中添加“ / REMOVE”选项,因此如下所示:

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

where mainfile.cpp is compiled to mainfile.obj, I hope. 我希望将mainfile.cpp编译为mainfile.obj。 The output from lnk is: lnk的输出为:

LINK : warning LNK4014: cannot find member object C:\dev\solutions\currentSolution\currentProject\Debug\mainfile.obj

The only thing I found about how to write the name of the object file is from here: http://msdn.microsoft.com/en-us/library/5ff8sk86(v=vs.100).aspx 我发现有关如何写入目标文件名称的唯一信息是从这里: http : //msdn.microsoft.com/zh-cn/library/5ff8sk86(v=vs.100).aspx

where they write: 他们在哪里写:

The /REMOVE and /EXTRACT options require the full name of the member object that is to be deleted or copied to a file. / REMOVE和/ EXTRACT选项要求删除或复制到文件的成员对象的全名。 The full name includes the path of the original object file. 全名包括原始目标文件的路径。 To see the full names of member objects in a library, use DUMPBIN /ARCHIVEMEMBERS or LIB /LIST. 要查看库中成员对象的全名,请使用DUMPBIN / ARCHIVEMEMBERS或LIB / LIST。

If I type any of those two I get a list where "C:\\dev\\solutions\\currentSolution\\currentProject\\Debug\\mainfile.obj" is one of the entries. 如果输入这两个中的任何一个,我都会得到一个列表,其中“ C:\\ dev \\ solutions \\ currentSolution \\ currentProject \\ Debug \\ mainfile.obj”是其中之一。

What am I doing wrong? 我究竟做错了什么? Is there some place that I type something wrong, or is there an easier solution to this problem? 是否在某些地方输入了错误的信息,或者有解决此问题的简便方法?

I had exactly the same problem while using Google Test to build unit tests for a console application. 使用Google Test构建控制台应用程序的单元测试时,我遇到了完全相同的问题。 I solved it by splitting the post-build action int two separate lib calls. 我通过将构建后操作int拆分为两个单独的lib调用来解决此问题。 First one builds the lib from all *.obj files. 第一个从所有* .obj文件构建lib。 Second call removes the *.obj file with the main function from the *.lib file. 第二次调用从* .lib文件中删除具有主要功能的* .obj文件。 In your case the calls would be: 您的情况将是:

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

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

相关问题 Visual Studio:即使项目是最新的,也运行C ++项目Post-Build Event - Visual Studio: Run C++ project Post-Build Event even if project is up-to-date 生成后脚本在VIsual Studio 2010中引发错误 - Post-Build script throws error in VIsual Studio 2010 如何在Visual Studio中正确设置我的构建后脚本 - How to correctly setup my post-build script in visual studio VC生成后事件命令 - VC post-build event command Visual Studio - 无法从链接器的命令行中删除特定的“.lib”文件。 我怎样才能编辑它? - Visual Studio - Cannot remove specific ".lib" file from Linker's Command Line. How can I edit it? Visual Studio C ++ LNK1104:无法从链接器中删除依赖项,无法打开文件pthreadsVC2.lib - Visual Studio C++ LNK1104: Cannot open file pthreadsVC2.lib, despite removing dependency from linker 尝试从Visual Studio命令提示符处调用lib.exe - Trying to call lib.exe from Visual Studio Command Prompt 错误LNK1104:无法打开文件'DFOR.lib'Visual Studio - Error LNK1104: cannot open file 'DFOR.lib' Visual Studio Visual Studio 2012 - 错误LNK1104:无法打开文件'glew32.lib' - Visual Studio 2012 - error LNK1104: cannot open file 'glew32.lib' 致命错误 LNK1104:无法在 Visual Studio 2019 中打开文件“kernel32.lib” - fatal error LNK1104: cannot open file 'kernel32.lib' in Visual Studio 2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM