Mix C and C++ with CMake - c++

I have a project written in C (on Linux) and now want to use a third-party C ++ library that provides .h and .c source files.
I renamed the .c file to .cpp and then include this library in the C project. However, the following error appears when compiled:
unknown type name ‘class’
Added: The third-party library is here https://github.com/0xmalloc/c-log
The author say it's friendly to both C and C++

There are two options here:
you make your whole project a C++ one - this doesn't mean you need to convert your C code to C++ but rather that you will probably have to touch this and that part of it and also go for a C++ and not C-only compiler
provide wrappers - usually if you write C++ code in C style (no classes etc.) you will only have to do that extern "C" void foo() routine for your C++ functions. However the moment you start working with features that C doesn't support you will have to provide wrappers for your C++ functionality so that it can be exposed to the C part of your application. Such procedure can be from very easy to incredibly complex. For example many modern C/C++ API also provide Python API. In order to do that without having to rewrite everything in Python the authors create wrappers that translate the C/C++ functionality to Python. Depending on how well this is done features of the target language (Python in case we go from C/C++ to Python) can be used to allow better error handling, type checking, readability, native (to the target language) data containers etc.
I do believe that the author of the library misled you since the library is clearly for C++ (the classes in the header are definitely the most obvious thing that just screams C++).

Related

Automatically generate go bindings to a c++ library

I am working on a program which from a c++ code generates a .so and bindings for multiple languages (C, C++, Haskell, Java, Php, Python...) and I'm trying to add support for golang.
I think the best way to get that result is to either use the C++ or the C version and create go bindings, but this has to be done automatically. I've tried using Swig but I didn't manage to get it working (likely because we're including a .so and not a .cc) and I've only found old documentation (this uses go tool 6g).
I've also tried creating bindings with cgo which doesn't work with C++, and I couldn't get it working with the C bindings (again, most likely because of the .so step).
At the end, I must generate another .so with a few exported functions, and which must be standalone which makes compiling the C++ version and talking with it through a pipe (almost?) impossible.
The functions to export are often things like:
void init_game(void);
void play_turn(void);
void end_game(void);
You can find a c++ header here and the auto-generated interfaces to C: interface.hh and interface.cc and prologin.h.
An example source code of lib.so can be found here.

Mixing C and C++ on an embedded system

So I'm having the following problem:
I have an MEMS board that runs on FreeRTOS and includes gyro, accelerometer and magnetometer.
I cannot change any existing code (All in C).
Now I have a basic motion detection library written in C++ and I extended this library with some functions (All in C++).
I thought I can just use a C++ compiler and compile everything but I'm getting hundreds of errors.
I found some solution how to use C functions inside C++ but I don't know how to use C++ functions (or libraries) inside C.
Is there a feasible way? Can I somehow wrap all my C++ code in an easy way?
I'm using Keil uvision to compile the code for my embedded system in case that is important.
There are a few differences between C and C++ that may make a compiler stumble over some of the code. See for example the wikipedia page on this topic.
I'd suggest that you split your project into two projects, one being the RTOS and application in C, the other being the motion detection library. Then you have to write a C wrapper around your C++ library API. Here's a good SO post on writing a C wrapper for C++ code.
Then you would have to link your RTOS + application project to your library, which you compiled in your other uVision project.

Gumbo parser in C++ Builder XE6

I'm trying to use the HTML parser - Gumbo (written in C) in my C++ Builder XE6 project.
When I compile, I get many errors (E2140 Declaration is not allowed here etc.), which appear to be coming from the file char_ref.rl.
I've tried a lot to avoid these errors but I didn't succeed.
Has anyone ever used Gumbo in a C++ Builder project, or at least in a C++ project?
Thank you
Note: extern "C" doesn't mean "compile this code as C". It means that the C++ code inside the block should be compiled so that any external names etc. are published in a way compatible with the C ABI. And such a block shouldn't include any function definitions. You may be using extern "C" incorrectly in your code but it's hard to say without seeing your code.
Anyway, the C compiler part of bcc32.exe doesn't seem to allow mixed statements and declarations, even if you give the flag -An which is supposed to mean "Use C99 keywords and extensions".
You will have to either do a 64-bit build or make a whole bunch of changes to this C source for compatibility with the dinosaur that is bcc32. Or you could build Gumbo as a DLL with a modern compiler (if it supports that option, IDK).

How to build a compiler-independent C++ library (for Solaris Studio and gcc)?

I would like to extend my library, which currently compiles only using gcc, to be used by Solaris Studio as well.
My idea is to do the following:
Write wrapper functions in C, which expose the relevant parts of the interface with extern C linkage.
Then build this library using gcc. The resulting c-header and binary are compiler independent as there is no name mangling anymore.
Include the c-header and link into the project compiled with Solaris Studio.
Question: Is this a feasible approach or is there a better solution to this problem?
Note: Besides name mangling, also watch out for problems related to exception handling.
Your plan is correct.
As long as your library exposes a C API compatible with platform ABI (sizes and alignments of C types, calling conventions) and does not throw C++ exceptions you are not going to have troubles linking your library using other compilers or languages.
You could also add a C++ header only wrapper for your C API to make it easily reusable from C++ and exception safe.

Add code to both iOS/objective-C project and c++ library at once?

I have a usual Objective-C project and MonkSVG li which contains C++ classes.
I want to add some common functions/methods which I could use in both Objective-C and C++ code (for example work with regular expressions). But I don't want to import C++ and .mm classes because for example Xcode doesn't support refactoring in such code.
Currently I duplicate the same functions in both Objective-C and C++ code. Potentially I could write these classes in ANSI C code and extend MonkSVG library, but this language is too limited in comparison with Objective-C and C++.
So are there better ways to resolve this issue?
If you don't want to use Objective-C++ (.mm files) you'd have to go down to the next common level:
Plain C code.
If you're simplied worried about not having refactoring in Xcode for Objective-C++ code you could still use Xcode and at the same time edit your code in something like AppCode.
It understands Xcode project files and does all the refactoring you could want.