C++ dll 导出未定义
C++ dll export undefined
我正在尝试创建一个可以在 c# 中导入的 C++ DLL,但我的代码无法编译。
代码如下:
//main.cpp
#include "C:\Users\Mihai\Desktop\body.cpp"
#include "C:\Users\Mihai\Desktop\header.h"
extern "C"_declspec(dllexport) int sumTwo(int var_x, int var_y)
{
myClass MC(var_x, var_y);
return MC.sumX_Y();
}
//body.cpp
#pragma once
#include "Header.h"
myClass::myClass(int var_x, int var_y)
{
x = var_x;
y = var_y;
}
int myClass::sumX_Y()
{
return x + y;
}
//Header.h
#pragma once
class myClass
{
public:
myClass(int var_x, int var_y);
int sumX_Y();
private:
int x;
int y;
};
/*
错误如下:
Severity Code Description Project File Line Suppression State
Error (active) E0130 expected a '{' SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0040 expected an identifier SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0020 identifier "dllexport" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0020 identifier "var_x" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 6
Error (active) E0020 identifier "var_y" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 6
Error C3690 expected a string literal, but found a user-defined string literal instead SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
Error C2144 syntax error: 'int' should be preceded by ';' SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
这是您的更新代码:
main.cpp:
#include "header.h"
extern "C" __declspec(dllexport) int sumTwo(int var_x, int var_y) {
myClass MC(var_x, var_y);
return MC.sumX_Y();
}
header.h:
#pragma once
class myClass {
public:
myClass(int var_x, int var_y);
int sumX_Y();
private:
int x;
int y;
};
body.cpp:
#include "header.h"
myClass::myClass(int var_x, int var_y) {
x = var_x;
y = var_y;
}
int myClass::sumX_Y() {
return x + y;
}
备注:
- 您的代码存在 2 个主要问题:
- class 定义不完整:header.h 末尾缺少
};
(不知道这是否这不是 copy/paste 问题)
extern "C"_declspec(dllexport)
,正如我在评论中指定的那样
- 除此之外,我还做了一些其他非关键性的更正。请看看你的代码和我的代码之间的差异
输出(构建自VStudio 2015 Community):
1>------ Build started: Project: q47496315, Configuration: Debug Win32 ------
1> main.cpp
1> body.cpp
1> Generating Code...
1> q47496315.vcxproj -> C:\Work\Dev\Whosebug\q47496315\Win32-Debug\q47496315.dll
1> q47496315.vcxproj -> C:\Work\Dev\Whosebug\q47496315\Win32-Debug\q47496315.pdb (Full PDB)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
这也是在 Dependency Walker 中加载的 .dll 的(部分)图像,显示正在导出函数:
诺洛克!
我正在尝试创建一个可以在 c# 中导入的 C++ DLL,但我的代码无法编译。
代码如下:
//main.cpp
#include "C:\Users\Mihai\Desktop\body.cpp"
#include "C:\Users\Mihai\Desktop\header.h"
extern "C"_declspec(dllexport) int sumTwo(int var_x, int var_y)
{
myClass MC(var_x, var_y);
return MC.sumX_Y();
}
//body.cpp
#pragma once
#include "Header.h"
myClass::myClass(int var_x, int var_y)
{
x = var_x;
y = var_y;
}
int myClass::sumX_Y()
{
return x + y;
}
//Header.h
#pragma once
class myClass
{
public:
myClass(int var_x, int var_y);
int sumX_Y();
private:
int x;
int y;
};
/*
错误如下:
Severity Code Description Project File Line Suppression State
Error (active) E0130 expected a '{' SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0040 expected an identifier SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0020 identifier "dllexport" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 4
Error (active) E0020 identifier "var_x" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 6
Error (active) E0020 identifier "var_y" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp 6
Error C3690 expected a string literal, but found a user-defined string literal instead SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
Error C2144 syntax error: 'int' should be preceded by ';' SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp 4
这是您的更新代码:
main.cpp:
#include "header.h"
extern "C" __declspec(dllexport) int sumTwo(int var_x, int var_y) {
myClass MC(var_x, var_y);
return MC.sumX_Y();
}
header.h:
#pragma once
class myClass {
public:
myClass(int var_x, int var_y);
int sumX_Y();
private:
int x;
int y;
};
body.cpp:
#include "header.h"
myClass::myClass(int var_x, int var_y) {
x = var_x;
y = var_y;
}
int myClass::sumX_Y() {
return x + y;
}
备注:
- 您的代码存在 2 个主要问题:
- class 定义不完整:header.h 末尾缺少
};
(不知道这是否这不是 copy/paste 问题) extern "C"_declspec(dllexport)
,正如我在评论中指定的那样
- class 定义不完整:header.h 末尾缺少
- 除此之外,我还做了一些其他非关键性的更正。请看看你的代码和我的代码之间的差异
输出(构建自VStudio 2015 Community):
1>------ Build started: Project: q47496315, Configuration: Debug Win32 ------ 1> main.cpp 1> body.cpp 1> Generating Code... 1> q47496315.vcxproj -> C:\Work\Dev\Whosebug\q47496315\Win32-Debug\q47496315.dll 1> q47496315.vcxproj -> C:\Work\Dev\Whosebug\q47496315\Win32-Debug\q47496315.pdb (Full PDB) ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
这也是在 Dependency Walker 中加载的 .dll 的(部分)图像,显示正在导出函数:
诺洛克!