2

Recently I was assigned a task to provide a C implementation of my C++ code. Since the original code is large, and is largely relying on structures not present in C, I wonder what would be the easiest way for the transfer.

I read that there are wrappers that allow one to use C++ in C? Will these allow me to simply copy/paste the code in C?

Also, I explored the following http://attractivechaos.wordpress.com/2008/09/19/c-array-vs-c-vector/

Do you have a suggestion on how I might do the task without much work (clearly, because the code is already written, needs only be "transferred")?

Mat
  • 202,337
  • 40
  • 393
  • 406
user506901
  • 699
  • 4
  • 9
  • 18
  • 1
    Are you still allowed to compile parts of the program in C++, or must you absolutely have only pure C code? – Kerrek SB Oct 31 '11 at 13:02

3 Answers3

1

You can wrap C++ code in a C API.

However, it may still depend on the C++ library. If your requirements prevent use of the C++ library, then wrapping the existing code in a C API is not going to help you.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • To try the wrapping variant I used the example from [link](http://www.g-truc.net/post-0235.html) After executing gcc -o source source.c, I get the following error: header.h:2: error: expected identifier or ‘(’ before string constant. -- What might be the problem here? – user506901 Oct 31 '11 at 14:23
  • Are your files *identical* to those in the example? If not, how do they differ? – Andy Thomas Oct 31 '11 at 14:50
  • I copied once more, and executed: gcc -o source source.c I obtaned: In file included from source.c:1: header.h:2: error: expected identifier or ‘(’ before string constant – user506901 Oct 31 '11 at 14:55
  • There's an error in the example. The extern "C" is for the C++ compilation. Change the #ifndef to a #ifdef. – Andy Thomas Oct 31 '11 at 15:05
  • The header.h content: #ifdef __cplusplus extern "C" #endif //__cplusplus void func(); ..... the error I get: /tmp/ccx8xKoj.o: In function `main': source.c:(.text+0xa): undefined reference to `func' collect2: ld returned 1 exit status – user506901 Oct 31 '11 at 15:13
  • Add a -c option to your gcc command to tell it to compile, but not link. Compile source.cpp similarly. Then link the two of them together. – Andy Thomas Oct 31 '11 at 15:20
  • With sequence g++ -c source.cpp gcc source.c source.o -o myprog I get the following error – user506901 Oct 31 '11 at 15:57
  • The error doesn't show in your comment. That said, make sure your two files with the same base are not being compiled to the same *.o file, and link in the appropriate libraries with -l. – Andy Thomas Oct 31 '11 at 16:33
1

You can write a wrapper-library that uses internal handles to the objects and classes used, then. A function to call a method in an object might look like this:

extern "C" int classname_methodname(int handle, int param1);

The handle parameter can then be an index into a vector<classname> internal_classname.

The actual function, classname_methodname in my example above, just fetches the object from the vector and calls the method in the object with its paremeters.

To create an object instance, you can have a classname_create or similar named function, that creates a new instance and then returns the handle used for the other functions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

This task depends heavily on your boundary conditions. The other posters defined a good method to provide a C interface to a C++ module.

If you absolutely need to convert your code to pure C, but maintainability is not an issue, you might think about compiling your code and then using a C decompiler to generate gibberish that compiles as valid C code into a C module functionally equivalent to your C++ module.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • 1
    Or you can convert the C++ code into C: http://stackoverflow.com/questions/737257/code-convert-from-c-to-c/737272#737272 – rve Oct 31 '11 at 13:21