3 Dec 2017

Create own DLL and import it to C/C++ Application in NetBeans IDE 8.2 (Win 10 + MinGW-W64)

The focus of this post is to create simple DLL with one class and import it into simple "Hello world!" application in NetBeans project. This tutorial summarizes my approach and it is focused to make things work, but it may not be the best approach...

Prerequisites for this task

In order to eliminate errors unrelated to this task, make sure that you have:

  • Installed NetBeans IDE 8.2 or higher,
  • Installed MinGW-W64 tools
  • Created and tested "Hello world!" console C/C++ Application.

My operating system is Windows 10, 64-bit version. In my NetBeans IDE, I have MinGW-W64 tool collection as default configuration.


The DLL library project

The first step is to create the DLL library, so I pick C/C++ Dynamic Library project. In my case, I name it MyDLL.



Now I have an empty project with no header or source files. Add new C++ Header file and name it libexport.



Write this code, modify MY_DLL so it defines yours DLL.


Now Add new C/C++ Class to project, my class is called MySampleClass.



Open and modify the header file of your class. I have included stdio.h and libexport.h, modified the constructor and a destructor for my class and added TestMe function. Also, you need to add DLL_EXPORT between class and its name definition. 


The source code can be simplified so it now contain only TestMe() function, which uses the stdio.h printf function. Put #define for your library.

The next step is to add a new C/C++ Header file, that will contains headers of this library. My header file is called as the project, MyDLL.h.


I have only one class header file to include, so the MyDLL.h is


Since this library is designed to be used in 64-bit application, in project Properties, C++ Compiler category set for All Configurations (Debug, Release) the 64-bit Architecture and add Additional Options

-static-libgcc -static-libstdc++
to Compilation Line.



Now, after compiling I have no errors and libMyDLL.dll file in output folder.


The next step is to use this library in simple application. 

The Test application project

Create new C/C++ Application project. Mine is called MyTestApp.



Open MyTestApp project properties, select C/C++ Compiler. For All Configurations, in General/Include Directories, locate the project with your DLL. Select Architecture to 64 bits and add Additional Options

-static-libgcc -static-libstdc++
to Compilation Line.



Next, open Linker category. First, in General/Additional Library Dependencies locate the output folder, where you have your library .dll file. 


Now, in Libraries, click to Add Library File and locate your .dll file. Check to Copy shared libraries so the library will copy to this project output folder.



The library should be now included, so you can use its functionality in this project. Open main.cpp source file and include your library. Remove all default code and start by writing #include <, than press Control+Space and you should see your library main header.

 Full main.cpp code for testing library function may be like


Last thing, select Run category and switch Console Type to Standard output.


Now, when you build the application, you should have no errors and library copied to output folder.



When you Run the application in NetBeans IDE, you should get this output:


But if I try to run application standalone in folder, I get this missing libwinpthread-1.dll error message:


So far, I found only this solution to make it run:
  1. open Console MyTestApp project Properties,
  2. select Linker category,
  3. open options for Libraries,
  4. click to Add Option...,
  5. select Static binding,
  6. click to Clean+Rebuild the project.

Now, I can run it from cmd with no error to see the output:




No comments:

Post a Comment