Will a C program compile in a C++ workspace? - c++

I'm new to C/C++ and I have just downloaded the CodeLite IDE (any other suggestions for free ones would be welcome).
I intend to write a C program, but it only gives me an option to create a C++ workspace. Can I write my C in this workspace all the same?

It depends on your IDE, your compiler (which is invoked by IDEs to compile code, and usually [not always] not a part of the IDE), and the type of code you write.
Practically, quite a few C++ compilers have a "C mode" - for example, command line switches or other settings that may be configurable through your IDE - to compile as C. You'll need to read your compiler and IDE documentation. Bear in mind that some compilers, even in C mode, support C++ features that are not valid in C (and that the reverse is also true).
It also depends on what sort of C code you intend to write, and how well you know the features of C that are not part of C++, and vice versa.
But, yes, as a general rule you can write a C program in a C++ workspace. Just be aware of concerns like the above when doing so.
Also, don't be surprised when you proudly show some of your lovingly crafted code to a seasoned C or C++ developer, only to be informed you are writing a hybrid of C and C++. Unfortunately, writing a hybrid of C and C++ is considered poor form by both C and C++ developers - mostly because the code is likely to work with some compilers and fail with others. Too many compiler vendors intermix C and C++ in their documentation, and too much learning material (introductory texts, etc) does the same. If writing C code in a (suitably configured) C++ environment, there is a fair chance your compiler won't complain about your code doing that. This isn't a problem inherent to writing C in a C++ environment - too many vendors of C compilers support C++ features as an extension, and vice versa - but it is a phenomenon you are more likely to encounter if you write your C code in a C++ environment, unless you go out of your way to learn the differences between standard C and standard C++ independently of your compiler and compiler documentation.
I've lost track of the number of beginners who end up writing code that is some hybrid of C and C++ (e.g. using C++-specific features while believing they are writing C, or vice versa). Only to have their code break when they build it with a different compiler (or, worse, an update of their compiler).
If you actually intend to write code that includes a mix of C and C++ (by which I mean pure C functions interfacing with pure C++ functions, not a hybrid like I mentioned above), you will need to allow orchestration of the build process - particularly linking - to be controlled by C++ settings, even if compilation settings for particular files mean they are compiled as C or C++.

Yes its possible. You must use your C++ compiler when compiling main() (e.g., for static initialization)
Your C++ compiler should direct the linking process (e.g., so it can get its special libraries)
Your C and C++ compilers probably need to come from the same vendor and have compatible versions (e.g., so they have the same calling conventions

Non-trivial C programs will not compile as C++ code without modification — C is not a subset of C++. See Where is C not a subset of C++?
Set compiler options to treat your code as C.
For free compiler with IDE on linux check this one:
Code::Blocks and
for windows:
Microsoft Visual Studio Community
You can find extensive list of free C and C++ compilers here.

Related

Compile GNU C project with C++

I just joined a project and there's a bunch of code (which has been written by 4 or 5 different people over the last 5 years). It's all C at this point, but I want to migrate the project to C++ in order to take advantage of OOP.
Step 1 will be to simply leave all the existing code in place and just selectively convert very small pieces of the functionality to be object oriented.
It's my understanding that C can be compiled by C++, but maybe I'm wrong about this.
So, before I invest a whole lot of time in trying to actually do it, am I likely to be able to get the existing C code to compile using C++ relatively easily?
Or, is this likely to involve a massive migration before it will work?
What factors affect the complexity of the task I'm proposing?
So to be clear - I'm not asking how big a project it will be to convert all of the code to objects, functions to methods, etc - but rather, just what I'm likely to face getting the existing C code to compile using the C++ compiler instead of the C compiler.
Thanks in advance - I look forward to any help/guidance you can provide.
EDIT
Sounds like it's really not a good idea. I always thought of c++ as an expansion of C, but it sounds like they're different enough that this is not an advisable path. Ultimately, I wasn't thinking of them as Different Languages, and was hoping that c++ was more backwards compatible with c than it sounds like it is. My initial searching made it look like people had done it to varying degrees of success, but the responses here make me think I should really just consider them to be two different languages. Thanks!
I always thought of c++ as an expansion of C, but it sounds like they're different enough that this is not an advisable path.
C++ is not an expansion of C. They are completely different languages. Compiling C code with C++ is not simply enabling "OO-mode". When you compile anything but trivial C code with a C++ compiler, you are potentially opening yourself up to a world of hurt due to the differences between the two languages.
am I likely to be able to get the existing C code to compile using C++ relatively easily?
Again, you have a fundamental misunderstanding -- C++ is not some extended version of C. They are different languages.
The best course of action is to slowly migrate your code to C++, but using two different compilers. Compile the C code with a C compiler, and compile the C++ code with a C++ compiler. It is not a challenging feat since any modern compiler suite (like GCC and clang) can compile both C and C++ very well.
is this likely to involve a massive migration before it will work?
If you intend to convert your code to C++, then yes.
One thing is worth mentioning: if you are compiling your C code with a C++ compiler, and calling it programming in C++, you're missing the spirit of the C++ development environment. If you're programming in C++, but using C code with very little OO, then what's the point of using C++? Just skip the overhead of OO and code in C. Using a C++ compiler is not some magic process that will instantly make your code better.
I think you should have a look here to get a list of (possible) issues.
What I did in the past for a C to C++ mgration was to leave the C code working as it should (C code compiled with a C compiler) and - in parallel - rewrite the code from scratch in C++.

In what languages are the C and C++ standard libraries written? Where is their source code?

I always wonder what language was the C/C++ runtime and standard library written in. At first I thought it is a casual C/C++ language, but to be able to talks with the machine I doubt C/C++ is enough. Therefore, I think it may be assembly language instead. If it is either C/C++ or asm language, then why don't I really see source codes flying around? or maybe I'm lacking in searching skill...
They are typically written in their host language, interoperating with the operating system API to obtain things that can't be obtained natively. Many features are written in pure language- for example, the containers & algorithms section of the C++ Standard library pretty much has to be written in C++. In fact, nearly the entire C++ Standard library has to be written in C++, because it's templated. I don't know why you haven't found any source- the Microsoft CRT source is available to any dev, I think, I've certainly seen questions on here posting the CRT source, and the GNU libc++ is open source, I'm pretty sure.
All the run-time and standard libraries are written in C/C++. Most vendors ship the source code.
They are usually written in C with some assembly mixed in. Visual Studio ships with big chunks of source code for CRT (C RunTime) if not all of it. Linux glibc is obviously open source:
http://ftp.gnu.org/gnu/glibc/
Most of the runtime is written in C or C++ but there is an important exception and that is the code that calls into the kernel. Onmost operating systems this is done by generating a Software Interrupt using a special instruction (SWI or SVC) on arm. There is no equivalent for this in C or C++ and therefore assembler will be used. Assembler is also typically used to implement highly optimized memcpy, memmove, memcmp and other similar functions.

Why there's no dedicated compiler for c or c++?

Seems all compilers can deal with both c and c++,like gcc ,msvc...
Is it because these 2 languages are exactly very much alike?
Actually, GCC (GNU Compiler Collection) has two different front-ends, gcc and g++. To specify C++, you can also use a .cpp (or a few others) extension, or -x c++, when executing gcc. However, this requires extra options (like linking in the C++ standard library).
cl, Microsoft's C++ compiler, does not support modern C. However, it will compile C source files as a variant of C89, and you can specify this explicitly with /TC.
For both, you are correct that there's a lot of shared code, regardless of which front-end is used (GCC also has many more). However, the languages do have significant differences, which are discussed elsewhere (this question among others).
There is no dedicated compiler for C/C++ because there is no such language....
If you are going to write a C++ compiler then you will have to be able to compile C too, so you may as well also provide one.
There may still be some C compilers around that do not have a companion C++ compiler with them though.
No. That's not true. Look at Pelles which is a C only compiler.
The semantics of the core language constructs for C and C++ remain more or less identical, and C++ was designed to add structural elements to C rather than change or remove existing language features. Therefore if you go to the trouble to build a C++ compiler, having it also compile C is relatively trivial (at least for ISO C90). C99 diverges in some significant ways from C++, and some C++ compilers either do not support C99, or include C99 features as extensions in their C++ compiler.
C++ is also highly inteoperable with C, C++ for example wholly includes ISO C90's standard library, and can link any C library. C++ libraries can be given a C linkage compatible interface for use by C code (although that is often less straightforward than C++ calling C code).
Early C++ tools were not true compilers, but rather C++ translators, which generated C code for compilation by a native C compiler. Comeau C++ still takes this approach in order to support C++ on any target with a C compiler, which is useful in embedded environments where some targets are not well served by C++ tools.
TCC is an example of a C compiler which is not a C++ compiler. Actually compiling C++ is a huge pain; the only reason so many C compilers also support C++ is that there's a pretty big demand for C++.
C++ is a superset of C. I don't know if this is still true, but it at least used to be common for c++ compilers to convert code to C as a first step in compiling.
Edit:
I've always heard that it is a superset. Since GMan says no, I looked at Wikipedia which says, "C++ is often considered to be a superset of C, but this is not strictly true.[21] Most C code can easily be made to compile correctly in C++, but there are a few differences that cause some valid C code to be invalid in C++, or to behave differently in C++." (See http://en.wikipedia.org/wiki/C%2B%2B for details.) So I stand a bit corrected.
Edit 2:
I've read a bit further in the Wikipedia article. Sounds like this is more accurate: C++ started as C; C++ evolved by adding new features to C. At some point, C++ changed enough to no longer be a pure superset of C. Since then, C has also evolved, and now has some features that C++ doesn't. So they're closely related, but no longer wholly compatible with each other.
No, LCC compiler is for C only.
GCC has gcc and the g++ components which compile C and C++ code.
clang-llvm has a C front-end. There is an experimental, separate C++ front-end.
IBM's Visual Age is split into xlc and xlC compilers.
Portable C Compiler is C only.
Last time I used it, Labwindows/CVI suite by National Instruments was a C only compiler.
It isn't true, there are several, and there were plenty in the 1980s before C++ compiler products started to appear :-) However given a C++ compiler the marginal cost of producing a C compiler out of the same codebase is relatively small, and even going the other way isn't a major increment, at least compared tom starting from scratch with either.

Do all C++ compilers generate C code?

Probably a pretty vague and broad question, but do all C++ compilers compile code into C first before compiling them into machine code?
Because C compilers are nearly ubiquitous and available on nearly every platform, a lot of (compiled) languages go through this phase in their development to bootstrap the process.
In the early phases of language development to see if the language is feasible the easiest way to get a working compiler out is to build a compiler that converts your language to C then let the native C compiler build the actual binary.
The trouble with this is that language specific constructs are lost and thus potential opportunities for optimization may be missed thus most languages in phase two get their own dedicated compiler front end that understands language specific constructs and can thus provide optimization strategies based on these constructs.
C++ has gone through phase 1 and phase 2 over two decades ago. So it is easy to find a `front end' of a compiler that is dedicated to C++ and generates an intermediate format that is passed directly to a backed. But you can still find versions of C++ that are translated into C (as an intermediate format) before being compiled.
Nope. GCC for example goes from C++ -> assembler. You can see this by using the -S option with g++.
Actually, now that I think about it, I don't think any modern compiler goes to C before ASM.
No. C++ -> C was used only in the earliest phases of C++'s development and evolution. Most C++ compilers today compile directly to assembler or machine code. Borland C++ compiles directly to machine code, for example.
No. This is a myth, based around the fact that a very early version of Stroustrup's work was implemented that way. C++ compilers generate machine code in almost exactly the same way that C compilers do.
As of this writing in 2010, the only C++ compiler that I was aware of that created C code was Comeau*. However, that compiler hasn't been heard from in over 5 years now (2022). There may be one or two more for embedded targets, but it is certainly not a mainstream thing.
* - There's a link to their old website on this WP page. I'd suggest not clicking that unless your computer has all its shots up to date
This is not defined by the standard. Certainly, compiling to C-source is a reasonable way to do it. It only requires the destination platform to have a C-compiler with a reasonable degree of compliance, so it is a highly portable way of doing things.
The downside is speed. Probably compilation speed and perhaps also execution speed (due to loads of casts for e.g. virtual functions that prevents the compiler to optimise fully) will suffer.
Not that long ago there was a company that had a very nice C++ compiler doing exactly that. Unfortunately, I do not remember the name of the company and a short google did not bring the name back. The owner of the company was an active participant in the ISO C++ committee and you could test your code directly on the homepage, which also had some quite decent ressources about C++.
Edit: one of my fellow posters just reminded me. I was talking about Comeau, of course.

Is C++ built on top of C?

Does C++ code gets converted to C before compilation ?
A few C++ compilers (the original cfront, Comeau C++) use C as an intermediate language during compilation. Most C++ compilers use other intermediate langauges (e.g. llvm).
Edit: Since there seems to be some misunderstanding about the history: "C with classes" started out using a preprocessor called "Cpre". At that time, it was seen strictly as a dialect of C, not a separate language in itself. In December 1983, people were starting to view it as a separate language, and the name C++ was invented. As it happens, development of cfront started in April 1983, so a reasonably usable version became available (to a select few) just about the same time as the name "C++" came into use. This appears to be mostly coincidence though.
As far as producing C as its output, that was really quite common on Unix. Just for example, the Berkeley Pascal compiler and at least a couple of Fortran compilers also produced C as their output.
There is, however, a huge difference between Cpre and Cfront. Although both produced C as their output, Cpre did virtually no syntax checking of its own -- it looked for a few specific things, and did a relatively mechanical translation on them. It wasn't until the C compiler looked at the result that real syntactical analysis was done. If your code contained a syntax error, it was almost certain that it wouldn't be caught until the C compiler parsed the output from Cpre.
Cfront, however, did full syntactical analysis of the source code itself, so (short of a bug in its code generator) you'd never see a syntax error from the C compiler. The C compiler was simply used as a code generator so nobody needed to rewrite CFront to accommodate different processors, object file formats, etc.
If you want to get into more detail, chapter 2 of The Design and Evolution of C++ is devoted almost entirely to the "C with Classes" time frame (and there are various other details about it spread throughout the book).
No, but like most myths there's a shred of truth to this. The original compiler for C with classes (which later became C++) was nicknamed CFront and did translate to C.
Not in most modern compilers.
The original C++ compiler was actually a preprocessor however. It generated C code, which was then compiled by a C compiler.
In the early days of C++ compilers, some did it that way. I haven't seen a C++ compiler implemented that way since the late 1980s however.
As others have answered. NO.
However if you want to use an OOP language like C#, and have your code compiled into C I recommend you take a look at Vala.
the title seems to ask is C++ a superset of C, i.e. can you just dump any c code in a c++ compiler and it will work? In which case, yes it is, sort of...
one major difference is that C automatically casts pointers for you, c++ does not, you need to cast the manually...
any one remember anything else?
thats all I remember from the horrible process of converting a massive C project to compile under c++ for some reason...