How to link a Dll

  Kiến thức lập trình

How can i link a dll? I use windows and minGW

this is the content of the folder:

dll.dll
main.cpp

the a main.dll is compiled with:

g++ -c dll.cpp
g++ --shared -o dll.dll dll.o

The content of dll.cpp:

#include <iostream>
#include <windows.h>

using namespace std;

__declspec (dllexport) void sayHi (void) {
      cout << "Hi" << endl;
}

I want to access the “sayHi” function in the main.cpp file with

#include <iostream>
#include <windows.h>

using namespace std;

__declspec(dllimport) void sayHi(void);

int main () {
     sayHi();
}

i tried with

g++ main.cpp -ldll

but it just says:

main.cpp:(.text+0xc): undefined reference to `_imp___Z8sayHiv'
collect2.exe: error: ld returned 1 exit status

can you help me?

2

You need to specify the path where the linker should look for when looking for libraries.

Here, you can do so by:

g++ -o main.exe main.cpp -L. -ldll
  • -L flag tells the linker where to look for library files.
  • -L. tells the compiler to look for library files in the current directory.

0

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT