Hi,
The format .lib are compiled with visual studio: you can't use it simply in g++.
Below is my test sample and I build the dll in x86:
MSVC DLL:
#pragma once
#include <iostream>
#ifdef MSVC_EXPORTS
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
extern "C" API void HelloWorld();
cpp:
#include"Header.h"
void HelloWorld()
{
std::cout << "Hello, World!" << std::endl;
}
main.cpp
#include <windows.h>
#include <iostream>
#include"../.../Header.h"
typedef void (*HelloWorldFunc)();
int main() {
HMODULE hDll = LoadLibraryA("mylib.dll");//Put the dll and cpp in the same folder
if (hDll != NULL) {
HelloWorldFunc helloWorld = reinterpret_cast<HelloWorldFunc>(GetProcAddress(hDll, "HelloWorld"));
if (helloWorld != nullptr) {
helloWorld();
}
else {
std::cout << "Failed to retrieve function address." << std::endl;
}
getchar();
FreeLibrary(hDll);
}
else {
std::cout << "Failed to load the DLL." << std::endl;
}
return 0;
}
And g++ command:g++ main.cpp
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.