解决尝试从C++/CLI调用非托管C++时的错误

Solving errors when trying to call unmanaged C++ from C++/CLI

本文关键字:C++ 错误 CLI 解决 调用      更新时间:2023-10-16

我有一些本机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