【发布时间】:2018-03-01 08:42:14
【问题描述】:
我正在开发一个带有 C 包装器的 C++ DLL,以便在 Python 和 C# 中使用它。所以我在 Visual Studio 上创建了一个项目 (DLL) 来开发和编译它。这里没问题。我什至可以毫无问题地在 Python 上使用我的 DLL。
但是,在 Visual 上,我想在与 DLL 相同的解决方案中创建另一个项目来测试 DLL。
于是我创建了第二个项目(Win32 Windows 应用程序),将 .h 添加到头文件中,将链接添加到我添加的 .lib 文件中测试项目的文件夹,但是当我尝试编译它时,我有关于LNK2019的错误,从构造函数开始:
error LNK2019: unresolved external symbol "public: __cdecl Projet::Projet(void)" (??Projet@@QEAA@XZ) referenced in function main
DLL = Projet / Test = Projet_Test
Projet.h
#pragma once
#include "Projet_inc.h"
class Projet
{
public:
Projet();
~Projet();
int multiply(int arg1, int arg2);
int result;
};
Projet_inc.h
#ifdef PROJET_EXPORTS
# define EXPORT __declspec(dllexport)
#else
# define EXPORT __declspec(dllimport)
#endif
#define CALLCONV_API __stdcall
#ifdef __cplusplus
extern "C" // C wrapper
{
#endif
typedef struct Projet Projet; // make the class opaque to the wrapper
EXPORT Projet* CALLCONV_API cCreateObject(void);
EXPORT int CALLCONV_API cMultiply(Projet* pDLLobject, int arg1, int arg2);
#ifdef __cplusplus
}
#endif
Projet.cpp
#include "stdafx.h"
#include "Projet.h"
Projet::Projet() {}
Projet::~Projet() {}
int Projet::multiply(int arg1, int arg2) {
result = arg1 * arg2;
return result;
}
Projet* EXPORT CALLCONV_API cCreateObject(void)
{
return new Projet();
}
int EXPORT CALLCONV_API cMultiply(Projet* pDLLtest, int arg1, int arg2)
{
if (!pDLLtest)
return 0;
return pDLLtest->multiply(arg1, arg2);
}
Projet_Test.cpp
// Projet_Test.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include "Projet.h"
int main()
{
Projet object;
return 0;
}
在 Visual 上,我选择测试项目作为启动项目以获取信息。我看了很多关于 SO 的帖子,但我现在没有找到解决方案。提前谢谢你。
【问题讨论】:
-
错误消息中引用的符号与您代码中的任何符号都不匹配。这段代码要么不完整,要么是假的。无论哪种方式,通过提供minimal reproducible example 来修复它。