Search code examples
c++dllexport

C++ dll export undefined


I am trying to create a C++ DLL that I can import in c#, but my code won't compile.

Here is the code:

//main.cpp
#include "C:\Users\Mihai\Desktop\body.cpp"
#include "C:\Users\Mihai\Desktop\header.h"

extern "C"_declspec(dllexport) int sumTwo(int var_x, int var_y)
{
    myClass MC(var_x, var_y);
    return MC.sumX_Y();

}

//body.cpp
#pragma once
#include "Header.h"

myClass::myClass(int var_x, int var_y)
{
    x = var_x;
    y = var_y;
}

int myClass::sumX_Y()
{
    return x + y;
}

//Header.h
#pragma once

class myClass
{
public:
    myClass(int var_x, int var_y);
    int sumX_Y();
private:
    int x;
    int y;

};

/*

Here are the errors:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0130   expected a '{'  SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp   4  
Error (active)  E0040   expected an identifier  SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp   4  
Error (active)  E0020   identifier "dllexport" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp   4  
Error (active)  E0020   identifier "var_x" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp   6  
Error (active)  E0020   identifier "var_y" is undefined SumadllptCsharp e:\work\Info\SumadllptCsharp\SumadllptCsharp\main.cpp   6  
Error   C3690   expected a string literal, but found a user-defined string literal instead  SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp   4  
Error   C2144   syntax error: 'int' should be preceded by ';'   SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp   4  
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    SumadllptCsharp e:\work\info\sumadllptcsharp\sumadllptcsharp\main.cpp   4  

Solution

  • Here's your updated code:

    • DummyClass.h

      #pragma once
      
      
      class DummyClass {
        public:
          DummyClass(int var_x, int var_y);
          int sumX_Y();
      
        private:
          int x;
          int y;
      };
      
    • DummyClass.cpp

      #include "DummyClass.h"
      
      
      DummyClass::DummyClass(int var_x, int var_y)
      {
          x = var_x;
          y = var_y;
      }
      
      
      int DummyClass::sumX_Y()
      {
          return x + y;
      }
      
    • header.h:

      #pragma once
      
      #if defined(_WIN32)
      #  if defined(Q047496315_STATIC)
      #    define Q047496315_EXPORT_API
      #  else
      #    if defined(Q047496315_EXPORTS)
      #      define Q047496315_EXPORT_API __declspec(dllexport)
      #    else
      #      define Q047496315_EXPORT_API __declspec(dllimport)
      #    endif
      #  endif
      #else
      #  define Q047496315_EXPORT_API
      #endif
      
      #if defined(__cplusplus)
      extern "C" {
      #endif
      
      Q047496315_EXPORT_API int sumTwo(int var_x, int var_y);
      
      #if defined(__cplusplus)
      }
      #endif
      
    • main.cpp

      #define Q047496315_EXPORTS
      #include "header.h"
      #include "DummyClass.h"
      
      
      int sumTwo(int var_x, int var_y)
      {
          DummyClass dc(var_x, var_y);
          return dc.sumX_Y();
      }
      

    Notes:

    • There were 2 major problems with your code:

      • The class definition wasn't complete: missing }; at the end of header.h (don't know whether this isn't a copy/paste issue)

      • The extern "C"_declspec(dllexport), as I specified in my comment

    • Other than that, I did some other non critical corrections. Please take a look at the differences between your code and mine

      • Rename the class: personally I hate stuff names starting with My, it tells me that their purpose is not clear in creators' mind

      • Reorganize the files:

        • The class is in its own 2 files now

        • split main.cpp in 2 (and use header.h as one of them) in order for the project to be usable from C / C++ (although you said you only need it from C#)

    Output (build from VStudio 2015 Community):

    1>------ Build started: Project: q47496315, Configuration: Debug Win32 ------
    1>  main.cpp
    1>  DummyClass.cpp
    1>  Generating Code...
    1>  q47496315.vcxproj -> C:\Work\Dev\StackOverflow\q47496315\Win32-Debug\q47496315.dll
    1>  q47496315.vcxproj -> C:\Work\Dev\StackOverflow\q47496315\Win32-Debug\q47496315.pdb (Full PDB)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    

    Here's also a (partial) image of the .dll loaded in Dependency Walker, showing that the function is being exported:

    img0

    References (that might be useful):

    Noroc!