函数定义在cpp文件中时链接器错误

问题描述:

我的解决方案有三个项目:GoogleTest(用于使用Google Test),Vi(用于大部分逻辑)和ViTests(用于使用Vi的单元测试). ViTests项目引用了Vi项目和Google Test项目.

My solution has three projects: GoogleTest (for using Google Test), Vi (for the bulk of the logic) and ViTests (for the unit tests using Vi). The ViTests project references the Vi project and the Google Test project.

Vi在v1.h中具有以下代码

Vi has the following code in v1.h

#pragma once

namespace Vi
{
    class Vi1
    {
    public:
        int SomeInt();
    };
}

和匹配的v1.cpp

And the matching v1.cpp

#include "vi1.h"

namespace Vi
{
    int Vi1::SomeInt()
    {
        return 123;
    }
}

随后是ViTests中的测试功能

The test function in ViTests follows

TEST(Vi1Foo, SomeIntIsSame)
{
    Vi1 v = Vi1{};
    EXPECT_EQ(123, v.SomeInt());
}

链接器错误表明存在未解析的符号SomeInt.但是,我可以通过如下内联函数来使链接器错误消失:

The linker error says there's an unresolved symbol SomeInt. However, I can make the linker error go away by inlining the function like so:

namespace Vi
{
    class Vi1
    {
    public:
        int SomeInt() { return 123; }
    };
}

为什么单元测试项目放在单独的cpp文件中时找不到SomeInt函数定义?

Why is the unit test project not finding the SomeInt function definition when it's placed in a separate cpp file?

谢谢.

其他详细信息以防万一:我正在使用Visual Studio 2015.

Extra details incase useful: I'm using Visual Studio 2015.

错误消息:

Error   LNK2019 unresolved external symbol "public: int __thiscall Vi::Vi1::SomeInt(void)" (?SomeInt@Vi1@Vi@@QAEHXZ) referenced in function "private: virtual void __thiscall ViTests::Vi1Foo_SomeIntIsSame_Test::TestBody(void)" (?TestBody@Vi1Foo_SomeIntIsSame_Test@ViTests@@EAEXXZ) Vi_Tests    C:\Users\MyName\Vi\Vi_Tests\Vi_Tests.obj    1

项目类型: Vi是Win32应用程序,ViTests是Win32控制台应用程序,GoogleTest是静态库.

Project types: Vi is Win32 Application, ViTests is Win32 Console Application, GoogleTest is a static library.

我在一个愚蠢的错误假设下工作.我以为VS只会提取cpp文件,因为我正在引用包含它们的项目并包括正确的头文件,但这不是事实.

I was working under a silly false assumption. I was assuming that VS would just pickup the cpp files because I was referencing the project containing them and including the correct header files but this isn't the case.

nabijaczleweli说我应该链接指向具有功能定义的文件".在网上进行了一些探索之后,我发现了如何做到这一点.我发现帖子概述了解决方案. cpp文件本身必须添加到测试项目中.右键单击ViTests,选择添加现有文件",然后选择vi1.cpp.

nabijaczleweli said I should link "towards the file with the function definition". After a bit of exploring online I found out how to do this. I found this post outlining a solution. The cpp files themselves must be added to the test project. This was done by right-clicking on ViTests, selecting "Add Existing Files" and selecting vi1.cpp.

这可行,但是我对此解决方案感到非常不满意,因为我必须根据需要添加这些文件.我发现我可以解决将Vi的项目类型更改为静态库的问题.在此处进行说明.迈克尔·伯尔(Michael Burr)的评论描述了一种链接到也将起作用的目标文件的方法.

This worked but I was quite unhappy with this solution because I had to add these files as I needed them. I found I could get around this changing the type of project Vi was to a static library. This is described here. Michael Burr's comment describes a way of linking to the object files that would also work.

没有这个线程中的发布者的帮助,我不会这么快就解决这个问题.非常感谢您.

I wouldn't have figured this out so quickly without the help of the posters in this thread. Thank you very much people.