Compile a Dll in C/C++, Then Call It from Another Program

Compile a DLL in C/C++, then call it from another program

Regarding building a DLL using MinGW, here are some very brief instructions.

First, you need to mark your functions for export, so they can be used by callers of the DLL. To do this, modify them so they look like (for example)

__declspec( dllexport ) int add2(int num){
return num + 2;
}

then, assuming your functions are in a file called funcs.c, you can compile them:

gcc -shared -o mylib.dll funcs.c

The -shared flag tells gcc to create a DLL.

To check if the DLL has actually exported the functions, get hold of the free Dependency Walker tool and use it to examine the DLL.

For a free IDE which will automate all the flags etc. needed to build DLLs, take a look at the excellent Code::Blocks, which works very well with MinGW.

Edit: For more details on this subject, see the article Creating a MinGW DLL for Use with Visual Basic on the MinGW Wiki.

Using exe or DLL for calling program from another program

Are there any advantages in using a dll ( like security, size, etc)

No. As a matter of fact, if you're looking at things like security, size, etc. using a DLL makes things worse. When you load a DLL, everything happens inside the loading process' address space. So any bug inside the DLL directly affects the rest of the program. A crash in the DLL code, will crash the whole program.

Is it easy to create a dll from my source-code ( I use Qt creator)

Yes it is. But to me it seems there's hardly any benefit for your particular use case. As a matter of fact, for rarely used code paths I'd rather strongly encourage putting it into a separate process (i.e. link it into a .EXE).

BTW: .dll and .exe are exactly the same. You can load a .exe as if it were a DLL; give it a DllMain and you can use it either way! Of course loading a EXE with LoadLibrary won't make it run in a separate process and rather import all the bugs into your main program.

Use .lib to compile then use .dll to link or explicitly call dll in C++?

Your college is wrong.

Your library project have created an import library(.lib) and the dll, when you link to your .lib, it is just a stub pointing to the dll file. If you remove the .dll file from where your .exe file sits, the .exe file should not start, so that's one way to verify that you need the .dll.

A .lib file in Windows can be either a static library or an import library.

Static Library

If it is a static library, all the code is contained in the .lib file and when you link to that .lib file the code is embedded in your executable. No dll file is used/needed.

Import Library

If the .lib file is an import library, it just contains a stub that points to the .dll, when you link to this, your executable does not embed the code of the library, it is linked to the .dll. The executable requires the .dll to be present, and will try to load it automatically when the exe starts.

Dynamic loaded

The third option is to have your code load the .dll explicitly, and extract/cast all functions/etc. from the dll using a string representation of its name. With this method you don't link to any .lib file during build time - this method is useful if you want to load plugins or dlls, but still have your executable work if those dlls are not present. This is likely what your college is talking about, and you probably don't need to do this.



Related Topics



Leave a reply



Submit