Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of compiling C11 to C89?

One of my (embedded) targets only has a C89 compiler.

I am working on a (hobby) project which targets multiple devices.

Is there a way of compiling (transpiling?) a C11 code base into C89?

(Otherwise I will have to code like it's 1989, literally.)

like image 514
fadedbee Avatar asked Oct 01 '13 16:10

fadedbee


People also ask

Does clang support C11?

C++11 implementation statusYou can use Clang in C++11 mode with the -std=c++11 option. Clang's C++11 mode can be used with libc++ or with gcc's libstdc++.

Should I use C99 or C11?

It is best to use C11 as that is the current standard. C99 and C11 both contained various "language bug fixes" and introduced new, useful features.

What is the difference between C89 and C99?

In C89, the results of / and % operators for a negative operand can be rounded either up or down. The sign of i % j for negative i or j depends on the implementation. In C99, the result is always truncated toward zero and the sign of i % j is the sign of i. In C89, declarations must precede statements within a block.

What is C99 and C11?

C11 (formerly C1X) is an informal name for ISO/IEC 9899:2011, a past standard for the C programming language. It replaced C99 (standard ISO/IEC 9899:1999) and has been superseded by C17 (standard ISO/IEC 9899:2018).


2 Answers

No I don't think that it is possible for all of C11. C11 has features that simply not exist in C89 or C99: _Generic, _Atomic, _Thread, _Alignof, well defined sequenced before ordering, unnamed struct and union members ... These don't have counter parts in the older versions and would be really difficult to emulate.

For any of these features you would have to rely on extensions of your target compiler, so probably possible for some of the features for one given compiler. But it would be a nightmare to write such a tools that would have plain C89 as a generic target. You'd better implement a C11 compiler directly.

like image 98
Jens Gustedt Avatar answered Oct 12 '22 19:10

Jens Gustedt


As Carl Norum comments:

What's your target? Would it be hard to port clang/llvm?

This seems to be the promising approach.
It's not necessary to port your target, a port for C89 is enough.

So clang compiles your code to llvm and then llvm to c89 and then you get it.

like image 23
jeb Avatar answered Oct 12 '22 20:10

jeb