Linking with a dll compiles, but causes a segfault

前端 未结 1 1589
我寻月下人不归
我寻月下人不归 2020-12-22 14:29

I\'ve been working on compiling with Visual Studio 2015 a large number of code files written originally for use on linux, using g++. Specifically, I need a .dll for use with

相关标签:
1条回答
  • 2020-12-22 14:47

    The static data in the dll is in a different address space so you can't directly reference it (the call has to be mapped thru an import table). When you link a static library everything is in the address space of the executable so you can.

    You must use dllexport and dllimport as a pair. dllexport where you define them in the shared library (dll) and dllimport where you use them in the application. You'll typically have a macro that evaluates to either __declspec(dllexport) or __declspec(dllimport) depending on where it's used. E.g.

    #ifdef _DLL     // inside the DLL
      #define DLLExportImport __declspec(dllexport)
    #else          // outside the DLL
       #define DLLExportImport __declspec(dllimport)
    #endif
    

    And use it where you define the class:

    class DLLExportImport FirstClass
    { ... };
    

    Defining the symbol _DLL as appropriate in the respective projects. Visual Studio predefines _DLL when you create a new dll project.

    There is no need for .def files in most cases these days.

    0 讨论(0)
提交回复
热议问题