Jump to content

Compile Windows C++ Code On Linux


Iain

Recommended Posts

I've started to use Linux recently and have found just how easy it is to compile C++. When I compiled C++ on my XP PC a while ago, it was a real nightmare to determine exactly what settings I needed in order to compile correctly and not spew out a massive .exe file.

Does anyone know if I can compile C++ for a Windows executable on Linux? If so, what software should I use? As far as I know, I can't just use gcc on my Ubuntu or BT4 system.

Link to comment
Share on other sites

Are you interested in (cross) compiling an executable for Windows in Linux, or (port) compiling code from Windows to a Linux executable ?

There is cross-compiling on Linux; the guys at VLC usually do this.

You kinda have to google it for more details, since I don't know too much about this at the moment.

On the VLC wiki there's this link : http://wiki.videolan.org/Win32Compile and something specific for Fedora13 : http://wiki.videolan.org/Win32CompileFedora13

For both cases (cross-compilation and porting) if the code was written in Visual C++, all's good if it's not too language specific. Both GCC and Visual C++ have all sort of language specific constructions that are not standard and a pain to work-around or rewrite sometimes.

You also have to be careful for certain types; for example widechars in Visual C++ are not compatible with the standard char type.

Link to comment
Share on other sites

Are you interested in (cross) compiling an executable for Windows in Linux, or (port) compiling code from Windows to a Linux executable ?

Thanks for the response. As far as I know, if I have some "Hello World" code and compile it on Linux, it will only run on Linux. If I have the same code and compile it on Windows (using Borland), it will only run on Windows. I'm interested in being able to compile it on Linux, but have it run on Windows.

I'll look at the links that you posted.

Link to comment
Share on other sites

  • 3 weeks later...

If you are using really generic code, you might be ok? There are different libraries that you have to deal with. Gui and or system programming makes it more complicated. Then you are on your own. There are several good books (pdf) about doing linux c++ on the web. You will need build-essential at the minimum. Eclipse might be nice for a gui environment.

test.cpp

#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	return 0;
}

$ g++ test.cpp -o testme

$ ./testme

Hello World!

Edited by inventoman
Link to comment
Share on other sites

You need all the windows header files to be compiled against. It can be done, but you need a wrapper that makes gcc use the windows hearders instead of linux headers

See www.youtube.com/watch?v=rhe6N7FB1D4

Link to comment
Share on other sites

Good catch, but does not c++ require using g++? I am not a C/C++ programmer, so do not take my word for it.

rpstest.cpp:

//Rock Paper Scissors game by Moonbat
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    srand((unsigned)time(0));
    int choice;
    int compchoice = (rand()%2)+1;
    cout << "Welcome to ~ Rock Paper Scissors ~!! I assume you know how to play,";
    cout << " so lets begin. You are playing against the computer. Type 0 for";
    cout << " rock, 1 for paper, and 2 for scissors\n";
    cin >> choice;

    if (choice == 0) 
    {
              if (compchoice == 0)
              cout << "It's a tie!\n\n\n\n";
              else if (compchoice == 1)
              cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
              else if (compchoice == 2)
              cout << "Rock beats scissors! You win!\n\n\n\n";
    }

    if (choice == 1)
    {
               if (compchoice == 0)
               cout << "It's a tie!\n\n\n\n";
               else if (compchoice == 1)
               cout << "Paper beats rock! You win!\n\n\n\n";
               else if (compchoice == 2)
               cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
   }

   if (choice == 2)
   {
              if (compchoice == 0)
              cout << "It's a tie!\n\n\n\n";
              else if (compchoice == 1)
              cout << "Scissors beat paper! You win!\n\n\n\n";
              else if (compchoice == 2)
              cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
   }
    return main();
}

g++ rpstest.cpp -o rps

Edited by inventoman
Link to comment
Share on other sites

  • 1 month later...

one very good tool for that is mingw (formerly mingw32) it works just like gcc/g++ but compiles to a .exe. you will, of course, need the windows versions of any libraries that you use, though it does come with the standard ones. the compiler is called with the command i586-mingw32msvc-gcc for c and i586-mingw32msvc-g++ for c++.

Link to comment
Share on other sites

  • 1 month later...

As long as you follow the standards cross-platform, and the C++ standards (not the Microsoft C++ standards, as they tend to avoid and even depreciate modern standards), then you should really be good to go.

In the many years I've been programming C++, I haven't had any issues. I use CMake for my project file generation, and compilation works fine using MinGW and/or MSYS on Windows and GCC on Linux.

You *almost* can't go wrong using something like CMake or Premake4.

Link to comment
Share on other sites

one very good tool for that is mingw (formerly mingw32) it works just like gcc/g++ but compiles to a .exe. you will, of course, need the windows versions of any libraries that you use, though it does come with the standard ones. the compiler is called with the command i586-mingw32msvc-gcc for c and i586-mingw32msvc-g++ for c++.

When you use this system (MinGW) with the builtin 'terminal', you would actually just call "gcc" or "g++" at it should work without problem.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...