[Solved] C++ dll export undefined


Here’s your updated code:

main.cpp:

#include "header.h"

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

header.h:

#pragma once

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

body.cpp:

#include "header.h"

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

int myClass::sumX_Y() {
    return x + 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

Output (build from VStudio 2015 Community):

1>------ Build started: Project: q47496315, Configuration: Debug Win32 ------
1>  main.cpp
1>  body.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:

enter image description here

Noroc!

2

solved C++ dll export undefined