Compile a DLL in C/C++, then call it from another program

前端 未结 5 1173
死守一世寂寞
死守一世寂寞 2020-11-27 09:12

I want to make a simple, simple DLL which exports one or two functions, then try to call it from another program... Everywhere I\'ve looked so far, is for complicated matter

5条回答
  •  无人及你
    2020-11-27 10:03

    Regarding building a DLL using MinGW, here are some very brief instructions.

    First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)

    __declspec( dllexport ) int add2(int num){
       return num + 2;
    }
    

    then, assuming your functions are in a file called funcs.c, you can compile them:

    gcc -shared -o mylib.dll funcs.c
    

    The -shared flag tells gcc to create a DLL.

    To check if the DLL has actually exported the functions, get hold of the free Dependency Walker tool and use it to examine the DLL.

    For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.

    Edit: For more details on this subject, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.

提交回复
热议问题