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:




27 Nov 2017

Hello world! C++ application in NetBeans IDE 8.2 (Win10 + MinGW-W64)

This post is dedicated to simple "Hello world!" application project in NetBeans IDE 8.2, that is compiled as 32-bit and 64-bit application using MinGW. 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

  • NetBeans IDE 8.2 for C/C++
  • MinGW tools
  • MinGW-W64 tools
My operating system is Windows 10, 64-bit version. The first step is to download and install NetBeans IDE, in my case it is NetBeans IDE 8.2. Then I have installed Mingw and MinGW-w64. In NetBeans main menu Tools/Options/C/C++ I have these two configurations:

MinGW32 (for 32-bit applications)



MinGW-W64




Hello world! project

Create a new C/C++ Application project


Name the project as you wish, in my case it is MySampleApp. I use MinGW32 tool collection as default, but you can use MinGW-W64 and make 64-bit application first.


Modify the default main.cpp file and add Hello world! message using printf function from stdio.h.

Now open the project Properties by right-click on project root. Since we want to make 32/64 bit application versions, click on Manage Configurations... Select Debug and duplicate it for 64-bit version. Do it for Release too. For example you may have configurations with these names:


The next step is to set an Architecture. Select both 32-bit configurations.


In C/C++ Compiler category, set Architecture to 32 bits. In Compilation Line, add Additional Options 

-static-libgcc -static-libstdc++


Same task for 64-bit configurations, select both set Architecture to 64 bits.



Moreover, in Build category, select MinGW-W64 in Tool collection field.


Now select All configurations, go to Run category and set Standard Output to Console Type


Compile and run the project


32-bit version


Select Debug configuration (32-bit) and Build the project.


If no errors, you can run the application with this output. 


Same result should be achieved in Release (32-bit) configuration.


64-bit version


Select Debug_64bit configuration and Build the project. Compilation output with no errors is like this



Application runs with same output


Summary


This post presents the simple console project that is configured for 32/64 bit versions of operating system. The main focus of this tutorial is to test, if the MinGW tool collections are configured correctly in NetBeans IDE. Presented project could be a basis for more complex applications.

26 Nov 2017

Adding version information and an Icon to C++ application in NetBeans IDE 8.2 (Win10 + MinGW-W64)

This post is about my personal experience with adding version information to application .exe file.  This tutorial is focused to make things work, but it may not be the best approach...


Prerequisites for this task

  • Install NetBeans IDE 8.2 for C/C++
  • Install and configure MinGW-W64 compiler
You should be able to compile and run simple Hello World! example for 64-bit application both in NetBeans IDE and as standalone application from folder on Windows 10. Firstly, create the  NetBeans C/C++ project for "Hello World!" application. In my case, the project name is MyApp.




Put "Hello World!" message to main.cpp by printf function from stdio.h.


To compile the project for 64-bit Windows 10, you need to configure the C++ Compiler options. First, select All Configurations so you can set the same options for Debug and Release configuration at once. Also, select the 64 Bits in Architecture options and add two Additional Options to Compilation line 
-static-libgcc -static-libstdc++

In Run options, you need to select the Console Type as Standard Output. My configuration for C++ Compiler was


And Run configuration was


Compilation output for my application was


and Run output was


My application file myapp.exe is now located in dest folder, by right clicking on it I can see that it does not contain any information about version or author. We are going to change it!



Adding the version information to application

I found this solution on StackOverflow by Ernestas Gruodis and adopted by my needs. First, you need to create header file ids.h


Secondly, you need to create resource.rc file with application values for your application. Add it as new empty file


name it resource with .rc extension and put code with ids.h include:


Now, you need to locate the resource.rc file and select its Properties by right-clicking 


In General, make sure that it is NOT excluded from build (at least for the first time).


On left pane, select Custom Build Step



On right side, you need to configure the Custom Build options for All configurations
  • Command Line: windres.exe resource.rc resource.o
  • Outputs: resource.o
  • Additional Dependencies: ids.h
  • Description: as you wish



Now, open Project properties and select Linker options. For All Configuration, add resource.o to Additional Dependencies and Additional Options for Compilation line.


Now, since the application needs an Icon, you can create one if you do not have any. You can use, for example, the Paint to create square shape image and than use online converter to make .ico file. My icon is 128x128 .png image, converted to 128x128 icon.


Name it as stated in resource.rc file, in my case MyAppIcon.ico and put it to project folder. You can include it as an existing item to Resource files in project. Last thing is to add manifest file. Add it as an empty file with name myapp.exe.manifest. The file remains empty in project.




To summarize the solution, overview on project folder is:
Now, when you Clean and Compile the project, you should see the compilation output.


The application file myapp.exe in dest folder now have an icon and properties.


Use version information in application

If needed, you can use version as an defined value. Add new defines for version


and modify main.cpp by adding include for ids.h and output line with version information

To link the version information defined in ids.h with version shown in Properties window, you also need to modify the resource.rc as


Now, when you Clean and Compile the project, you can run the application by command line and see the version like mine.