C and C++
-
- KVRer
- Topic Starter
- 19 posts since 25 May, 2014
Is it possible to compile C and C++ together and would I need a piece of code to achieve it?
- KVRAF
- 12555 posts since 7 Dec, 2004
Yes, and no. You can configure the compiler to compile c source as c, and call functions between the two languages.
Ideally however you should convert any c source or libraries to c++ to take advantage of the features of the language which are not present in c, namely features that enable RAII. Not to mention all the other features that blow plain c right out of the water on anything but simple embedded systems.
Ideally however you should convert any c source or libraries to c++ to take advantage of the features of the language which are not present in c, namely features that enable RAII. Not to mention all the other features that blow plain c right out of the water on anything but simple embedded systems.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
-
- KVRian
- 1000 posts since 1 Dec, 2004
You can make a project containing both c++ files and c files (they are recognized using the extension - ".cpp" versus ".c"). You'll probably need use the extern "C" keyword to make the C++ parts and C parts link together.
That being said, like aciddose said, this is not usually a good plan, and within your own c++ project, it's generally better to simply just rename all the ".c" files to ".cpp" (thus turning them into C++ code).
Code: Select all
extern "C" {
void function_name();
}
- KVRAF
- 7889 posts since 12 Feb, 2006 from Helsinki, Finland
Also.. if you have any headers that you want to include from both C and C++ then the canonical way is to write them as C code (obvious, otherwise the C compiler would reject them) and then add #ifdef guards for C++ like this:
This way the C compiler sees a regular C header, and the C++ compiler sees the same thing inside an extern "C" block so it links correctly.
Code: Select all
/* the standard "include once" guard */
#ifndef MYFOOBAR_H
#define MYFOOBAR_H
/* C++ compilers define __cplusplus */
#ifdef __cplusplus
extern "C" {
#endif
/* all the actual header stuff here :) */
#ifdef __cplusplus
} /* end the extern "C" block */
#endif
#endif /* MYFOOBAR_H */
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
C code cannot be compiled by C++ compiler as is.MadBrain wrote:That being said, like aciddose said, this is not usually a good plan, and within your own c++ project, it's generally better to simply just rename all the ".c" files to ".cpp" (thus turning them into C++ code).
- KVRAF
- 12555 posts since 7 Dec, 2004
There are some issues, sure, but in most cases c code will compile fine as c++ with minimal changes.
Of course, again, ideally you'll want to fully convert to c++ to take advantage of the features available. Otherwise you're still just writing c.
Of course, again, ideally you'll want to fully convert to c++ to take advantage of the features available. Otherwise you're still just writing c.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
-
- KVRian
- 1256 posts since 15 Mar, 2007 from Yorkshire, England
Why would a C++ compiler not compile C? Has always worked for me
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
Because if the original C++ derived from C++, C99 is not compatible with C++11 or even less C++03.
-
- KVRian
- 1000 posts since 1 Dec, 2004
You can write stange C99 code that won't compile in C++ yes... but why would you want to do that? Most times that I've seen C code that couldn't be compiled in C++, it was old math-oriented almost-fortran style code full of global variables (good luck multi-threading that). And there are no useful C99 features that would justify this.Miles1981 wrote:Because if the original C++ derived from C++, C99 is not compatible with C++11 or even less C++03.
-
- KVRian
- 1256 posts since 15 Mar, 2007 from Yorkshire, England
yes exactly, most modern C usage will compile fine using a C++ compiler
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
Nope. VLA for instance is not available, restrict keyword is only an extension in some compilers, and simply malloc calls have to be fixed.
So no, it's not strange C, it's basic/proper C that is not compatible with basic/proper C++.
So no, it's not strange C, it's basic/proper C that is not compatible with basic/proper C++.
-
- KVRian
- 1000 posts since 1 Dec, 2004
Why would you want to use variable length arrays? You have std::vector that does this stuff 1000 times better.Miles1981 wrote:Nope. VLA for instance is not available, restrict keyword is only an extension in some compilers, and simply malloc calls have to be fixed.
So no, it's not strange C, it's basic/proper C that is not compatible with basic/proper C++.
The lack of standard "restrict" in C++ is a bit more problematic (g++ uses __restrict__ but MSVC uses __restrict, so you have to use macros) but I'm not sure if it's going to make much difference on x86 (the CPU is already does some alias resolution in hardware).
As for mallocs needing a cast, well, there's not much you can do about that one, true.
-
- KVRian
- 1379 posts since 26 Apr, 2004 from UK
VLA are used in C, as you don't have vectors. I agree that vectors are better (or a scoped_array), but we are talking about C99 code that can be compiled by a C++ compiler, and VLA is quite usual in C99 code as it enables better heap usage and hopefully removes some out-of-bound reads/writes.
-
- KVRer
- Topic Starter
- 19 posts since 25 May, 2014
Thanks for the replys ill look into it more I mainly just wanted to integrate c API with c++.