One, write code
This program has 3 files, namely export.h, export.c, main.c
export.h file content
/*此头很有必要,别人在调用的时候知道有哪些方法*/
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
EXPORT void export(void);
export.c file content
#include<stdio.h>
#include "export.h"
void say_hello(void)
{
printf("Hello DLL\n");
}
Content of main.c file (if only compiling DLL file, this file is not required to participate)
#include "export.h"
int main()
{
export();
return 0;
}
Second, use MinGW to compile the dll
Open the command line interface of windows to execute
gcc -shared -o export.dll export.c
At this point, the DLL has been compiled and the export.dll file is generated in the current working directory, which can be called by other programs.
Three, call the dll in the program
Execute in the command line
gcc -o main main.c -L./ -lexport
Where -L indicates the directory of the link library, and -l (lowercase L) indicates the name of the link library.
At this time, the executable file main.exe has been generated. After the execution, it is found that the call is successful, and the Hello DLL is output.
Fourth, other language calls
python call
from ctypes import *
#dll = CDLL("export.dll")
dll = windll.LoadLibrary("export.dll")
dll.say_hello()
See also: The difference between cdll and windll
java call
JNI 、 JNA 、 JNative
See also: JNI uses Java to call C programs