-1

I've developed a custom C++ linux library to control a device through my driver. The library controls the flow to correctly use the device and I'd like my users to be able to call the "lib/API" from C and C++ app.

Basically the library is just a class containing public and private/protected variables and methods.

The users should be able to call my library from C and C++ application.

I'd like to hide all those private/protected variables and methods from the header.

  • What's the best practice for my need?

  • Is there a tool out there being able to generate/export automatically:

    • a C header file for the user to develop a C application
      • Basically a struct instead of class and functions with the struct* instead of method
    • a C++ header file exposing only the public variables and methods
      • the class definition will be different then I'm not sure it works

Long time ago, I received a shared library with a C header file with all the functions that looked like it was developed originally in C++.

Edit:

  • Is there a way to have a C header that links to C++ class?

C++ header:

class foo
{
public:
    int foo(bar *bar);
private:
    foo *m_foo;
};

C header equivalent to the C++ header:

struct foo;

int foo_foo(foo* foo, bar *bar);
Alexis
  • 2,136
  • 2
  • 19
  • 47
  • If you want to create a `c` header then you lose everything OOP related. You would need to wrap each interaction from the C-API to your cpp code. Which might not be trivial in each case. – t.niese May 15 '20 at 14:16
  • `Since I'd like to hide all those private/protected variables and methods and prevent my users to change anything in any event` hiding data does not prevent the user to change anything in any event. The data in memory could still be modified by direct memory access. `private`/`protected` is not a mechanism to protect the data from being looked at or intentionally changed. – t.niese May 15 '20 at 14:19
  • @t.niese The library and the inner OOP structure stay valid. Do you say I need to create the header file manually? – Alexis May 15 '20 at 14:24
  • @t.niese If I want to generate a C++ header file, what is the best practice? If I give the header file of my class, all the private/protected var/methods are visible. – Alexis May 15 '20 at 14:25
  • @t.niese Well, I think we are talking about a different kind of protection. I just don't want the user to be able to change anything he shouldn't by mistake. `private/protected` keywords are here especially for that purpose in OOP. – Alexis May 15 '20 at 14:28
  • 2
    https://isocpp.org/wiki/faq/compiler-dependencies#convert-to-c might be insightful or depressing for you – mukunda May 15 '20 at 14:37
  • 2
    "a C++ header file exposing only the public variables and methods" isn't a useful header, because it declares an incompatible type with the same name – Caleth May 15 '20 at 14:46
  • @mukunda I don't want to convert my C++ library to C code. I want to be able to call my C++ lib from C code. The worst thing I should need to do is to write manually a C header using struct and functions instead of class definition. – Alexis May 15 '20 at 14:46
  • @Caleth Thanks, I see, then the only solution is to write a C "wrapper" of my library exposing only the needed functions. – Alexis May 15 '20 at 14:47
  • Swig might be an option, but for just one class it's prob more work than just doing it by hand. – Mat May 15 '20 at 14:49
  • 1
    you could write a C++ facade, with your actual class as a pImpl – Caleth May 15 '20 at 14:52
  • This is strange, you can't call C++ from C _application_. The `main()` of that C application is going to be compiled with C++, so anyway someone will use C++. Honestly, you are looking for something to do the work for you. No, C and C++ are different languages and you can't "translate" one to another. – KamilCuk May 15 '20 at 14:52
  • @Mat Definitely! but that's good to know! Thanks! – Alexis May 15 '20 at 14:53
  • @KamilCuk but there are linkers that can link object code created by some C++ compiler to object code created with some C compiler – Caleth May 15 '20 at 14:56
  • You can always link them together, the symbols will not clash. The problem is in exceptions, static objects constructors and destructors and other parts of the standard library. The linker and `main()` have to be C++-ish, for the compiler to insert code needed to initialize C++ objects, handle exceptions. – KamilCuk May 15 '20 at 15:01

1 Answers1

0

I think this link answered my question about the C header: Can I use shared library created in C++ in a C program?

For the C++ header hiding private/protected, I discovered the PIMPL idiom that seems perfect. Thanks Caleth.

Alexis
  • 2,136
  • 2
  • 19
  • 47