1

重複の可能性:
C++ クラスとそのメンバー関数の名前マングリング?

Visual C++ dll を作成しました。それは機能しており、c# でこの dll を介して cuda から Thrust メソッドを呼び出すことができます。

唯一の問題は、関数名を解読できないことです。通常の名前にしたいので、規則でエントリポイントを使用する必要はありません。

これが私のコードです。これは私のヘッダーです

//ThrustCH.h
    #pragma once
    enter code here__declspec(dllexport) class ThrustFuncs 
    {
        __declspec(dllexport) static int maxValueThrust(int *data, int N);
        __declspec(dllexport) static double maxValueThrust(double *data, int N);
        __declspec(dllexport) static int* sort(int* data, int N);
        __declspec(dllexport) static double* sort(double* data, int N);
        __declspec(dllexport) static int simple(int N);
    };

これは私のcppです

// thrustDLL.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "thrustH.h"
#include "thrustCH.h"

extern "C" {
    __declspec(dllexport) int ThrustFuncs::maxValueThrust(int *data, int N){
        return thrustH::maxValue(data,N);
    }

    __declspec(dllexport) double ThrustFuncs::maxValueThrust(double *data, int N){
        return thrustH::maxValue(data,N);
    }

    __declspec(dllexport) int* ThrustFuncs::sort(int* data, int N){
        return thrustH::sort(data,N);
    }
    __declspec(dllexport) double* ThrustFuncs::sort(double* data, int N){
        return thrustH::sort(data,N);
    }
    __declspec(dllexport) int ThrustFuncs::simple(int N){
        return N;
    }
}

extern "C" と __declspec(dllexport) をほぼどこでも使用しようとしましたが、何か間違っていると思います。私を手伝ってくれますか?

4

2 に答える 2

3

C++ 関数をエクスポートしようとしているようですが、それらに C 名を付けたいと考えています。

主に意味がないため、これを行う直接的な方法はありません。

Cにはクラス (または名前空間) がなく、それらは通常、C++ の名前マングリングに関与しています。つまり、宣言Cで名前装飾を使用してエクスポートする関数を記述しないでください。class

extern "C"ただし、 C++ 関数、メソッド、またはクラスを呼び出すC 関数を (ブロック内に) 記述することはできます。

何かのようなもの:

class foo
{
  static int bar(const std::string& str) { return static_cast<int>(str.size()); }
}

extern "C"
{
  int bar(const char* str)
  {
    // Call C++ version of the function.
    try
    {
      return foo::bar(str);
    }
    catch (std::exception&)
    {
      // Handle it somehow
    }
  }
}
于 2012-08-03T14:49:25.117 に答える
0

__cdecl名前を壊さないエクスポートしたい関数に使用したい場合があります。

参照: /Gd、/Gr、/Gz (呼び出し規約)

于 2012-08-03T14:49:12.417 に答える