If I write in C or C++ on for example: Windows. Is it guaranteed than I can compile and run it on any other operating system such as Mac OS, Linux, Unix-like systems? So, does it mean C or C++ is cross-platform language?
-
6It really depends on the libraries you'll be using. – noko Nov 30 '12 at 03:33
-
1http://stackoverflow.com/questions/3103568/how-to-write-portable-code-in-c – jonvuri Nov 30 '12 at 03:33
-
strictly speaking, C and C++ don't *run* on any platform. They are just languages that can be compiled to runnable programs – phuclv Mar 13 '14 at 05:27
4 Answers
If you write a C or C++ program that strictly complies with some standard, then the program should work on any platform that supplies a tool chain that complies with that standard. In that sense, C and C++ are cross-platform languages.

- 179,497
- 17
- 214
- 278
-
5It's important to make clear, I think, that many of the things one might want to do in a program require libraries that may *not* be cross-platform. Have to pick your libraries well. – Michael Petrotta Nov 30 '12 at 03:36
-
11+1. Based on your careful selection of words, you'd make a good lawyer. – jonathan.cone Nov 30 '12 at 03:37
-
So, if I have two same compilers running on different OS then it should work regardless of what OS, right? – user963241 Nov 30 '12 at 03:40
-
2@user963241 Not unless you stick to a set of libraries and (importantly) system APIa that are available on both platforms. If you're trying to write "native" application on either Windows or Mac OS you'll have to use APIs that are not trivially available on other platforms. – dmckee --- ex-moderator kitten Nov 30 '12 at 03:43
-
The point of my answer is that while what it says is strictly true, the carefully-worded set of conditions make it not all that useful for typical, real-world applications. It's very difficult to write useful real-world applications that *strictly* comply with any standard that's available on a wide range of platforms. Some kind of tweaking for different platforms is needed in almost any realistic application. – David Schwartz Dec 03 '12 at 20:03
No.... there are C and C++ compilers for many many many platforms, but different compilers have their own quirks, and the libraries they link to are completely different on various platforms. Mozilla had a guideline of what features to use and what to avoid to make your software cross platform.
There are environments like cygwin that help with cross platform compatibility in windows *nix.
You can write libraries that are stock standard C that don't have dependencies on platform libraries that will be pretty portable

- 43,549
- 15
- 93
- 156
If you directly access any Windows API, it will fail to run (or even compile) on other platforms. If you use a standard function which indirectly accesses the correct API, or if you add #ifdef guards and access the correct platforms API, then the answer is better. The former should be cross platform. Latter will work on platforms your code caters to.

- 31,456
- 5
- 68
- 87
For Mac OSX, Linux, and, of course, Windows, you can write and compile C++. From personal experience, I've always found Windows to be easiest for C++ usage, closely followed by Linux, and with Max OSX trailing distantly behind. The compilers tend to be temperamental in my experience, and either because the support community is better for C++ on Windows, or because it's naturally better for programming, I've always had less problems with Windows. Though I labeled it in second place, I don't have that much experience in Linux.
Edit: You say "guaranteed" to run and compile. For basic C++ this is definitely the case, but some more advanced features may have varying support across platforms.

- 11,093
- 25
- 85
- 121