解决尝试调用非托管 C++ 时出现的错误来自 C++/CLI

发布于 2025-01-07 12:17:37 字数 3959 浏览 1 评论 0原文

我有一些本机 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# 中使用。

PS:完整的错误是:

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 Settings\zalman\Desktop\CppCodeManagerLib\CppCodeManagerLib\CppCodeManagerLib.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 Settings\zalman\Desktop\CppCodeManagerLib\CppCodeManagerLib\CppCodeManagerLib.obj  CppCodeManagerLib

I have some native C++ code that I want to use in C# and after doing some research I decided to create a C++/CLI wrapper for the native code. So far, so good. When running the program that contains the C++/CLI class, it works perfectly, but when I try to create the library that will provide me the chance of using the functionality of the native code in C#, I get the aforementioned errors (LNK2028 and LNK2019). Although I have tried several configurations for the linker and the compiler, I still cannot manage to get rid of these errors.

This is the code for the working wrapper:

//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;
}

And this is the code for the Library that I am trying to build:

// 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);
    }

    /*...*/
};
}

As I said, I am interested in the changes that I have to make to the code (either to the native one or to the managed one) or to the commands and restrictions provided to the linker and to the compiler, in order to make the code usable in C#.

P.S: The complete errors are:

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 Settings\zalman\Desktop\CppCodeManagerLib\CppCodeManagerLib\CppCodeManagerLib.obj CppCodeManagerLib

and

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 Settings\zalman\Desktop\CppCodeManagerLib\CppCodeManagerLib\CppCodeManagerLib.obj  CppCodeManagerLib

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2025-01-14 12:17:37

根据 LNK2028 的 MSDN 页面,这是由于指定(或默认)的调用约定不匹配。 http://msdn.microsoft.com/en -us/library/ms235590%28v=vs.80%29.aspx

According to the MSDN page for LNK2028, it's due to having mismatched calling conventions specified (or defaulted). http://msdn.microsoft.com/en-us/library/ms235590%28v=vs.80%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文