尝试测试dll时出现LNK2019错误

LNK2019 error when trying to test a dll

本文关键字:LNK2019 错误 测试 dll      更新时间:2023-10-16

我使用一个应用程序(几周前才开始,所以我还在学习旧的应用程序)用C语言构建,我的公司希望使用该程序的能力来调用外部DLL来扩展一些新功能。为此,我开始编写POC,即下面的前两个文件。我们得到的唯一规范是dll必须导出以下函数:extern int __stdcall TestMethod_LoadCustomer(const char * name, char * id);我试着这样实现:

TestDLL.h

#define TestDLL_API __declspec(dllexport)
namespace TestDLL
{
        class TestDLL
        {
        public:
                static TestDLL_API int TestMethod_LoadCustomer(const char* name, char* id);
        };
}

TestDLL.cpp

// TestDLL.cpp : Defines the exported functions for the DLL application.
//  
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
extern "C" int __declspec(dllexport) __stdcall  TestMethod_LoadCustomer(const char* name, char* id)
{
  if (strlen(name) <= 8) {
    strcpy(id, name);           // name contains customer id
  } else {
    id[0] = 0;                  // Customer not found
  }
  return 0;
}

这两个文件编译得很好。当我试图通过一个单独的小控制台应用程序测试这个dll时,问题出现了:RunTEST.cpp

// RunTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "TestDLL.h"
using namespace std;
int _tmain()
{
        char* id= "";
        TestDLL::TestDLL::TestMethod_LoadCustomer("77777", id);
        cout << id;
        cin >> id;
        return 0;
}

所有我正在寻找的是能够在一个字符串传入调用TestMethod_LoadCustomer(),并有它被添加到id字段。

当我实际尝试构建此解决方案时,我得到以下错误:

"error LNK2019: unresolved external symbol "public: static int __cdecl TestDLL::TestDLL::TestMethod_LoadCustomer(char const *, char *)"(?TestMethod_LoadCustomer@TestDLL@1@SAHPBDAD@ z)引用函数_wmain"

我假设它与我试图在客户端应用程序中引用它的方式有关,但我不确定。我看过StackOverflow上的其他LNK2019错误,但这些解决方案似乎都不起作用,因为我错误地实现了它们。有谁能帮我去掉这个错误信息吗?

关于TestDLL.cpp文件缺少两件事:

1)命名空间TestDLL.

2) TestDLL::在方法名之前。

// TestDLL.cpp : Defines the exported functions for the DLL application.
//  
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
namespace TestDLL {
extern "C" int __declspec(dllexport) __stdcall  TestDLL::TestMethod_LoadCustomer(const char* name, char* id)
{
  if (strlen(name) <= 8) {
    strcpy(id, name);           // name contains customer id
  } else {
    id[0] = 0;                  // Customer not found
  }
  return 0;
}
}

在TestDLL.cpp中定义函数时,您没有提到该函数是类TestDLL的成员。