googletest测试框架c ++:静态方法链接器错误 [英] googletest testing framework c++: static method linker error

查看:292
本文介绍了googletest测试框架c ++:静态方法链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的TEST调用静态方法,但我得到未解决的外部符号。
到目前为止我已经做了:
创建了3个项目:

I'm trying to call static method from my TEST, but I'm getting unresolved external symbol. What I've done so far: Created 3 projects:


  1. GoogleTest项目作为静态库 - gtest-all.cc和gtest_main.cc

  2. MyProject - 我保存我的.h和.cpp文件

  3. UnitTest项目 -

我设置了UnitTest的其他目录,lib目录和引用的GoogleTest和MyProject。测试运行良好,直到我从我的一个类调用静态方法...

I've set up UnitTest's additional directories, lib directories, and referenced GoogleTest and MyProject. Tests run fine until I call static method from one of my classes...

链接器选项

/OUT:"D:\SkyDrive\Projekti\Visual Studio 2010\Projects\File System\Debug\Unit Test.exe" /INCREMENTAL /NOLOGO "..\lib\part.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" "D:\SkyDrive\Projekti\Visual Studio 2010\Projects\File System\Debug\Google Test.lib" /MANIFEST /ManifestFile:"Debug\Unit Test.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\SkyDrive\Projekti\Visual Studio 2010\Projects\File System\Debug\Unit Test.pdb" /SUBSYSTEM:CONSOLE /PGD:"D:\SkyDrive\Projekti\Visual Studio 2010\Projects\File System\Debug\Unit Test.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE

错误:

error LNK2019: unresolved external symbol "public: static char __cdecl FS::mount(class Partition *)" (?mount@FS@@SADPAVPartition@@@Z) referenced in function "private: virtual void __thiscall Cluster_KernelFS_mountPartition_Test::TestBody(void)" (?TestBody@Cluster_KernelFS_mountPartition_Test@@EAEXXZ)   D:\SkyDrive\Projekti\Vi‌​sual Studio 2010\Projects\File System\Unit Test\cluster.obj


推荐答案

从我们的意见交换中,看起来很清楚你的
googletest单元测试项目$ c>单元测试)不包括项目生成的它应该测试的必要的目标文件( MyProject )。 fs.obj
kernelfs.obj 至少有两个缺失, b $ b失败的链接器命令行似乎你正在链接 的目标文件
实现你想要测试的类或函数(如果有更多)

From our exchange of comments it looks clear that the linkage of your googletest unit-test project (Unit Test) does not include necessary object files generated by the project that it is supposed to test (MyProject). fs.obj and kernelfs.obj are at least two that are missing and from inspection of the failing linker commandline it appears that you are linking none of the object files that implement the classes or functions you want to test (if there are any more)

您似乎相信添加Visual Studio项目引用 MyProject 单元测试会自动向 Unit Test 提供缺少的对象文件依赖,但不会。

You seem to believe that adding a Visual Studio project reference to MyProject to Unit Test will automatically provide the missing object file dependencies to Unit Test, but it won't.

em> 方法来删除链接错误只是将 MyProject 生成的缺少的目标文件添加到单元测试 s项目的配置属性 - > 链接器 - > 输入 - > 其他依赖关系

The quickest way to remove the linkage errors would simply be to add the missing object files generated by MyProject to the Unit Tests project's Configuration Properties -> Linker -> Input -> Additional Dependencies.

您可以通过定位
的完全限定路径名添加每个缺少的 .obj 它在 MyProject Debug 输出目录中。或者更容易,您可以将 Debug 目录的路径添加到配置属性 - > 链接器 - > 输入 - > 其他目录,然后只需将缺少的 .obj 文件的非限定名称添加到其他依赖项

You could add each of the missing .obj files by a fully-qualified pathname that locates it in the Debug output directory of MyProject. Or more easily, you could add the path to that Debug directory to Configuration Properties -> Linker -> Input -> Additional Directories, and then simply add the unqualified name of the missing .obj file to Additional Dependencies.

但这是一个有缺陷的解决方案,因为您当然希望单元测试总是
基于 MyProject 的最新源版本构建,而不仅仅是任何 MyProject object
文件存在的时候。在这种情况下,您有两个选项: -

But this would be a flawed solution because of course you want Unit Test always to build on the latest source version of MyProject and not just whatever MyProject object files happen to exist at the time. In that case you have two options:-


  • a)您可以添加源文件单元测试项目以及 MyProject 生成缺少的 .obj c $ c>。在单元测试解决方案资源管理器视图中,右键点击源文件,然后导航添加 - > 现有项。在这种情况下,你必须小心,每当你添加一个新的源文件到 MyProject ,你也添加到单元测试

  • a) You can add the source files that generate the missing .obj files to the Unit Test project as well as MyProject. In Unit Test's Solution Explorer view, right-click Source Files and navigate Add -> Existing Item. In this case you must take care that whenever your add a new source file to MyProject you also add it to Unit Test.

此选项假定两个项目都在同一个解决方案中。如果他们不是你
可以使他们这样。然后,你可以,首先,执行快速但有缺陷的解决方案(并测试)。然后,为了修复缺陷,您可以在 MyProject 上进行单元测试 每当你建立单元测试 MyProject 将自动首先构建,如果它是过时的。要创建此依赖关系,请右键单击解决方案资源管理器窗格中的单元测试,导航构建依赖关系 - > 项目依赖关系 - > 依赖关系并勾选复选框以使单元测试依赖于 MyProject 。在这种情况下,您必须注意每当由 MyProject 生成一个新的 .obj 文件时, 单元测试的附加链接器依赖性。

b) This option assumes that both projects are in the same solution. If they're not you can make them so. Then you can, first, carry out the quick-but-flawed solution (and test it). Then to fix the flaw, you can make Unit Test dependent on MyProject, so that whenever you build Unit Test, MyProject will automatically be built first if it is out-of-date. To create this dependency, right-click Unit Test in the Solution Explorer pane, navigate Build Dependencies -> Project Dependencies -> Dependencies and tick the checkbox to make Unit Test depend on MyProject. In this case you must take care that whenever a new .obj file is to be generated by MyProject you add it to the additional linker dependencies of Unit Test.

)是更好的做法。

所有上述假设您已正确配置单元测试搜索 MyProject 的include目录并正确定位其头文件。

All of the above assumes that you have correctly configured Unit Test so that the compiler searches the include-directories of MyProject and correctly locates its header files. You don't seem to have any build errors on that score.

跟进Q. 1 b $ b


您可以向我解释为什么链接器不链接来自MyProject的任何对象文件(我必须手动添加它们)。

Could you explain to me why doesn't the linker link any object files from MyProject (and I have to add them manually).

就Visual Studio而言, MyProject 单元测试只是两个项目
包含在构建不同可执行文件的同一个解决方案中。它不知道单元测试的目的是单元测试 MyProject ,因此将需要链接由 MyProject 生成的所有对象文件,其中包含单元测试要测试的功能。因此,为什么自动将 MyProject 的对象文件添加到单元测试的链接?

As far as the Visual Studio is concerned, MyProject and Unit Test are just two projects contained in the same solution that build different executables. It doesn't "know" that the purpose of Unit Test is to unit-test MyProject and therefore will need to link all of the object files generated by MyProject that contain functionality that Unit Test is going to test. So why would it automatically add MyProject's object files to Unit Test's linkage?

即使Visual Studio确实知道单元测试的目的是单元测试 MyProject ,应如何知道<$ c $> MyProject $ c>单元测试?其中一个可能包含 main()函数 - 如果 MyProject 构建控制台应用程序 - 单元测试也会在其对象文件之一中生成一个 main()所以将单元测试中的 MyProject 对象文件链接到将是不可能的: 乘法定义的符号错误。

Even if Visual Studio did somehow know that the purpose of Unit Test was to unit-test MyProject, how would it know which of MyProject's object files it should link in Unit Test? One of them might contain a main() function - which will in fact be the case if MyProject builds a console application - but Unit Test also generates a main() function in one of its object files; so linking all the MyProject object files in Unit Test would be impossible: it would cause a multiply defined symbol error.

我指出给予 c $ c>对我的项目的项目引用没有添加所有我的项目的对象文件到单元测试的链接。你现在看到一个
的原因,为什么它不能有效果。

I pointed out that giving Unit Test a project reference to My Project does not have the effect of adding all My Project's object files to Unit Test's linkage. You now see one reason why it couldn't have that effect.

这个原因举例说明一个更一般的:当你链接对象文件建立可执行文件,它是一个链接错误,同一个符号在多个目标文件中可见。哪一个应该链接?但是,当然,在同一个符号在不同可执行文件的链接中可见时,不会出现任何错误。这意味着你不能简单地从一个可执行文件的链接舀出一堆目标文件,并将它们添加到另一个可执行文件的链接,并期望它可以工作。如果你想从一个可执行文件添加对象文件到另一个可执行文件的链接,你必须知道一些这样的混合可以链接和你想要什么混合。你必须知道哪些符号是从哪些目标文件链接,使第二个可执行文件,并相应地选择
目标文件。正如你所说,这是一个手动的练习。

And that reason exemplifies a more general one: When you link object files to build an executable, it's a linkage error for the same symbol to be visible in multiple object files. Which one is supposed to get linked? But of course it's not any kind of error for the same symbol to be visible in the linkages of different executables. That means you can't simply scoop up a bunch of object files from the linkage of one executable and add them to the linkage of another executable and expect it to work. If you want to add object files from one executable to the linkage of another, you have to know that some such mixture can be linked and what mixture you want. You have to know which symbols are meant to be linked from which object files to make the second executable and select the object files accordingly. As you say, it's a "manual" exercise.

如果 MyProject 生成了,Visual Studio的项目引用功能将帮助您一个可执行文件。在这种情况下,从单元测试 MyProject 创建一个引用会(除其他外)告诉Visual Studio默认情况下 MyProject 库将添加到单元测试的链接。为了与不同的可执行文件链接,与常规对象文件不同的是创建库,因此MS Project Reference特性自然支持库依赖的自动化。如果一个项目生成一个可执行文件,而不是一个库,那么它的目标文件只是一个副产品,并且不打算与其他可执行文件链接。

Visual Studio's Project Reference feature would help you if MyProject generated a library rather than an executable. In that case creating a reference from Unit Test to MyProject would (among other things) tell Visual Studio that by default the MyProject library will be added to the linkage of Unit Test. Libraries, unlike plain object files, are created for the purpose of being being linked with different executables, so it's natural for the MS Project Reference feature to support automation of library dependencies. It's also natural to assume that if a project generates an executable, not a library, then its object files are just a by-product and not intended to be linked with other executables.

组织您的工作的教科书方式将在三个项目中: -

The text-book way of organizing your work would be in three projects:-


  • MyLib ,生成包含您要进行单元测试的所有功能的库。

  • MyProject ,生成与目前相同的可执行文件,但链接 MyLib

  • 单元测试,生成一个单元测试 MyLib 的可执行文件,也链接 MyLib

  • MyLib, generating library that contains all the functionality you want to unit-test.
  • MyProject, generating the same executable as at present, but linking MyLib
  • Unit Test, generating an executable that unit-tests MyLib, also linking MyLib

跟进问卷2


我如何配置链接器链接.objs从MyProject的调试文件夹?

Explain to me how to configure the linker to link .objs from MyProject's debug folder?

我已经有。第4段,或更容易...等。没有办法避免有
指定要链接的个别 .obj 文件。你不能指示链接器只需链接在 / path / to / MyProject / Debug 中找到的任何目标文件,因为我已经解释过,不做那么麻烦的事情。

I already have. Paragraph 4, "Or more easily..." etc. There is no way you can avoid having to specify the individual .obj files to be linked. You can't instruct the linker "Just link whatever object files you find in /path/to/MyProject/Debug", because for the reasons I've explained, the linker doesn't do things as sloppy as that.

这篇关于googletest测试框架c ++:静态方法链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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