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

查看:302
本文介绍了如何解决错误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 make 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

-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 the beginner, this is the simple program and it run ok without error. I read in the internet and interested in the unit test, so I creat the test project:

文件>新建>项目...> Installed> Templates> Visual C ++> Test> Native Unit Test Project>

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

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

然后位置auto切换到当前打开解决方案的路径
这是解决方案的文件夹结构:

Name: UnitTest1 Solution: Add to solution then the location auto switch to the path of current opening solution This is the folder structure of the solution:

我仅编辑文件unittest1.cpp:

I edit only 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:unresolved external symbol
我知道它缺少函数 multiple 的实现。
我尝试删除文件function.cpp和替换声明与defination,并运行。但是不推荐在同一个文件中同时声明和定义。
如何解决这个错误,没有这样做。我应该替换为 #include../ MyProjectTest / function.cpp在文件unittest.cpp?
(我不太懂英语非常感谢)

But I get error LNK2019: unresolved external symbol I know that it is missing the implement of function multiple. I try to delete file function.cpp and replace the declare with the defination, and it run. But It is not recommend to write both declare and defination in the same file. How can i fix this error without do 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 存在,并且没有任何链接,包含 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.

您可以通过在项目属性中更改它将第一个程序转换为静态库。在General选项卡下应该有一个选项,其中项目设置为构建为可执行文件( .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天全站免登陆