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.
Related
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).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have read that most of the new C++ compilers can compile both C and C++ programs and also read that not to use the extension .cpp to a C programming file so as to inform the compiler to compile in C. What is the difference in "C compile" and "C++ compile" using a C++ compiler. Is there any problem when a C program is given extension .CPP and compiled with a C++ compiler ?
Given a C program with C++ constrains( like not using typename, new, private, class etc as Identifiers ) how differently does a C++ compiler compile the C program with .CPP extension compared to a C compiler.
What is the difference in "C compile" and "C++ compile" using a C++ compiler.
Each interprets the source code according to the rules of a different language. One interprets it as C, the other as C++.
Is there any problem when a C program is given extension .CPP and compiled with a C++ compiler ?
Yes. Although C is very similar to a subset of C++, there are plenty of cases where valid C is not valid C++. For example, some keywords are reserved in C++ but not C:
int private = 42;
int class = 7;
and there are some type conversions allowed in C but not C++
char * array = malloc(32);
Any of these, and more besides, will cause a C++ compiler to fail if you lie about which language it's compiling.
C++ compilers compiling C code is not a new feature, but a design strategy of C++ from the onset. However, a lot of perfectly valid C constructs don't compile as valid C++. Some of them are inoccuous, such as not being able to use names like new for variables, but some, such as mandatory casts of malloc result, require making valid C code less maintainable. Those changes make sense in C++ which offers "better" options to do the same, but not in C. Also, with the later evolution of C, some C99 features are actually not present and will never be present in C++. Thus using a C++ compiler may require you to modify perfectly valid C code, which is a non-trivial undertaking if it's written by others and can introduce defects.
Because of this it is much better to leave it to the C compiler - or, more precisely, the C frontend of the compiler suite you are using - to compile C code.
C and C++ are really different languages and they have really different compilers. But c compiler is compatible with c++ compiler, so yes, you can build c program with c++ compiler.
But the good way to do it is use extern "C" like this (definition __cplusplus is in gcc and compatible compilers)
YourFile.cpp
#ifdef __cplusplus
extern "C" {
#endif
//Code of your program
#ifdef __cplusplus
}
#endif
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.
Have not found the exact question i am asking in either google or here, everything talks about wanting to call c++ from c code or some part being compiled with c compiler and some other with c++ and then later linked together and the problems that arise from that which i do not want.
I want to compile and link C99 files with C++ compiler of Visual Studio in my all C++ application and be able to call the c functions without errors and problems.There will be no c linker involved or compiling some part with different compilers and linking together later, or any kind of trick. The headers are from C library (libcurl) and some others as i want to use them in my application. I do not want to use the C++ bindings i want to compile c code as c++. Can i trust c code be compiled as C++ code without major refactoring? What to do differently than when including C++ headers? What incompatibilities to expect?
In theory, C code should be able to be compiled as C++ code. At some point Dr.Stroustrup made the point that all code from ANSI C edition of the K&R compiles with a C++ compiler and has the same semantics as the code compiled with a C compiler has (this was construed that all ANSI C code would be valid C++ code which is, obviously, not the case, e.g., because many C++ keywords are not reserved identifiers in C).
However, certain idioms in C will require substantial changes to the C code if you want to compile the code with a C++ compiler. A typical example is the need to cast void* to the proper type in C++ which isn't needed in C and it seems it is frowned upon casting the result from malloc() to the proper pointer type although the effect is that it prevents the C code from being compiled with a C++ compiler (in my opinion a good think, e.g., because there the tighter rules may result in discovering problems in the C code even if the production version is being compiled with a C compiler). There are also a few subtle semantic differences as far as I know, although right now I can't easily pin-point one of them. That is, the same code compiled with a C and a C++ compiler may have defined but different results for both cases.
In practice, I doubt that you can simply compile a non-trivial body of C code with a C++ compiler and get a program which behaves the same as the original C code. If the C program you envision to compile with a C++ comes with a thorough set of test cases it may be feasible to port the code to C++ but it will involve more work than merely renaming the file from <name>.c to <name>.cpp. I could imagine that a tool could do the required conversions (a compiler compiling C source to C++ source) but I'm not aware of a such a tool. I'm only aware of the opposite direction yielding entirely unreadable code (for example Comeau C++ uses C as a form of portable assembler).
If you want to do this using visual studio, then it is not possible. MSVC doesn't support C99.
C and C++ are two different, but closely related, languages. C++ is nearly a superset of C++, but not quite (in particular, C++ has keywords that C lacks).
If your code depends on C99 features (i.e., features that are in C99 but not in C90), then you may be out of luck. Microsoft's C compiler does not support C99 (except for a few minor features; I think it permits // comments), and Microsoft has stated clearly that such support is not a priority. You may be able to modify the code so it's valid C90, depending on what features it uses.
Microsoft Visual Studio supports compiling both C and C++ (though it tends to emphasize C++). If you can get your C code compiling with the MS C compiler, I suggest doing just that rather than treating it as C++. C++ has features, particularly extern "C", that are specifically designed to let you interface C and C++ code. The C++ FAQ Lite discusses this in section 32.
If you really need to compile your C code as C++ for some reason, you can probably do so with a few minor source changes. Rename the source file from foo.c to foo.cpp, compile it, and fix any errors that are reported. The result probably won't be good C++, but you should be able to get it to work. There are a few constructs that are valid C and valid C++ with different semantics, but there aren't many of them, and you're not likely to run into them (but you should definitely keep that in mind).
If you want to continue maintaining the code as C++, my advice is to go ahead and make the changes needed to do that, and then stop thinking of it as C code.
The actual need to compile the same code both as C and as C++ is quite rare. (P.J. Plauger, for example, needs to do this, since he provides some libraries intended to be used in either language.) In most cases, C++'s extern "C" and other features are good enough to let you mix the two languages reasonably cleanly.
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.