Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it important for C / C++ Code to be compilable on different compilers?

I'm interested in different aspects of portability (as you can see when browsing my other questions), so I read a lot about it. Quite often, I read/hear that Code should be written in a way that makes it compilable on different compilers.

Without any real life experience with gcc / g++, it seems to me that it supports every major platform one can imagine, so Code that compiles on g++ can run on almost any system. So why would someone bother to have his code run on the MS Compiler, the Intel compiler and others?

I can think of some reasons, too. As the FAQ suggest, I'll try to post them as an answer, opposed to including them into my own question.

Edit: Conclusion

You people got me completely convinced that there are several good reasons to support multiple compilers. There are so many reasons that it was hard to choose an answer to be the accepted one. The most important reasons for me:

  • Contributors are much more likely to work an my project or just use it if they can use the compiler of their choice
  • Being compilable everywhere, being usable with future compilers and tools, and adhering to the standards are enforcing each other, so it's a good idea

On the other hand, I still believe that there are other things which are more important, and now I know that sometimes it isn't important at all.

And last of all, there was no single answer that could convince me not to choose GCC as the primary or default compiler for my project.

like image 458
Lena Schimmel Avatar asked Dec 31 '09 18:12

Lena Schimmel


People also ask

Why are there different C compilers?

There are over 50 compilers for C like ICC by Intel to GNU GCC by GNU Project. The focus of having multiple compilers is to optimize the compiled C code for specific hardware and software environments.

Why different compilers give different results?

This is because there is no sequence point anywhere in that expression. The only requirement is that each of those subexpressions is evaluated before the result is needed (that is, before the result is to be printed).

Why compiling is important in C program compile?

Its purpose is to provide an intuitive way for humans to provide instructions that can be easily converted into machine code that is comprehensible to a microprocessor. The compiler is what translates our human-readable source code into machine code.

Why does program code have to be run through a compiler?

Because computer can't understand the source code directly. It will understand only object level code. Source codes are human readable format but the system cannot understand it. So, the compiler is intermediate between human readable format and machine-readable format.


2 Answers

Some reasons from the top of my head:

1) To avoid being locked with a single compiler vendor (open source or not).

2) Compiling code with different compilers is likely to discover more errors: warnings are different and different compilers support the Standard to a different degree.

like image 96
Nemanja Trifunovic Avatar answered Nov 07 '22 18:11

Nemanja Trifunovic


It is good to be compilable on MSVC, because some people may have projects that they build in MSVC that they want to link your code into, without having to set up an entirely different build system.

It is good to be compilable under the Intel compiler, because it frequently compiles faster code.

It is good to be compilable under Clang, because it can give better error messages and provide a better development experience, and it is an easier project to work on than GCC and so may gain additional benefits in the future.

In general, it is good to keep your options open, because there is no one compiler that fits all needs. GCC is a good compiler, and is great for most purposes, but you sometimes need something else.

And even if you're usually only going to be compiling under GCC, making sure your code compiles under other compilers is also likely to help find problems that could prevent your code from working with past and future versions of GCC, for instance, if there's something that GCC is less strict about now, but later adds checks for, another compiler may catch in advance, helping you keep your code cleaner. I've found this helpful in the reverse case, where GCC caught more potential problems with warnings than MSVC did (MSVC is the only compiler we needed to support, as we were only shipping on Windows, but we did a partial port to the Mac under GCC in our free time), which allowed me to produce cleaner code than I would have otherwise.

like image 38
Brian Campbell Avatar answered Nov 07 '22 19:11

Brian Campbell