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:

    Notes:

    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!