1

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?

2

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: You can't do that. Read bmargulies' post
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.
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!
4

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.

Comments

1

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

You can also try your luck with Wine.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.