Prisacari Dmitrii
Prisacari Dmitrii

Reputation: 2106

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

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:

  1. Right Click on Project -> New -> Makefile
  2. File Name: "Makefile-Windows" -> Next
  3. Compiler: GNU Compilers, Platform: Windows -> Next
  4. Base Directory: Project directory -> Next
  5. Target Name: hello-world ( "Executable" radio button ) -> Add -> Next
  6. Directory for All created files: GNU-amd64-Windows -> Next
  7. Enter source files: main.cpp -> Next
  8. Directories to Search for Include Files: Left empty -> Next
  9. Choose libraries to link with: None, Link mode: Dynamic Linking -> Next
  10. Libraries to link with: Left empty -> Next
  11. How would you like your code to be built?: Dev. mode -> Next
  12. , 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?

Upvotes: 2

Views: 2020

Answers (1)

Prisacari Dmitrii
Prisacari Dmitrii

Reputation: 2106

As mentioned Borgleader, only C++ code can be cross-platform. Here is my solution:

  1. 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
    
  2. Secondly, while creating makefile we should specify alternative compiler. Use:

    /usr/bin/i586-mingw32msvc-g++
    

    insteat of default:

    g++
    
  3. Finally, after making this makefile our output file becomes executable on Windows platforms.

Upvotes: 2

Related Questions