How to create a cross-platform C++ application in Netbeans? - c++

I am experiencing some difficulties while creating a simple "Hello world" cross-platform C++ app in Netbeans 8.0.1 for both Linux and Windows operating systems. I found very few instructions for Windows and none for Linux (my OS is Ubuntu 14.04).
My research over the Internet brought me to a conclusion that there should be created one more makefile for Windows OS. Usually, while creating a C++ project Netbeans kindly proposes us to create a makefile for us. And it's great, but it's just for Linux.
I stuck on Makefile creation (never done this before manually), so these were my steps to creating makefile for Windows:
Right Click on Project -> New -> Makefile
File Name: "Makefile-Windows" -> Next
Compiler: GNU Compilers, Platform: Windows -> Next
Base Directory: Project directory -> Next
Target Name: hello-world ( "Executable" radio button ) -> Add -> Next
Directory for All created files: GNU-amd64-Windows -> Next
Enter source files: main.cpp -> Next
Directories to Search for Include Files: Left empty -> Next
Choose libraries to link with: None, Link mode: Dynamic Linking -> Next
Libraries to link with: Left empty -> Next
How would you like your code to be built?: Dev. mode -> Next
, 13. Compiler options: Left default -> Finish
As a result, after making this fresh baked makefile in my GNU-amd64-Windows folder appears an object file (main.o) and an executable file (hello-world), but still executable only in Linux. When I try to run it in Windows command line, I get the error:
"Bad Command or file name"
Just in case here is my main.cpp content:
#include <iostream>
int main( int argc, char** argv )
{
std::cout << "Hello world" << std::endl;
return 0;
}
If I have chosen a wrong way, then how should it be done?

As mentioned Borgleader, only C++ code can be cross-platform. Here is my solution:
Firstly, we shouldn't use Linux GCC compiler for compiling Windows applications. That's why we need to install Windows compiler for Linux:
sudo apt-get install mingw32 mingw32-binutils mingw32-runtime
Secondly, while creating makefile we should specify alternative compiler. Use:
/usr/bin/i586-mingw32msvc-g++
insteat of default:
g++
Finally, after making this makefile our output file becomes executable on Windows platforms.

Related

C++: Error occurs when fmt library is included

https://github.com/fmtlib/fmt
I'm having trouble using c++ library on ubuntu eclipse. I downloaded the fmt library from github. I followed the steps as described in the documentation section Building the Library. In fmt folder;
mkdir build # Create a directory to hold the build output.
cd build
cmake .. # Generate native build scripts.
After running the commands, makefiles are created in the build array.
To install the library I ran following command.
sudo make install
After the installation, the necessary files were copied to the /usr/local/ directory. I copied the header files and compiled libfmt.a file to my project directory.
My project hierarchy;
fmtTestProject
|--Debug
|--src
|--fmtTest.cpp
|--include
|--args.h
|--chrono.h
|--color.h
|--compile.h
|--core.h
|--format.h
|--format-inl.h
|--os.h
|--ostream.h
|--printf.h
|--ranges.h
|--std.h
|--xchar.h
|--lib
|--libfmt.a
I defined the include files to the eclipse project.
fmtTest -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes -> Include paths -> "${workspace_loc:/${ProjName}/include}"
fmtTest -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Library search path -> "${workspace_loc:/${ProjName}/lib}"
After the define libraries, I wrote the code in the documentation and compiled it.
#include <fmt/core.h>
int main() {
fmt::print("Hello, world!\n");
}
The code compiles without errors. But the problem is there are too many errors for core.h file. Some errors;
Type 'std::experimental::basic_string_view<Char>' could not be resolved
Invalid overload of 'parse_format_specs'
Return has value, in function returning void
There are errors like these. The software compiles and runs, but there are errors in both main and library files. Likewise, I did the same compiling and installing to use the library named spdlog. It also compiles and works, but a lot of errors occur. Am I missing something in the cmake processes? There are always errors when I import an external library. How can I solve it?

Failing to install gtkmm / gtk+

So i am new on the platform, i use c/c++ and i have interests in graph interfaces so i decided to use gtk+and gtkmm with visual studio.
First, i downloaded gtk for the gnome project. I followed all the steps and i got this after a manual run :
\\\ test23.cpp
#include <gtkmm.h>
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
Gtk::Window fenetre;
Gtk::Main::run(fenetre);
return 0;
}
$ g++ -std=c++ test23.cpp $(pkg-config gtkmm-3.0 --cflags --libs | sed 's/ -I/ -isystem /g')
Package gtkmm-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtkmm-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtkmm-3.0' found
bash: g++: command not found
so i decided to install using vcpkg.
But I failed to install gtk+ and gtkmm after multiple tries.
Here what i've got :
C:\WINDOWS\system32>vcpkg install gtk
Computing installation plan...
The following packages will be built and installed:
gtk[core]:x86-windows
* harfbuzz[core,glib]:x86-windows
* libepoxy[core]:x86-windows
* pango[core]:x86-windows
Additional packages (*) will be modified to complete this operation.
Detecting compiler hash for triplet x86-windows...
Starting package 1/4: libepoxy:x86-windows
Building package libepoxy[core]:x86-windows...
Could not locate cached archive: C:\Users\Manolo97233\AppData\Local\vcpkg\archives\f7\f743ec00b235ca7fd37812284b7d2e09d89b368a.zip
-- Using cached C:/Windows/SysWOW64/vcpkg/downloads/anholt-libepoxy-1.5.4.tar.gz
-- Cleaning sources at C:/Windows/SysWOW64/vcpkg/buildtrees/libepoxy/src/1.5.4-337c486045.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source C:/Windows/SysWOW64/vcpkg/downloads/anholt-libepoxy-1.5.4.tar.gz
-- Applying patch libepoxy-1.5.4_Add_call_convention_to_mock_function.patch
-- Using source at C:/Windows/SysWOW64/vcpkg/buildtrees/libepoxy/src/1.5.4-337c486045.clean
-- Acquiring MSYS Packages...
CMake Error at scripts/cmake/vcpkg_execute_required_process.cmake:72 (message):
Command failed: C:/Windows/SysWOW64/vcpkg/downloads/tools/msys2/msys64/usr/bin/bash.exe --noprofile --norc -c "pacman -S --noconfirm --needed pkg-config"
Working Directory: C:/Windows/SysWOW64/vcpkg/downloads/tools/msys2
Error code: 1
See logs for more information:
C:\Windows\SysWOW64\vcpkg\buildtrees\libepoxy\msys-pacman-x86-windows-err.log
Call Stack (most recent call first):
scripts/cmake/vcpkg_acquire_msys.cmake:127 (vcpkg_execute_required_process)
scripts/cmake/vcpkg_configure_meson.cmake:106 (vcpkg_acquire_msys)
ports/libepoxy/portfile.cmake:16 (vcpkg_configure_meson)
scripts/ports.cmake:79 (include)
I believed i had a problem with msys2 so i tried to install it separately following an install kit unsuccessfully.
then i tried to run separately other packages involved with gtkmm like pango and i got this :
//vcpkg install pango
Computing installation plan...
error writing file: C:\Windows\SysWOW64\vcpkg\buildtrees\0.vcpkg_dep_info.cmake: The data is invalid.
Someone explained on a topic it could be an vcpkg accessibility problem for others packages. i modified the accessibility of vcpkg for other packages in my systWOW64'file parameters but it went unsuccessful.
I don't know if vcpkg is problem because i did not remove it and re install it.
I am looking for solutions/options, I am short on ideas, i literally need some help.
Thanks
Use JHbuild, its your friend when building GNOME applications from source !
Run jhbuild build gtkmm --nodeps, the built files will be installed in $HOME/jhbuild/build. To compile your application with it, you must export the environment variable PKG_CONFIG_PATH as follows:
export PKG_CONFIG_PATH="$HOME/jhbuild/build/lib/pkgconfig"
(in the terminal you are running the g++ command from)
And you have to install g++.
I do not know how you are using bash together with visual studio, but
if you have apt, you can install g++ and gtkmm with the following command :
apt install libgtkmm-3.0-dev libgstreamermm-1.0-dev g++
If you have already downloaded gtk, then the downloaded package must contain a .pc file. The directory this file is in when you installed the downloaded package you must add to the PKG_CONFIG_PATH.
Thanks Frederic for your answer it really help me.
I have got an other question. In my quest to add external packages to my C/C++ project, i read differents topics about how to convert static library (.a) into static library (.lib).
I downloaded packages and i wanted to add them manually using their pathway.
I added my packages as followed:
1 / Add the path of the headers in the compiler directories for the project:
-> Project / Properties menu
-> On the left tree, choose Configuration properties - C / C ++ - General
-> On the right table, the first line "Other Include directories": add the directory (s) of your library containing the headers
2 / Add the path of the .lib in the compiler directories for the project:
-> Project / Properties menu
-> On the left tree, choose Configuration Properties - Link Editor - General
-> On the right table, the line "Directory of additional libraries": add the directory (s) of your library containing the .libs
3 / Specify the libraries with which your project is linked:
-> Project / Properties menu
-> On the left tree, choose Configuration Properties - Link Editor - Enter
-> On the right table, the first line "Additional dependencies": add the .lib library (s) with which your project must be linked
But I looked for .lib files to add to my linker input instead, I found .a files and .dll files and I didn't know what to do. . a files and .lib files are almost identical. It seems like .a files are used under linux while .lib are used under windows.
I tried to add the .a files unsuccesfilly. I wondered if I could convert an .a file into a .lib file.
Thanks

Configuring Netbeans to Debug a C++ program containing non standard libraries

I want to use a non standard library ( igraph C library ) to build a 'hello world' program in C++, using Netbeans.
I want to use Debug features of Netbeans too.
First I followed the installation instructions provided by the library vendor, used a terminal to compile and run a hello world program supplied by library vendor. It compiles & runs fine using terminal.
The problem is as soon as I open the C project file in Netbeans, I must CONFIGURE it so that it can :
Run the program
Debug the program
Netbeans lets you run and debug C++ programs off the bat, if you using their standard libraries. But I read on some forums that some configuration needs to be done if I want to make Netbeans run and debug C++ programs containing non standard libraries
I found the answer. Here are the steps :
1. Find out where your non standard library files are installed
On my Ubuntu I used the following command to find out the location of my installed files :
pkg-config --libs --cflags igraph
In above command you have to substitute igraph with name of YOUR non standard library that you installed.
This command gave me following output :
-I/usr/local/include/igraph -L/usr/local/lib -ligraph
I noted down the path after -I, the path after -L and the string after -l. These 3 need to be fed inside Netbeans in the steps below
source: http://igraph.org/c/doc/igraph-tutorial.html#idm470953198960
2. Configure Netbeans
Right click on Project in Netbeans -> properties -> Linker ->Libraries -> Add option -> Other -> type -ligraph in here
In your case you have to type what you found instead of -ligraph on your system during step #1
Project -> properties -> Linker -> Additional Library Directories > I typed /usr/local/lib in here
In your case you have to use the path you got on your system after -L flag in step #1
Project -> properties -> C++ Compiler -> Include Directories -> I typed /usr/local/include/igraph in here
In your case you have to use the path you got on your system after -I flag in step #1
source: https://stackoverflow.com/a/13292276/3143538
add to the Project->Properties->Run->Environment :
Name: LD_LIBRARY_PATH
Value: $LD_LIBRARY_PATH:/usr/local/lib
instead of /usr/local/lib you have to use the path you got after -L flag in step #1
source: https://askubuntu.com/questions/267071/ld-library-path-specification
After above steps, I am able to both compile, run, and debug the program

Why am I getting the "No binary found." error in the Eclipse IDE?

When trying to run this:
int main()
{
return 0;
}
I get the
Launch failed. No binary found.
error. Why? I have the correct path set (C:\cygwin64\bin). Also, I have the Cygwin PE set too. Finally, I installed the devel package. Any help?
Also getting the 3 errors of:
G++, gcc and make not found in PATH
Try to install MinGW instead, it works perfectly with eclipse: just make sure to set the install directory to C:\MinGW (the directory name is case-sensitive).
Then you have to set a compiler for your projects: Go to Window -> Prefrences then
Then create a new project and set the compiler for it:
Before run your program build your project: Project -> Build Project

using googletest in eclipse: how?

I've downloaded google test, but now I've no idea on how to link it to my project in eclipse.
Should I add it as a source folder? Should include it as g++ included library? And how can I run test then?
Using Riga's excellent answer, here is a summary of how I got it to work:
Created a new C++ project in Eclipse (I chose Executable > Empty Project)
Downloaded googletest 1.5.0, untarred, and ran ./scripts/fuse_gtest_files.py . <project-dir>/contrib
Back in Eclipse, excluded the contrib directory from the Release build configuration, and added <project-dir>/contrib to the include directories (odd, I know)
Added a src directory and added a class named Foo (see below for the contents of Foo.h--I left Foo.cpp empty for now)
Added a test directory in Eclipse, excluded it from the Release build configuration, added <project-dir>/contrib to the include directories, and added new source files FooTest.cpp and AllTests.cpp (see below for contents)
Built and ran the project!
Foo.h:
#ifndef FOO_H_
#define FOO_H_
class Foo {
public:
virtual ~Foo();
Foo();
bool foo(void) { return true; }
};
#endif /* FOO_H_ */
FooTest.cpp:
#include "gtest/gtest.h"
#include "Foo.h"
namespace {
class FooTest : public ::testing::Test {
protected:
Foo foo;
};
TEST_F(FooTest, Foo) {
ASSERT_TRUE(foo.foo());
}
}
AllTests.cpp:
#include "gtest/gtest.h"
#include "FooTest.cpp"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here are the detailed steps:
In Eclipse, open the File menu and select New > C++ Project
Project Type: Executable > Empty Project
Toolchain: Linux GCC
Click Finish
Open a terminal and cd /tmp
wget http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2
cd gtest-1.5.0/
./scripts/fuse_gtest_files.py . <project-dir>/contrib
Back in Eclipse, right-click on the project folder in the Project Explorer pane, then select Refresh
In the Project Explorer pane, right-click on the contrib folder, select Exclude from build...*, untick only the **Release box, and click OK
Right-click on the contrib folder and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Compiler > Directories
Click on the Add... button, then the Workspace... button, then select <project-name>/contrib and click OK to add the directory
Click OK once more to accept your changes to the build settings
Right-click on the project in the Project Explorer pane and select New > Folder, enter src as its name, and click OK
Right-click on the src folder in the Project Explorer pane and select New > Class, name it Foo, then click OK (see above for contents of Foo.h; Foo.cpp can be left as is)
Right-click on the project in the Project Explorer pane and select New > Folder, enter test as its name, and click OK
Follow the steps above to add <project-name>/contrib and <project-name>/src as include directories to the test directory
Right-click on the test folder, then select New > Source File to add AllTests.cpp to the test folder, then repeat the same steps to add FooTest.cpp (see above for contents)
Right-click on FooTest.cpp and select Exclude from build..., click the Select All button, then OK
Right-click on the project in the Project Explorer pane, and select Properties > C/C++ Build > Settings > Tool Settings tab > GCC C++ Linker > Libraries, then click the Add... button, enter pthread (required by googletest), click OK to add the library, then OK once more to accept the changes
Hit Ctrl-b to build the project
Hit Ctrl-F11 to run the project
Victory!
Step 1 Install Eclipse
If Eclipse is not already installed on the machine, then get the latest version of the Eclipse IDE for C/C++ Developers from the Eclipse downloads page (http://www.eclipse.org/downloads/).
If Eclipse is already installed but only for Java, download the C++ plug-in following these instructions.
a. Open Eclipse and click on Help->Install New Software
b. In the Work with: box, type in http://download.eclipse.org/tools/cdt/releases/kepler. After a few moments, the Name box will populate. Select the following components:
CDT Main Features -> C/C++ Development Tools
CDT Main Features -> C/C++ Development Tools SDK
CDT Optional Features -> C/C++ Unit Testing Support
CDT Optional Features -> C/C++ Unit Testing Support Source
CDT Optional Features -> C/C++ Visual C++ Support
c. Click on Next, agree to the statements, and then Finish.
Step 2 Download Cygwin
Install Cygwin by clicking on the setup-x86_64.exe link on the Cygwin install page (http://www.cygwin.com/install.html). After running, click Next through the defaults until you get to the Select Packages window.
You will need to search for and install two packages: gcc and make.
The first search term is gcc. Search for gcc and then open the Devel folder. Mark the following packages for install by clicking on the word Skip (it will then change to the build number, which may by higher than the one depicted here): gcc-core, gcc-g++, and libgcc1.
The second search term is make. Here, we will only need the Devel package make.
Once these have been selected, click Next to install.
Step 3 Download and build Google Test project
Download the latest release of GoogleTest from https://code.google.com/p/googletest/downloads/list, and extract the zip file contents into a common directory. It is important that all users are able to access this directory.
(Note: the following commands use make -- the last revision of GoogleTest that uses make is https://github.com/google/googletest/releases/tag/release-1.8.1. For future revisions of GoogleTest use cmake instead.)
To build the Google Test project:
Open Cygwin (find the install directory for Cygwin and double-click
on Cygwin.bat).
Change the current working directory to the unzipped
GoogleTest make directory: cd c:/<<yourpath>>/gtest-1.7.0/make/
Build the project: make
Create an archived library out of the
gtest-all.o file: ar -rv libgtest.a gtest-all.o
Step 4 Add the Cygwin bin directory to the computers PATH variable
Follow the instructions on this page for your version of Windows: http://www.java.com/en/download/help/path.xml, to add Cygwins bin directory to the computers PATH environment variable. (typically by adding ;C:\cygwin64\bin to the end of the current value).
Step 5 Create a new project that uses GoogleTest
Start Eclipse and select File->New->C++ Project. Enter the values below and click Finish.
In the Project Explore, right-click the name of the project and select Properties. Under C/C++ Build, change the Builder type to Internal Builder.
Under C/C++ Build, select Settings, then click on the Includes folder under Cygwin C++ Compiler. Click on the Add button in the top box and then browse to the GoogleTest include folder.
Lastly, under the Cygwin C++ Linker folder, select Miscellaneous and then click the Add icon under Other objects. Find the libgtest.a file that you built back in step 3 (it should be in the make directory of the unzipped gtest folder).
Thats it! Now you're ready to try it out.
Step 6 Write some code that uses GoogleTest
Add a source folder by clicking File->New->Source folder. Call it
src.
Add a header file by right-clicking on the src folder and select New->Header File. Call this file Counter.h.
Add a source file by right-clicking on the src folder and select New->Source File. Call
this file Counter.cpp.
Add another source file and call it Counter_Tests.cpp.
Copy and paste the code below into the appropriate files:
Counter.h
class Counter {
private:
int mCounter;
public:
Counter() : mCounter(0) {}
int Increment();
};
Counter.cpp
#include <stdio.h>
#include "Counter.h"
int Counter::Increment() {
return mCounter++;
}
Counter_Tests.cpp
#include "gtest/gtest.h"
#include "Counter.h"
TEST(Counter, Increment) {
Counter c;
EXPECT_EQ(0, c.Increment());
EXPECT_EQ(1, c.Increment());
EXPECT_EQ(2, c.Increment());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
In the Project menu select Build All. Now, to connect up the GoogleTest unit testing framework, select Run Configurations from the Run menu. From this dialog, select C/C++ Unit and click the New button.
It should fill in this project name automatically under C/C++ Application, if not click on Search Project to select this project. Next, click on the C/C++ Testing tab. In the Tests Runner drop-down, choose Google Tests Runner, and then click Run to watch the magic!
Below is a snapshot of the result. After writing more code/tests, you can click on the button highlighted in red to quickly recompile and re-run all of the tests.
You should not add it to your source folder, make separate folder instead. This is for avoidance of dependency of your production code from testing project. Do it like this
../ #your project folder
Makefile
src/
module1 #some module
module2 #another module
build #tmp for build
dist #binaries
contrib/
gtest
...
test/ #your test project folder
Makefile
src/
module1 #correspondent to main project's one
module2 #correspondent to main project's one
build
dist
...
I usually use google test as two files, this is very handy. Use scripts/fuse_gtest_files.py from gtest distribution to extract them. Having only two files you can include their compilation in your test project compilation and have simple project structure.
In your test projectspecify include directories ../contrib:../src:src.
Thus you can include headers like this
test/src/module1/class1Test.h:
#include "gtest/gtest.h"
#include "module1/class1.h"
// test class1 here
// ...
test/src/mainTest.cpp:
#include "gtest/gtest.h"
#include "module1/class1Test.h"
#include "module2/class2Test.h"
// include other tests you have
// ...
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Here is my solution for Eclipse 4.3 and CDT 8.2
I felt this was somewhat easier then described above.
Download gtest and install it as described in the readme.txt (using cmake and make in linux)
Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Compiler-> Includes-> Include paths" and add the include folder in gtest
Go to "YourProject-> Properties-> C/C++ Build-> Settings-> GCC C++ Linker-> Libraries", add the gtest folder as search path and add libraries "gtest" and "pthread"
(4. If you have tests in the same project as sources exclude tests from release build)
Go to "Run-> Run Configurations..." and Create a new C/C++ Unit run configuration
Set project to your project and C/C++ Application to your Application in main tab. Set Tests Runner to Google Test Runner in C/C++ Testing tab.
(7. Error notifications may stick around in the eclipse gui, if this is the case re-indexing the project might help)
I've tray your solution and it runs well. I can say that for compile gtest is not very clear in the README. txt.
I've run the makefile in the /make directory through a cygwin console.
In my case the compiler advise me that don't findt the pthread library. So I modified the line
CXXFLAGS += -g -Wall -Wextra -pthread
and changed it to
CXXFLAGS += -g -Wall -Wextra -lpthread
The output I get is gtest_main.a. Then I have rename this file into libgtest.a and copy it to C:\cygwin\lib directory.
Then i've configure my eclipse project to use cygwin and added gtest and pthread as you say... and it works!
I hope it can help someone