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

时间:2012-02-21 15:54:45

标签: c# c++ visual-studio-2010 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 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

谢谢!

1 个答案:

答案 0 :(得分:0)

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