tags:

views:

605

answers:

5

I was told about the fascination of C++ and I have recently downloaded Visual C++ IDE to start learning C++.

However I had this question in mind: How can I write C++ console application in Visual C++ and build it for Linux and Windows? Is there any plugin, additional compilers or hacks to go around with?

+4  A: 

The most important thing is you want to avoid OS specific calls and stick with the standard C++ library.

If you don't include any Windows header file such as windows.h or winuser.h, then the compiler will warn you if you try to call a Windows specific function.

There are some features available on both Windows and Linux that need to be handled slightly differently (such as networking and memory mapping). You may want to look into a portable runtime library such as the Apache Portable Runtime that will abstract out the differences for you.

R Samuel Klatchko
I totally agree with that and will definitely keep that in mind. Thanks! But how can I compile the program for linux using visual studio?
thephpdeveloper
@thephpdeveloper: You can't do that. Read bmargulies' post
Prasoon Saurav
First you would need a cross compiling toolchain that runs on Windows and targets Linux. Then you will need to configure Visual Studio to use this alternative toolchain. But since you are going to have to move over to Linux to actually run/test the code, it may be easier just to build on Linux as well.
R Samuel Klatchko
ok got that. What I intend to do now is actually to build the project outside VS in Windows for Linux using linux compilers as mentioned thanks for great help!
thephpdeveloper
+4  A: 

Well, you can set up a 'makefile project' and have the makefile invoke mingw, but you'd have to go though the complex procedure to build a cross-compiling ming.

bmargulies
+1  A: 

If you stick to standard C++, you can compile the same source in linux with gcc.

You can also try your luck with Wine.

Seth
Not sure Wine will work with such an heavy application :)
the_drow
A: 

I would recommend using Visual C++ just as a project manager and text editor, and possibly for compiling for Windows. Then in your project maintain a Makefile to use with a cross-compiling GCC to produce the Linux binaries. Here is some information on setting up GCC on windows to cross-compile for Linux.

Do not expect this to be easy, but it's doable.

Danut Enachioiu
A: 

Writing portable code doesn't mean you can actually build all targets from a single environment. It's probably possible, if you like a lot of pain and hassles for no great reward, but the sane way to do it would be to just have a Linux machine sitting nearby (or in a VMware virtual machine on the same computer). Then a quick login to the Linux box, download the latest source, compile, test, and then go home and watch Futurama instead of sitting up all night trying to figure out how to work a cross-compiler that basically nobody else uses.

Eric Seppanen