-4

I need to write my 4-5 .cpp and .h file to c code. In C++ code we have defined a class, constructor, destructor, function.

How to convert them in C code? Can somebody give me example of it so that i can implement it or provide link so that I can better explore it to my C code? How to implement all the functionality i mean constructor,destructor,functions of class in C ?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
Neel Patel
  • 315
  • 2
  • 6
  • 17
  • Are you trying to find a tool that can do this *automatically*, or are you trying to do this one specific case manually? – Billy ONeal Sep 09 '10 at 16:13
  • 2
    Are you trying to write the C equivalent or are you trying to access C++ functionality via a C interface. – Martin York Sep 09 '10 at 16:13
  • 3
    Do you just need it to compile with a C compiler or do you need maintainable code? If it's the former, get [Comeau C++](http://www.comeaucomputing.com). Last time I looked it was $50 and it outputs C, tailored to several C compilers, GCC among them. – sbi Sep 09 '10 at 16:21
  • I want to write the my seperate C equivalent code from C++ code. Is there any tool available or i have to write it seperatally. ? – Neel Patel Sep 09 '10 at 16:23
  • @Neel: As sbi asked, do you want to wind up with maintainable C code? In that case, tools aren't going to help you all that much. – David Thornley Sep 09 '10 at 16:31
  • 2
    As sbi just said Comeau C++ is a C++ to C translator (intended to allow platform independent C++ usage even whne the platform has no C++ compiler). – Clifford Sep 09 '10 at 16:33
  • 1
    A classic example of describing a step instead of the goal; http://www.catb.org/esr/faqs/smart-questions.html#goal. We will be better able to help if you show what you have and describe what you're trying to accomplish. – Dour High Arch Sep 09 '10 at 17:46

5 Answers5

13
  1. convert all classes to data structures (typedefs)
  2. replace constructors and destructors with functions that use malloc/calloc and free and return/take pointers to your typedef'd structures
  3. eliminate polymorphism; if this is not possible then implement a message-dispatching function for each typedef (a big switch statement) and represent messages as constant integers
  4. alter all functions to take a pointer to the appropriate typedef

Note that typedefs do not support subtyping, so all inheritance will have to be converted to composition.

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
  • 4
    About point 3, you can also build "manually" a vtable, initialized by the fake-constructors. – Matteo Italia Sep 09 '10 at 16:28
  • 1
    On point 2: Constructors/destructors have nothing to do with allocation/deallocation, but with initalization and finalization. That should probably be: 2.0) convert constructors and destructors into intialization and finalization functions 2.1) convert all new into malloc+call to inialization function, 2.2) convert all delete into call to finalization function + free, 2.3) ensure that finalization function is called before objects go out of scope for all classes that have non-trivial destructors – David Rodríguez - dribeas Sep 09 '10 at 16:56
  • Which actually is converting all the constructors to placement constructors. – Matteo Italia Sep 09 '10 at 17:11
  • 1
    @David the details are left as an exercise for the reader ;-) – Steven A. Lowe Sep 09 '10 at 20:44
5

There are a lot of considerations and things that need to change, but I think what you want to do is emulate object oriented code in C.

Basically for each of your classes you need to take out the functions and leave only the data members inside the class/struct (you will rename class to struct and remove public/private/protected specifiers).

Then you need to add a first parameter to each of those functions which is a pointer to the struct to operate on.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
3

Nearly all typical OO C++ code is just snazzy syntax around the old technique of creating libraries that created a "cookie" and required it to be passed into every call. So if none of the classes have virtual methods (no runtime dispatch) you should be able to do the following:

  • Think of all class names as "facility" or "library" names.
  • Put all data members of the class into a struct named classname (what the class used to be named)
  • Turn all the methods into functions named classname_methodname.
  • Add a pointer to the classname struct to the parameter list of all the functions (methods) for that class.
  • Turn the constructors into a functions named classname_construct(# for overloads perhaps) and the destructor into a function named classname_destruct.
  • Change all the method calls in other code from objectname.methodname (...) to classname_methodname (objecname, ...).
  • This is the tricky part: You will have to put in code to call the destructors manually, since those are automagically called in C++.

Taking the example given in the comments:

class QtCommandData { 
public: 
    QtCommandData(unsigned short net,         unsigned short command, 
                  unsigned long n_data_bytes, unsigned short flgs, 
                  unsigned char* data = NULL); 
    QtCommandData(); 
    ~QtCommandData(); 
public: 
    unsigned char* m_pData; 
    int m_Async; 
protected: 
    unsigned int m_nDataBytes; 
    unsigned int m_BytesAllocated; 
protected: 
    int Fill_Trailer(); 
};

...becomes (I'll abbreviate the "facility name" from QtCommandData to QtCD):

typedef struct {
    unsigned char* m_pData; 
    int m_Async; 
    unsigned int m_nDataBytes; 
    unsigned int m_BytesAllocated; 
} QtCD;

QtCD_Construct(QtCD * handle,
               unsigned short net,         unsigned short command, 
               unsigned long n_data_bytes, unsigned short flgs, 
               unsigned char* data); 
QtCD_Destruct(QtCD * handle);
QtCD_Fill_Trailer (QtCD * handle);

That's the general idea. Templates and dynamic dispatch and a few other things may throw you a monkeywrench or two, but I'm sure you are up to the challenge. :-)

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • i have following class can u pls tell me how to do that ? – Neel Patel Sep 09 '10 at 18:41
  • class QtCommandData { public: QtCommandData(unsigned short net,unsigned short command, unsigned long n_data_bytes, unsigned short flgs, unsigned char* data = NULL); QtCommandData(); ~QtCommandData(); public: unsigned char* m_pData; int m_Async; protected: unsigned int m_nDataBytes; unsigned int m_BytesAllocated; protected: int Fill_Trailer(); }; – Neel Patel Sep 09 '10 at 18:54
2

For functions you also need to prevent the C++ compiler from name mangling if you want to link a C++ library to a C program, so you add blocks in your headers like

#ifdef __cplusplus
extern "C" {
#endif

 /* Some functions here */

#ifdef __cplusplus
}
#endif
arsenm
  • 2,903
  • 1
  • 23
  • 23
0

C does not directly support the OOP paradigm. Translating an OO design or code to C is a fairly simple mechanical process but not one that could not reasonably be fully covered here perhaps. There is at least one thread on the subject already: Can you write object-oriented code in C? with links to resources.

If your code uses the more complex C++ features such as generic programming, polymorphism and multiple inheritance, your work may be more difficult than it is worth. However C and C++ can interoperate fairly easy. Why would you not just provide a C wrapper around your C classes?

Community
  • 1
  • 1
Clifford
  • 88,407
  • 13
  • 85
  • 165