Iain Posted June 13, 2011 Share Posted June 13, 2011 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. Quote Link to comment Share on other sites More sharing options...
commodo Posted June 14, 2011 Share Posted June 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
Iain Posted June 14, 2011 Author Share Posted June 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
justapeon Posted July 3, 2011 Share Posted July 3, 2011 (edited) 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 July 3, 2011 by inventoman Quote Link to comment Share on other sites More sharing options...
digip Posted July 3, 2011 Share Posted July 3, 2011 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 Quote Link to comment Share on other sites More sharing options...
justapeon Posted July 7, 2011 Share Posted July 7, 2011 (edited) 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 July 7, 2011 by inventoman Quote Link to comment Share on other sites More sharing options...
Iain Posted July 8, 2011 Author Share Posted July 8, 2011 (edited) I'm grateful for the further input (and apologies for not having responded earlier). That's a bit more for me to digest and investigate further. Edited July 8, 2011 by Iain Quote Link to comment Share on other sites More sharing options...
supereater14 Posted August 25, 2011 Share Posted August 25, 2011 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++. Quote Link to comment Share on other sites More sharing options...
T3hGamerDK Posted October 10, 2011 Share Posted October 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
T3hGamerDK Posted October 10, 2011 Share Posted October 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
Iain Posted October 15, 2011 Author Share Posted October 15, 2011 Thank you for the further tips. I've started using MinGW and find it a very useful tool. It's certainly a lot easier than when I used to use Dev-C++ but maybe I'm just a bit more experienced in coding nowadays! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.