از __declspec(dllexport و __declspec(dllimport استفاده کن
مثلا فرض کن dll دو تا تا فایل داره test.h test.c
test.h
#ifndef TEST_H
#define TEST_H
#ifdef EXPORTING
#define TEST_DLL __declspec(dllexport)
#else
#define TEST_DLL __declspec(dllexport)
#endif
int TEST_DLL foo();
#endif // TEST_H
test.c
#include "test.h"
int foo()
{
return 1;
}
حالا برای بیلد کردن از این 2 دستور استفاده کن :
gcc -c -DEXPORTING test.c
gcc -shared -o test.dll test.o
دستور اول بعد از define کردن EXPORTING میاد object file رو میسازه
دستور دوم هم برای ساخت dll استفاده میشه .
اگر بخوای فایل .a رو هم داشته باشی که بتونی بعدا داخل vs هم از dll استفاده کنی با این دستور
gcc -shared -o test.dll test.o -Wl,--out-implib,libtest.a
الان فرضا اگر بخوای dll رو داخل main.c استفاده کنی :
#include "test.h"
#include <stdio.h>
int main()
{
int a=foo();
printf("%d",a);
return 1;
}
با این 2 کد main.c رو کامپایل می کنیم :
gcc -c main.c
gcc -o main.exe main.o test.dll
برای build کردن به شکل static ;
gcc -c main.c
gcc -static main.o -L. -ltest -o main.exe