FIRST_DLL
g++写dll
1.my_dll.h:
#pragma once
// #ifdef MY_DLL_EXPORTS//vs下
// # define MY_DLL_EXP __declspec(dllexport)
// #else
// # define MY_DLL_EXP __declspec(dllimport)
// #endif
#ifdef __cplusplus
extern "C" {
#endif
class WHYCAN_T {
public:
void hello();
};
#ifdef __cplusplus
}
#endif
2.my_dll.cpp:
#include<iostream>
#include"my_dll.h"
extern "C" {
void WHYCAN_T::hello()
{
std::cout << "Hello,this is my dll ^_^" << std::endl;
}
}
3.resource.cpp:
#include "my_dll.h"
int main()
{
WHYCAN_T helloDll;
helloDll.hello();
return 0;
}
4.MINGW:
MINGW64 ~/Desktop/TEST_DLL
$ ls
my_dll.cpp my_dll.h resource.cpp
MINGW64 ~/Desktop/TEST_DLL
$ g++ --share my_dll.cpp -o my_dll.dll
MINGW64 ~/Desktop/TEST_DLL
$ g++ resource.cpp my_dll.dll -o 111.exe
MINGW64 ~/Desktop/TEST_DLL
$ ./111.exe
Hello,this is my dll ^_^
VS写dll , g++ 调用
1.在DLL项目里的main.cpp
#include <iostream>
#include <windows.h>
extern "C" __declspec(dllexport) void Go() {
std::cout << "go" << std::endl;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
然后生成
2.将mydll.lib , mydll.dll , mydll.exp , mydll.pdb复制到main.cpp当前目录
MINGW64 ~/Desktop/test2
$ ls
main.cpp mydll.dll* mydll.exp mydll.lib mydll.pdb
3.编译执行:
MINGW64 ~/Desktop/test2
$ g++ main.cpp -lmydll -L.
MINGW64 ~/Desktop/test2
$ ls
a.exe* main.cpp mydll.dll* mydll.exp mydll.lib mydll.pdb
MINGW64 ~/Desktop/test2
$ ./a.exe
go

浙公网安备 33010602011771号