Best way to use one C ++ class in a C program

I need to import / translate code from a single C ++ class so that I can use it in a C program.

The C program is large and has many dependencies on C libraries, open and closed.

C ++ Class .cpp File - 650 Lines

I have no experience mixing C and C ++, so although I looked at one guide on how to do this, I'm not sure where to go.

I need to use C ++ code in only a few places (fairly isolated use

I am using gcc (gcc / g ++)

This is linux environment

So what do I need to do to import it? and will it be less time than translation?

Thanks,

Mike

+4
source share
7 answers

Hmm, 650 lines are not too long - I would rewrite it. You will probably spend at least as much time as you want to wrap it, and you can easily find the result.

+11
source

You need to create functions in C ++ that are "extern" C "', so they can be called from C.

You can get OO by making this pointer explicit (and of type void *), while the implementation will cast the pointer and redirect the actual member function.

+6
source

In C ++ code, you must use the extern "C" construct to instruct the compiler / linker to create a compatible link so that C code can call your C ++ code.

extern "C" { void my_function_that_can_be_called_from_c() { // ... } } 

C code knows nothing about objects, so you cannot easily use C ++ objects from C. The usual method is to manipulate C ++ objects inside an "externed" function.

+5
source

Say you have the following C ++ class:

 #if __cplusplus // only C++ programs see this part of foo.h class foo { public: // a couple constructors foo(); foo( int); // and a couple methods bool dosomething(); bool doSomethingElse( std::string const&); private: // a bunch of private stuff which is immaterial to the C interface } #endif 

What you can do is to have a set of C-called functions that wrap the C ++ interface:

 // both C and C++ programs can see this part of foo.h #if __cplusplus // but C++ programs need to know that no name mangling should occur extern "C" { #endif struct CFoo_struct; typedef struct CFoo_struct foo_t; // used as a handle to a foo class pointer // constructors foo_t* CreateFoo( void); foo_t* CreateFoo_int( int); int CFooDoSomething( foo_t*); int CFooDoSomethingElse( foo_t*, char const*); #if __cplusplus } // end the extern "C" block #endif 

Then the implementation in foo.cpp might look something like this:

 // in foo.cpp extern "C" { struct CFoo_struct { }; // constructors foo_t* CreateFoo( void) { foo* pFoo = new Foo; // a bit of ugliness - the need for this cast could be // avoided with some overhead by having the foo_t // struct contain a foo* pointer, and putting a foo_t // structure inside the foo class initialized with // the this pointer. return reinterpret_cast<foo_t*>( pFoo); } // something similar for CreateFoo_int()... // method wrappers int CFooDoSomethingElse( foo_t* pHandle, char const* s) { foo* pFoo = reinterpret_cast<foo*>( pHandle); std::string str( s); return pFoo->doSomethingElse( str); } // something similar for CFooDoSomething() } // end extern "C" block 
+5
source

If you want to turn a C ++ class into a common Linux library available for your C programs, this answer in the previous question will show you how with a small class example.

+1
source

There you can do different things.

You can rewrite it to C. Without actually looking at the code, I don't know how many problems there will be. A lot of C ++ code is just C with several add-ons, and some heavily use templates and overloaded functions, etc.

If you do not, you need to communicate well with C. This means providing an interface for C and its environment with extern "C"{ ... } , so the C ++ compiler will know to make the interface C-compatible. Again, without knowing something from the C ++ code, I can’t say how much this will work. You will need a shell for any of the following solutions.

You can make this a C ++ project, merge each C file with extern"C" { ... } and just link it. If you have C ++ files, the whole compilation should be C ++.

You can create a separate library that you can link to.

What you cannot do is compile C and C ++ with the C main () function or the C compiler. C ++ is more demanding and requires more from the main () function.

You can always try recompiling the C files that you use as C ++ and wrap the .h files for libraries in extern "C" { ... } . A well-written C90 is not that far from legal C ++ (although the C99 standard has moved away from it), and the compiler will note any conversion problems you find.

Which one is the best idea for you depends on questions such as: How easy is it to convert code to C ++? How easy is it to write a C shell for the C ++ functionality you want? How many changes do you want to make to the C code? How familiar are you with creating the Linux library?

+1
source

All Articles