producing c code from c++ [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Code convert from C++ to C
Two months ago, my instructor have asked one question, I have searched to looking for answer but I could not find it.
question :
From c++ code, how can one generate c code just using console ( with g++ ) .
How can I do this ?

g++ compiles C++ directly to machine code, it does not first compile to C then compile that.
There may be some compilers that compile to C code first. I do not know of any if you really need the code. It is not the most efficient way to do it though.
I think that is what your instructor was trying to ask you, i.e. if there is a compiler switch to generate C code.
Is there any particular reason why you need to generate C code. Creating a C interface can be useful and there are ways to do this.

I'm far from a GNU expert, but I belive the compiler option would be -std=C89, to enforce compilation to follow the C90 version of the C standard (equivalent to old "ANSI C" which was released in 89, hence the C89), or -std=C99 for the C99 version of the standard.

Related

If I treat my C code like C++ code, will it run like normal? [duplicate]

This question already has answers here:
can i use c++ compiler to compile c source code? [duplicate]
(3 answers)
What is the difference between g++ and gcc?
(10 answers)
Closed 2 years ago.
It's what I believe to be a very simple question.
Context: I'm following a tutorial that allows me to run C++ code in Visual Studio Code, but I'm trying to run C code, not C++ code. The program I'm trying to run is a simple Hello World program (shown below), but this question applies to all C code.
#include <stdio.h>
int main() {
printf("Hello World!")
}
C and C++ are different languages. And even though they share a similar syntax, the semantic meaning of certain constructs are different.
C++ incorporates a large part of C, but it also diverges. You cannot just assume that C code compiled as C++ will give the same result.
You can write code that is both valid C and valid C++ yet mean different things in the two languages.
While C++ can be seen for the most part a superset of C, there are some constructions that are invalid C++ and others that have different behavior.
Instead of dealing with that, tell your compiler to target C instead of C++. All the popular C++ compilers also support C (at least one version).

Is the C++ code converted to C during the compilation process? [duplicate]

This question already has answers here:
Do all C++ compilers generate C code?
(5 answers)
Closed 8 years ago.
I have read that the original implementation of C++ by Bjarne Stroustrup was using a compiler named Cfront that converted C++ to C during the compilation process.
Is this still the case with modern compilers (most of them ?) ?
I couldn't find a good answer using Google (or I couldn't find the right search terms).
edit: This is not an exact duplicate because I'm asking for current/modern ones. But both questions & answers apply.
Absolutely not. The CFront way of doing things became untenable long ago. There are some C++ constructs with no C interpretation, especially exceptions, and stamping out literal C source for every template instantiation is a bit ridiculous. The entire reason Bjarne stopped making Cfront is because it was impossible.
It is, however, common to lower the code to a more useful IR like LLVM IR, and GCC also has an internal IR, before converting to machine code.
Short answer: no. Modern C++ compilers generate native code directly.
There's no reason why you can't compile C++ to C, there's just no real reason to do so either any more, so you're adding an extra stage in the compilation process that could just as easily not exist. However, there are still a couple of options if you really need C code output for some reason: the Comeau C++ compiler emits C code with the aim of porting your C++ to platforms where a C++ compiler may not exist (which these days, is very few), and Clang uses LLVM as a backend code generator, which has C as one of its many target instruction languages. (edit: of these options, the first is outdated and the second is no longer maintained)
In neither case does the C look anything like the code you put in: it's significantly less readable than machine code would be. The days of converting method calls to function calls with a this are certainly long gone - it's very much a case of "compiling" rather than "converting".
No, modern compilers, such as GCC and clang (and others based on LLVM) have generally two parts: back-end and front-end.
Front-end handles compiling source code language into some intermediate representaton, such as LLVM IR.
Back-end generates machine code on target platform, possibly using some optimisations from that intermediate form.

Will code written using the C++98 standard compile with new compilers? [duplicate]

This question already has answers here:
What breaking changes are introduced in C++11?
(9 answers)
Closed 10 years ago.
Will code written under the C++98 standard work with newer compilers, such as g++. Or will it not work. Assumedly simple "Hello World" programs would work, but how about complicated command line programs?
The -std= command line argument to g++ allows you to compile against a specific version of the standard.
See: http://linux.die.net/man/1/g++
If in the future the authors of g++ (this applies to any compiler I suppose) decide to default to an incompatible version of the standard, this argument would let you compile older code.
It should only stop working if the authors of g++ drop C++98 support entirely. I see this happening only when the amount of C++98 code becomes so small or the new standards so incompatible with it that it's easier to have people update all their code than to keep supporting it. In any case, it should be a gradual and foreseeable change.

Is there a way of disabling the old c style casts in c++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to find (and replace) all old C-style data type casts in my C++ source code?
I'm currently refactoring some old code and the project I am working on has a policy of only using the new c++ style casts. I'm trying to make sure that I don't miss any but currently the approach I'm taking is quite crude so I'm wondering if there is any way of making the old c style casts not compile in a c++ project? (or at least give a compiler warning if this is not possible)
If you use GCC, add -Wold-style-cast to the command line. That gives warnings, not errors, but you can always add -Werror, which turns warnings (all warnings) into errors.
As for other compilers, it seems no other compiler has such a warning option.
But that doesn't really matter: GCC is Free Software, and available on practically anything that can distinguish between zeros and ones. Just install it alongside your main compiler on your workstation, or into your continuous integration system, and use it for this task only. You will find that having two C++ compilers at hand is very convenient in general.
If installing GCC really isn't an option for you, you might want to take a peek at How to find (and replace) all old C-style data type casts in my C++ source code?, where some alternatives are discussed.
I recommend using this Perl script. Except for unusual conditions like
(void**)&b.ComInterfaceCall
it just appears to work.

can i use c++ compiler to compile c source code? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What issues can I expect compiling C code with a C++ compiler?
Just curious whether I could make use of a c++ compiler to compile c source code??Anyway is there any compiler that fully support c99 standard yet??
C++ is not a superset of C. There are places where they differ, which means some C code will not compile in C++ mode.
As for C99 support, GCC and Clang are the closest. Microsoft does not support C99, and only focuses on C++ (which overlaps with C99 in places).
You might have a problem compiling C code with a C++ compiler unless you explicitly restrict the compiler to use C (which all the C++ know how to do). If the compiler uses C++ to compile C code you might have issues if in the C code you use words that are reserved in C++.
For example, C code like this:
int main(void) { int class = 5; return class;}
Will compile fine with a C compiler (or C++ compiler in C mode), but will not compile with a C++ compiler.
The two problems that I can quickly think of (there's probably more) that would arise when compiling C code with C++ is casting and variable names. For example:
char* new = malloc(20);
The above is valid C, but when compiling in C++ you would get the following errors:
char* cannot be assigned to void* without an explicit cast.
new is a keyword.
Yes, some compilers do support C99. GCC probably does, but I only have experience using MSVC and they don't support it.