C and C++

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

Is it possible to compile C and C++ together and would I need a piece of code to achieve it?

Post

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.
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.

Post

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.

Code: Select all

extern "C" {
   void function_name();
}
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).

Post

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:

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 */
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. :)

Post

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).
C code cannot be compiled by C++ compiler as is.

Post

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.
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.

Post

Agreed.

Post

Why would a C++ compiler not compile C? Has always worked for me

Post

Because if the original C++ derived from C++, C99 is not compatible with C++11 or even less C++03.

Post

Miles1981 wrote:Because if the original C++ derived from C++, C99 is not compatible with C++11 or even less C++03.
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.

Post

yes exactly, most modern C usage will compile fine using a C++ compiler

Post

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++.

Post

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++.
Why would you want to use variable length arrays? You have std::vector that does this stuff 1000 times better.

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.

Post

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.

Post

Thanks for the replys ill look into it more I mainly just wanted to integrate c API with c++.

Post Reply

Return to “DSP and Plugin Development”