Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

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.







1 Sept 2015

OpenCV 3.0 in NetBeans IDE 8.0.3 configuration (MinGW + Windows 7)

This post is about my personal experience with installing, configuring and running OpenCV 3.0 in NetBeans IDE 8.0.2 with MinGW compiler on Windows 7. I do not say that this is the best possible approach, but I have learnt from some my mistakes, so someone could find this tutorial useful.

Prerequisites for this task

  • Install the MinGW compiler for 32-bit compilation, my install path is C:\Code\mingw\
  • Install MSYS, my path is C:\Code\msys\1.0\
  • Install CMake, my install path is C:\Code\CMake\
  • Install and configure NetBeans IDE with C++ plugin, link with C:\Code\mingw\bin
  • Include CMake bin folder in C:\Code\CMake\bin in Path environment variable
  • Include MinGW compiler bin folder in C:\Code\mingw\bin in Path environment variable
You should be able to compile and run simple Hello World! example. (To minimize possible initial error, that is not relevant to OpenCV configuration.). Create new project:


I have named my project MyOpenCVtest. I use GNU MinGW.


In NetBeans IDE menu, the Tools/Options/C/C++ show the compiler configuration, it should be set like this.


The simple Hello World! program can be written with iostream library and you should have no problem to compile it an run with no additional steps.


Now you can close NetBeans and you are ready to setup the OpenCV 3.0. To verify that your Environment Variable Path contains the C:\Code\CMake\bin, and C:\Code\mingw\bin, that are required for next steps, run the command prompt and type path. The output should contain it. If not, Right-click on the Computer and select the Properties option. Than select Advanced System Settings, in Advanced click on the Environment Variables and find Path. Append the

C:\Code\CMake\bin;C:\Code\mingw\bin;

at the end of the textbox with no blank spaces and do not delete anything. Test again with command prompt. Now, you are ready to download  the OpenCV.

Note: If you have problems with MinGW, install also Cygwin.

Prepare the files

The first step is to download the OpenCV for Windows. Go to official site official site and click on link to start download.



Extract the files into working directory, in my case it is C:\Code\opencv. After extraction there will be two folders, the build folder and the source folder. I was trying to locate the mingw folder which should contain the files for MinGW compilator, inside it, but it is not there. After some googling, I found that I need to compile it myself using CMake. To do so, locate the cmake-gui.exe in the CMake bin folder, in my case it is C:\Code\CMake\bin. Run the application.

I have set field Where is the source code:  to C:/Code/opencv/sources. You can use the Browse button. This is where the source files are located. Just under that, the field Where to build binaries: should lead to your custom folder, where you want the output. I chosen the path build folder, to C:/Code/opencv/build/x86/mingw, but you can specify any folder. (Prefer folder with path that doesn't contain space characters). Also, you can check Grouped and for better orientation in lists.



After that, I have clicked the Configure button below. Since in my case the mingw folder does not exist, the CMake asks to create it by window.



After that you are prompted to specify generator for this project. Since I am using MinGW, the option  MinGW Makefiles is obvious. Click Finish button to complete.


If everything is OK, the CMake now start to configure - it takes a few moments. In the log on the bottom of the window you may see some red warning messages, but it does not mean that there is a problem. After the configuration run is complete, the window should look like this:


Now click on the Generate button. The process should take only a few moments. The red colored list is now white.


Now you can close the CMake and head to working folder, in my case it is C:/Code/opencv/build/x86/mingw. Run the command prompt (the simple way is Hold Shift + right mouse click in folder) and run the

mingw32-make

command in it. If it throws the unrecognized command error, you probably have an error in the C:\Code\mingw\bin in PATH system variable. The output should look like this:


During this process, I have encountered two errors that stopped build. The first was at 26% - I have encountered this error:


I browsed and found the solution that I need to uncheck the WITH_IPP option in CMake list. After that I pressed the Configure and Generate buttons. 


After that I have started the command prompt again and run the

mingw32-make

command in it. Build was restarted, but it reached 26% really fast, ant than slowly continued. Unfortunately, at 37%, I have encountered second error:


I found solution, that I need to comment (#) the line 67 in file OpenCVCompilerOptions.cmake  located in C:\Code\opencv\sources\cmake.

# add_extra_compiler_option(-Werror=non-virtual-dtor)

After that I have started the command prompt again and run the

mingw32-make

command in it. The configuration and generation occurred and the build process restarted from zero, but it reached 100% without additional input. Finally, the result looks like this:

When finished, I have started the command prompt and run the

mingw32-make install

command in it. This created the C:\Code\opencv\build\x86\mingw\install folder with include and x86 folders. Copy the folders opencv and opencv2 from include folder to MinGW include folder, in my case to C:\Code\mingw\include. (approx. 3MB). Than open the lib folder, in my case C:\Code\opencv\build\x86\mingw\install\x86\mingw\lib, and copy all .a files (18 files) to MinGW lib folder, in my case to C:\Code\mingw\libNow it should be prepared to use OpenCV in NetBeans.


Test OpenCV in NetBeans IDE 8.0.2

Run the NetBeans and open the Hello World project. Write simple code to get OpenCV version:

Without any settings to project properties, the result should be "OpenCV Version: 3.0.0." in Debug and Release mode. Now we try to test webcam, copy the code



This code access the default webcam in your computer, opens the window and show frames. To stop the application, press any key. When you try to compile, you get errors like undefined reference to cv::VideoCapture::VideoCapture(int). You need to add the OpenCV libraries to Linker. Right click on the Project (in my case MyOpenCVtest) node and select Properties option. Set  Configuration to <All Configurations>, so changes will apply both on debug and release settings. In the tree-view on the left, open Build node, select Linker and find Libraries section on left. Click on the ... button and use Add Library File ... button. In the MinGW lib folder, in my case the C:\Code\mingw\lib, select all libopencv_ libraries. You should end up with this:



Click OK button twice and you should be able to compile the webcam test code with no errors. The application cannot run, you need to copy the 17 .dll files from x86\mingw\bin in install folder that you created, in my case from C:\Code\opencv\build\x86\mingw\install\x86\mingw\bin to project output folder, where is the myopencvtest.exe, in my case to MyOpenCVtest\dist\Debug\MinGW-Windows and MyOpenCVtest\dist\Release\MinGW-Windows. Now you can run the application, through the NetBeans or standalone.