Hi Everyone. I'm pretty new to C++ and C, but I have experience with other languages (not OOP). I'm working on a project that requires me to take a working source code in C++ and convert it run in C. Luckily the inheritance diagram isn't too bad, so theoretically, I should be able to trace the dependencies through the objects and class structures to make it do what I want. Originally, it was much more sophisticated than what I need, so I only need a few parts of it. Is there an easy/fast way to convert C++ with class structure, etc to C?

Another twist to this problem is that the working C++ code uses functions that have only prototypes and are called in main(), but do have definitions. The definitions are proprietary to another company, so they just attached a library file. As I understand it, library files contain functions and other often called routines that have already been compiled to machine code. However, when you define a member function in a class, you use the :: operator to let the compiler know which class a function belongs to. If the functions have already been compiled, can I still call those functions from my C code without the classes that they were once a member of? Sorry for the wordy question. I’d appreciate any input someone could give me. Thanks.

>>Is there an easy/fast way to convert C++ with class structure, etc to C?

No. First convert the class to a structure and remove all its methods as well as access rights (public, protected, private). If it has base class(s) you will have to remove them too.
Next, convert class methods into normal functions and add a structure pointer as one of the function parameters (most likely make it the first argument).

>>The definitions are proprietary to another company
If the library has C style linkage then you probably won't have to do much other than use the library(s) as-is.

If the library is c++ then you may have big problems because C can not normally call C++ functions. You will have to write c++ wrappers for those library functions that can be called by C, which means you will not be able to completely eliminate c++ from, your program.

Can you not write c wrappers on top of c++ interfaces and convert the whole thing as a library and use it ?

Thank you so much. I'm following the steps outlined above. I'm working on linking a DLL to see if that will work, but I'm having trouble with the loadlibrary function.

why use LoadLibrary()? If you wrote the DLL then you should have *.lib which can be linked just like any other library and its functions called just like any other function.

The end result is be able to call this C code from another program called MatrixX that is similar to Simulink in Matlab. Basically, you draw block diagrams of what you want it to do. There is a user code block (UCB) that allows you to enter C code to define your own functions. When you first build your MatrixX project, it uses Visual in the background to compile the code in its own particular way. Pre compiled C codes won't work because they have to be written in a very unique format in order to pass inputs, output, and parameters between the C code and MatrixX. In other words, if you take a code that works in a UCB and try to compile it, you will get errors.

I have been experimenting with what the UCB will and will not accept. That's why I need to convert the application to C--the UCB will not accept C++. It will accept structures, but not classes. I can include header files with function definitions, and call those functions from the UCB code. If I can explicitly link a library to it and call a function from there, the company that supplies the drivers will give me a DLL with the deffinitions.

Before I try it in the UCB code, I want to get a simple program to call a function from a DLL. I just wrote, compiled and built a very simple DLL that successfully outputs a function. I even put in a .def file to undecorate the function name. I say successfully because I checked it with dumpbin.exe. All I want my simple program to be able to do is use that function from the dll for now. I found some examples online of using loadlibrary, but I haven't gotten it to work yet. I think I need to include more header files, but I'm not sure which ones (maybe WINBASE.H?). Thanks for all the advice.
Glenn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.