如何解决错误LNK2019:未解决的外部符号 - 功能? [英] How to solve the error LNK2019: unresolved external symbol - function?

查看:174
本文介绍了如何解决错误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 in the internet and became interested in the unit test, so I created a test project:

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

名称: UnitTest1
解决方案: 添加到解决方案
然后位置自动切换到当前打开的解决方案的路径
这是解决方案的文件夹结构:

Name: UnitTest1 Solution: Add to solution Then the location auto-switched to the path of the current open solution This is the folder structure of the solution:

我只有编辑的文件unittest1.cpp:

I only edited file 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:未解决的外部符号
我知道它缺少功能 multiple 的实现。
我试图删除文件function.cpp并用定义替换声明,并运行它。但是不建议将声明和定义写入同一文件。
如何解决这个错误呢?应该在文件unittest.cpp中替换为 #include../ MyProjectTest / function.cpp
(我不是非常擅长英文,谢谢)

But I get error LNK2019: unresolved external symbol I know that it is missing the implementation of function multiple. I tried to delete file function.cpp and replace the declaration with the definition, and it run. But writing both declaration and definition in the same file is not recommended. How can I fix this error without doing that? Should I replace with #include "../MyProjectTest/function.cpp" in file unittest.cpp? (I'm not good at English very much. Thanks)

推荐答案

选项将在 UnitTest1 项目中包含 function.cpp ,但这可能不是最理想的解决方案结构。您的问题的简短答案是,当构建您的 UnitTest1 项目时,编译器和链接器不知道 function.cpp 存在,也没有任何链接,其中包含多个的定义。一个解决这个问题的方法是利用链接库。

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 builds。然后,对于链接器 - 输入选项卡下的附加依赖关系,添加静态库的名称,最有可能 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天全站免登陆