I mean,let's say we write a console (windows) application to display a text.
it woruld be

console.write("whatever");
  1. do we have to use different compilers for both windows and linux?what are the names of them
    2.is console.write a windows only code? even if we compiled for Linux, will it run on linux? why

Firstly, source code does not run; it is compiled. It is possible to compile the same source code on multiple platforms but only if you write portable code. Using system-specific utilities will tie to you a particular platform and porting will be next to impossible.
It's aslo unclear whether console.write is windows-specific. It could be part of a multi-platform library or something specific to a platform but it is hard to tell without the context.

can the same source code run in different operating systems ? how?

The same source code can be compiled for different operatings systems (and overall platform). There are about 4 kinds of "source code": the standard library, the operating system's API, third-party libraries, and your own code. The standard library is provided to you along with the compiler (GCC, MSVC, ICC, etc.), and it was either specifically made for the target platform (e.g., MSVC) or configured to work well on the target platform (e.g., GCC). The functions and classes available in the standard library are, well, standard, so they apply across the board, all conforming compilers must provide it on each platform that they provide compilers for (with some exceptions for restricted environments like micro-controllers).

The operating system's API is obviously platform-specific. If you need to use it, you must be aware that it will only work on that platform. If you want to do the same thing (e.g., open a network connection) on different platforms, you must use conditional compilation, typically looking like this:

#ifdef _WIN32
 // put Windows-specific code here..
#else
 // put Unix/Linux-specific code here..
#endif

Third-party libraries (or external libraries) will vary depending on whoever wrote them. Most library implementers try to be as cross-platform as possible with their implementations. The way to do that is to use only use standard libraries and other external cross-platform libraries, and use the above techniques whenever a operating system API feature is needed directly. You also need to be very pedantic about the code, because every compiler and platform has got its little set of quirky things, some exceptions to the standard language rules, extensions to it, etc.

As for your own code, it's the same as the rules for writing cross-platform libraries, as long as you follow those rules, you're fine. As long as your code is compileable by any compiler (i.e., your code strictly abides to the standard and handles the few quirks of some compilers), and as long as all your dependencies are cross-platform, which is trivially the case for standard libraries, but something to watch out for with external libraries, then you should be able to compile and run your same code across platforms.

do we have to use different compilers for both windows and linux?what are the names of them

It depends, some compilers are cross-platforms, others are not. Microsoft compilers are all Windows-only. Professional compiler suites like GCC (GNU Compiler Collection) and ICC (Intel Compiler Collection) provide support for just about every platform imaginable (most of which are Unix-like, with Windows as an exception). Compilers generally have the same name on any platform. Then, some specialized compilers will be restricted to Unix environment (which is not very restrictive since most specialized applications run on a Unix-like environment anyways). It varies, but most main-stream compilers are cross-platform (i.e., you can get a version of it for almost any platform) and have the same name everywhere, the list mostly includes GCC, ICC, and Clang.

is console.write a windows only code? even if we compiled for Linux, will it run on linux? why

Well, this is a C++ forum, and console.write() is certainly not a standard C++ code, not even a Win32 API function (since the Win32 API is only in C, not containing any classes). My guess is that this function is the .NET function Console.Write(). .NET is pretty much exclusively Windows-based (except for some attempts to support it partially on other platforms), it does not work with C++ (but with C++/CLI, a hybrid language between C++ and C#), and, if anything, would fall under the category of OS API calls that I described above. And by the way, .NET code is never fully compiled anyways.

yeah it is a C# code. So I am guessing, this "console.write() won't be showing anything on a Linux OS if we compile it using another compiler(other than the Visual studio) ?

if we take a big applications that works on both Linux and Windows,
it must use to use this format

#ifdef _WIN32
 // put Windows-specific code here..
#else
 // put Unix/Linux-specific code here..
#endif

if not, the developer will have to write two different source codes?
thanks for the answers!

The best way is to use portable libraries such as QT and boost. That way you write just one program without all those preprocessor statements. There are quite a few cross-platform libraries to choose from, which one you choose will depend on what you are trying to accomplish.

I've heard that there is a C# compiler for *nix, but I've never tried it. You might want to read a few of these google links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.