解决尝试从C++/CLI调用非托管C++时的错误
Solving errors when trying to call unmanaged C++ from C++/CLI
我有一些本机C++代码,我想在C#中使用,经过一些研究,我决定为本机代码创建一个C++/CLI包装器。到目前为止,一切都很好。当运行包含C++/CLI类的程序时,它工作得很好,但当我试图创建一个库,为我提供使用C#中本机代码功能的机会时,我会遇到上述错误(LNK2028和LNK2019)。尽管我已经尝试了链接器和编译器的几种配置,但我仍然无法消除这些错误。
这是工作包装器的代码:
//Wrapper_src.h
#include <cstdio>
#include "Project_Manager.h"
using namespace System;
public ref class WrapperParabola
{
private:
ProjectManager *mainPM; //ProjectManager is the main class from the native code
public:
WrapperParabola()
{
mainPM = new ProjectManager;
}
void InitSphereInfo(float R, float Cx, float Cy, float Cz, float H0, float H1, float dH, int segm)
{
mainPM->Init_SphereInfo(R, Cx, Cy, Cz, H0, H1, dH, segm);
}
void InitParableInfo(float P, float Refl, int Lsegm, int HSegm, float Lng)
{
mainPM->Init_ParableInfo(P, Refl, Lsegm, HSegm, Lng);
}
void InitRaysSetUpInfo(float Ora0, float Ora1, float dOra, int GridN, int NRays)
{
mainPM->Init_RaysSetUpInfo(Ora0, Ora1, dOra, GridN, NRays);
}
void InitSTPInfo(int zi, int luna, float LongLegala, float LongLoc, float Latitudine, float OraLegala, float UnghiInclinare, float UnghiAbatere)
{
mainPM->Init_STPInfo(zi, luna, LongLegala, LongLoc, Latitudine, OraLegala, UnghiInclinare, UnghiAbatere);
}
void ProcessData(void)
{
mainPM->ProcessData();
}
void PrintOutputData(void)
{
mainPM->PrintOutputData();
}
~WrapperParabola()
{
this->!WrapperParabola();
}
!WrapperParabola()
{
delete mainPM;
}
};
int main(void)
{
/*********************Test**********************/
WrapperParabola^ wrapperP = gcnew WrapperParabola;
//Init input data
wrapperP->InitSphereInfo(0, 0, 0, 0, 0, 0, 0, 0);
wrapperP->InitParableInfo(0, 0, 0, 0, 0);
wrapperP->InitRaysSetUpInfo(0, 0, 0, 0, 0);
wrapperP->InitSTPInfo(0, 0, 0, 0, 0, 0, 0, 0);
//Process read data
wrapperP->ProcessData();
//Print the results
wrapperP->PrintOutputData();
Console::WriteLine("Data Processed Successfully!");
std::getchar();
return 0;
}
这是我试图构建的图书馆的代码:
// CppCodeManagerLib.h
#pragma once
#include "Project_Manager.h"
using namespace System;
namespace CppCodeManagerLib {
public ref class CodeManager
{
private:
ProjectManager *mainPM;
public:
CodeManager()
{
mainPM = new ProjectManager;
}
void InitSphereInfo(float R, float Cx, float Cy, float Cz, float H0, float H1, float dH, int segm)
{
mainPM->Init_SphereInfo(R, Cx, Cy, Cz, H0, H1, dH, segm);
}
/*...*/
};
}
正如我所说,为了使代码在C#中可用,我对必须对代码(对本机代码或托管代码)或提供给链接器和编译器的命令和限制进行的更改很感兴趣。
p.S:完整的错误是:
Error 2 error LNK2028:unresolved token (0A00003A) "public: void __thiscall ProjectManager::Init_SphereInfo(float,float,float,float,float,float,float,int)"
(?Init_SphereInfo@ProjectManager@@$$FQAEXMMMMMMMH@Z) referenced in function "public: void __clrcall CppCodeManagerLib::CodeManager::InitSphereInfo(float,float,float,float,float,float,float,int)"
(?InitSphereInfo@CodeManager@CppCodeManagerLib@@$$FQ$AAMXMMMMMMMH@Z) E:Documents and SettingszalmanDesktopCppCodeManagerLibCppCodeManagerLibCppCodeManagerLib.obj CppCodeManagerLib
和
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall ProjectManager::Init_SphereInfo(float,float,float,float,float,float,float,int)"
(?Init_SphereInfo@ProjectManager@@$$FQAEXMMMMMMMH@Z) referenced in function "public: void __clrcall CppCodeManagerLib::CodeManager::InitSphereInfo(float,float,float,float,float,float,float,int)"
(?InitSphereInfo@CodeManager@CppCodeManagerLib@@$$FQ$AAMXMMMMMMMH@Z) E:Documents and SettingszalmanDesktopCppCodeManagerLibCppCodeManagerLibCppCodeManagerLib.obj CppCodeManagerLib
谢谢!
根据LNK2028的MSDN页面,这是由于指定(或默认)了不匹配的调用约定。http://msdn.microsoft.com/en-us/library/ms235590%28v=vs.80%29.aspx
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'