C++ dll export undefined

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

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   

回答1:

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:

Noroc!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!