如何解决错误LNK2019:无法解析的外部符号-函数? [英] How can I solve the error LNK2019: unresolved external symbol - function?

查看:82
本文介绍了如何解决错误LNK2019:无法解析的外部符号-函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在使用Visual Studio2013.我将解决方案名称命名为 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 ++ 测试本地单元测试项目

名称: 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?

推荐答案

一种选择是在 UnitTest1 项目中包含 function.cpp ,但这可能不是最理想的解决方案结构.对您的问题的简短答案是,在构建 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 项目中,可以转到其属性,然后在其他库目录"类别中的链接器"选项卡下,添加构建 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.

这篇关于如何解决错误LNK2019:无法解析的外部符号-函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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