如何解决错误 LNK2019:未解析的外部符号 - 函数?

2021-12-15 00:00:00 testing error-handling c++ lnk2019

我收到此错误,但我不知道如何解决.

I get this error, but I don't know how to fix it.

我使用的是 Visual Studio 2013.我将解决方案命名为 MyProjectTest这是我的测试解决方案的结构:

I'm using Visual Studio 2013. I made the solution name MyProjectTest This is the structure of my test solution:

-function.h

#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H

int multiple(int x, int y);
#endif

-function.cpp

#include "function.h"

int multiple(int x, int y){
    return x*y;
}

-main.cpp

#include <iostream>
#include <cstdlib>
#include "function.h"

using namespace std;

int main(){
    int a, b;
    cin >> a >> b;
    cout << multiple(a, b) << endl;

    system("pause");
    return 0;
}

我是初学者;这是一个简单的程序,它运行没有错误.网上看了看,对单元测试产生了兴趣,于是创建了一个测试项目:

I'm a beginner; this is a simple program and it runs without error. I read on the Internet and became interested in the unit test, so I created a test project:

菜单文件 → 新建 → 项目... → 已安装 → 模板 → Visual C++ → 测试 → 本机单元测试项目 →

Menu File → New → Project... → Installed → Templates → Visual C++ → Test → Native Unit Test Project →

名称:UnitTest1
解决方案:添加到解决方案

然后位置自动切换到当前打开解决方案的路径.

Then the location auto-switched to the path of the current open solution.

这是解决方案的文件夹结构:

This is the folder structure of the solution:

我只编辑了文件unittest1.cpp:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "../MyProjectTest/function.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{
    TEST_CLASS(UnitTest1)
    {
    public:

        TEST_METHOD(TestEqual)
        {
            Assert::AreEqual(multiple(2, 3), 6);
            // TODO: Your test code here
        }

    };
}

但我明白了:

错误 LNK2019:未解析的外部符号.

error LNK2019: unresolved external symbol.

我知道缺少函数 multiple 的实现.我试图删除 function.cpp 文件,我用定义替换了声明,它运行了.但是不建议将声明和定义写在同一个文件中.

I know that the implementation of function multiple is missing. I tried to delete the function.cpp file and I replaced the declaration with the definition, and it ran. But writing both declaration and definition in the same file is not recommended.

如何在不这样做的情况下修复此错误?我应该用文件 unittest.cpp 中的 #include "../MyProjectTest/function.cpp" 替换它吗?

How can I fix this error without doing that? Should I replace it with #include "../MyProjectTest/function.cpp" in file unittest.cpp?

推荐答案

一种选择是将 function.cpp 包含在您的 UnitTest1 项目中,但这可能不是最理想的解决方案结构.对您的问题的简短回答是,在构建 UnitTest1 项目时,编译器和链接器不知道 function.cpp 存在,并且也没有任何链接包含multiple 的定义.解决此问题的一种方法是使用链接库.

One option would be to include function.cpp in your UnitTest1 project, but that may not be the most ideal solution structure. The short answer to your problem is that when building your UnitTest1 project, the compiler and linker have no idea that function.cpp exists, and also have nothing to link that contains a definition of multiple. A way to fix this is making use of linking libraries.

由于您的单元测试在不同的项目中,我假设您的意图是使该项目成为一个独立的单元测试程序.如果您正在测试的函数位于另一个项目中,则可以将该项目构建为动态或静态链接的库.静态库在构建时链接到其他程序,扩展名为.lib,动态库在运行时链接,扩展名为.dll.对于我的回答,我更喜欢静态库.

Since your unit tests are in a different project, I'm assuming your intention is to make that project a standalone unit-testing program. With the functions you are testing located in another project, it's possible to build that project to either a dynamically or statically linked library. Static libraries are linked to other programs at build time, and have the extension .lib, and dynamic libraries are linked at runtime, and have the extension .dll. For my answer I'll prefer static libraries.

您可以通过在项目属性中更改您的第一个程序来将其转换为静态库.在常规"选项卡下应该有一个选项,将项目设置为生成可执行文件 (.exe).您可以将其更改为 .lib..lib 文件将构建到与 .exe 相同的位置.

You can turn your first program into a static library by changing it in the projects properties. There should be an option under the General tab where the project is set to build to an executable (.exe). You can change this to .lib. The .lib file will build to the same place as the .exe.

在您的 UnitTest1 项目中,您可以转到其属性,然后在类别 Additional Library Directories 的 Linker 选项卡下,添加 MyProjectTest 构建的路径.然后,对于链接器 - 输入选项卡下的附加依赖项,添加静态库的名称,很可能是 MyProjectTest.lib.

In your UnitTest1 project, you can go to its properties, and under the Linker tab in the category Additional Library Directories, add the path to which MyProjectTest builds. Then, for Additional Dependencies under the Linker - Input tab, add the name of your static library, most likely MyProjectTest.lib.

这应该允许您的项目构建.请注意,这样做后,MyProjectTest 将不会成为独立的可执行程序,除非您根据需要更改其构建属性,这会不太理想.

That should allow your project to build. Note that by doing this, MyProjectTest will not be a standalone executable program unless you change its build properties as needed, which would be less than ideal.

相关文章