Using a C++ class in C code - c

I was under the impression that it should be possible to mix c and c++ code, but looks like I was wrong.
I'm having a bunch of existing C code and have written a class in C++ that I would like to use in the existing C code.
Is that even possible?

C++ is not a superset of C and thus not all C code is valid C++ code. This is even more true for C++ code in a C compiler. All language additions C++ has are not valid C (classes, generic programming, namespaces). What you could do is compile the result code with a C++ compiler and fix cases where code that was valid for a C compiler isn't for a C++ compiler.

You can't use classes from C code, because classes don't exist in C.
However, you can define a bunch of global functions that access your class, and then you can access those functions from C.

You can mix the two and then compile the result as C++.
If you have a C++ class that you want to use in C then you could remove all the member functions and rewrite then with an extra parameter being a ptr to a structure.
More advance C++ features wouldn't be so easy to incorporate in C.

Related

Declaring a class with microchip xc8 compiler

I was trying to declare a class in a library for a projects using xc8 compiler in free mode and the pic16f876a. But it seems that xc8 compiler doesn't accept or
compiles classes...
Is this true? If not, can somebody post an example?
Thanks!!
There are no classes in the paid compiler, either. XC8 is an ANSI C compiler, not a C++ compiler.
You get structs in C, but no member functions ("methods"), constructors, or destructors, and all member variables are "public".
To answer the question in comment, the compiler appears to be ANSI, with the sole documented exception (a huge one):
"Due to limited memory and no hardware
implementation of a data stack, recursion is not supported and functions are not reentrant." -- MPLAB XC8 C Compiler User's Guide [5.2.1]
I found the guide here.
I can't see any comments in their samples, but I assume that ANSI means no // comments, no variable length arrays, no extended integer and float types. I also don't see the use of much of on a PIC, so "ANSI C" probably refers only to the language and not the standard library.

Looking for a way to access c++ objects in C as if I was in c++

I have a few ideas:
char* test = "testInteger(5).workOnReturn("doIt")[10]"
int ret = execute(test);
What if I use the 'extern' keyword?
Suppose I have a whole bunch of C++ implementations and classes.
Couldn't I just define the same things in C with 'extern' and provide a dummy implementation and on runtime, it would access the C++ library with the actual implementation?
Perhaps you could customize your GCC compiler (e.g. with a MELT extension) to produce somehow a C interface to your library (perhaps by annotating relevant C++ functions with your own #pragma-s ....)
But your question is too vague to get a more precise answer.
What is your C++ library, how should it be used in C++?
Look at how other C++ libraries interface to C; e.g. look inside the source code of PPL
If you want to use C++ from C, it can be done only in an extremely limited way, and as far as the C++ code is explicitly built for it (that means basically throwing away most for what makes C++ C++). Won't happen in your case.
Your ways out are to just use C++ or get a library for C. Anything else will hurt too much to make any sense.

Is D backwards compatible with C if you use the C libraries?

If I import the std.c libaries instead of including the libraries in C, would C code compile with a D compiler, or are there other backwords compatibility issues?
There are several subtleties in D that can make C code not behave exactly as you may want it to. For instance, integer promotion rules are not quite the same (but almost), and initialization rules are different (floating point values -- including arrays of such -- are initialized to NaN, for example). Further, C function pointer syntax was deprecated recently, so you might have to translate some C type syntax to the equivalent D syntax.
In general, though, there is great focus on backwards compatibility, and most C code should compile fine (or with a very minor amount of changes) in D with the same semantics as in C.
Also note that std.c is deprecated; please use core.stdc instead.
Your question is different from the one you ask in the OP body.
Q1: Is D backwards compatible with C if you use the C libraries?
A: Yes. You can use C libraries. More about this here.
Q2: Would C code compile with a D compiler?
A: It was never intention for an implementation of D compiler to be able to compile C code. However, lots of C code would compile because D matches C compiler's data types, layouts, and function call/return sequences. As Zor pointed out C-style function pointer syntax, and C-style array pointer syntax has been deprecated.
You're never going to be able to take a C or C++ file and compile at as D code, and you can't just #include C headers in D. D is not backwards compatible with either C or C++. Rather, it's possible to declare extern(C) functions in your D code and call those C functions as if they were D functions (naturally, you then have to link with the C library that they're defined in). See
Interfacing to C
Interfacing to C++
Converting C.h Files to D Modules
for details on calling C code from D.
druntime (which contains the core.* modules) has declarations for quite a few of the standard C and OS functions (in the core.stdc.* and core.sys.* modules), but you'll have to look at the druntime files yourself to see what they are, because they're not properly documented at this point. For any other C functions that you want to call, you can easily create declarations for them yourself, as described in the links above.
Now, C and D are very similar syntactically, so some sections of C code will compile just fine as D code, but programs as a whole will not. The general rule is that C/C++ code will either compile as valid D code with the same semantics, or it won't compile as D code. There are a few cases where that isn't true (e.g. static arrays are value types in D, unlike C/C++), but it is in almost all cases. This makes porting C/C++ code to D fairly easy, but it was never intended that D be backwards compatible with C code in the way that C++ is.

Can c++ libraries run in c?

I'm sorry if this is a basic question(I'm new to c/c++, but I'm a little confused at how to get the answer. stxxl is a c++ library but some of my code is in c. I know c++ can use c code(my c code is embedded in c++), but does it work the other way around so c can run c++ code?
Their site only mentions c++ but I'm wondering if there's something special that can be done to run c++ libraries within c?
Sorry the books I have read talk about using c code in c++ and the c book I read was written before c++ came out. Right now my c function is sending data to my c++ code which is using the c++ library and then sending results back so I'm thinking I want to test performance if I cut the middle man(c++).
You can link to a C++ library from C only when the C++ library has been designed to be used from C. Specifically, the functions the library provides need to be exported with extern "C" {} block to avoid name mangling, and the interface should be designed in a way to be usable from plain C (i.e. no classes or member functions, only functionless structs and plain functions).
It is worth mentioning that you can compile your C code with a C++ compiler, and it will for the most part be OK. This lets you pretend that your C code is a C++ code, and freely mix in functionality provided through C++ - specific interfaces.
Here's a links that may help you:
How to mix C and C++

Objective-C and C interoperability

We're going to have to write a C library and this library has to be accessible from Objective-C. Now I don't know anything about Objective-C so I wonder how simple Objective-C <-> C interoperability is. Is that as simple as including the C header in the Objective-C code and linking to the library? Is there anything special I'm supposed to do (like the extern "C" bit with C++)?
Objective-C is a strict superset of C. You don't need to use extern "C" or anything like that.
Objective-C is a strict superset of GNU C (note, this is not the same as ISO C90).

Resources