I am currently having somewhat of a brain stump here. I am programming in a larger project and well, it's on the move to c++. the source code is all c we compile by using a makefile.

There is a file with several functions we are taking one removing everything in that function except the function name(since it's being used already) and calling that function from the cpp folder/file? I am not very experienced in this conversion type. In the cpp folder there would be another file with that function in c++ using some c functions in it and would compile with one makefile.

How would I go about doing this? by creating a header file for each file .cpp file? I am discovering that there has to be a way to incorporate or do it this way.
some instruction in the right direction would be nice. If you still do not understand it. Let me explain another way? Lets say you have a function you want to convert it to c++, but cannot due to it being in hundreds of other places, so you have to do it bit by bit. how do you leave that function void fun in the c file and redirect it to another c++ file with a redesigned updated version of the same function??

Recommended Answers

All 4 Replies

I have not directly had to change over from c to c++ for a large number of files but there are couple of things to perhaps clarify in your questions.

I think that what you have is effectively a file called "functions.h"
In a functions.c file you have defined several functions:
fun1()
fun2()
...

and what you are trying to do is change the definitions and inner workings of the function but not what the function is doing.

IF you are using a function in lots of places it is safe to assume that the current version of the function performs the task as you want it to. Therefore, it can be used to test that your new c++ version is still working properly. Most C code should run within c++so the old c code can be used as a starting point.

You can either have a version of each include file in a c++ folder and you can keep the name of the file but you temporarily
#include "c++/function.h"
that can be changed to function when all of the files have been converted and then change the folder properties. This works if you can change all of the class in one go.

method 2
If you place all of c++ code into a namespace then you can have
functions with the same name and when you call the c++ version
it can be scoped name::fun1();
Once a file uses all updated files the names spzce can be taken out with a temporary include file
that would just have
using namespce
and again once everything is done you can change the code out.

in short you want to gradually update one file at a time and try to avoid changing old code until you update th efile so no apis.

I hope this helps you think about the problem.

Why do you have to convert those C functions at all? Just call them norally in any c++ code you want to add to the project and leave the C code in tact.

In the c++ files you just have to add this: probably add it in the header file that contains the function prototypes of all those C functions. This is the same method that compiler writers use in standard header files so that the same header files can be used in both C and C++ programs.

#ifdef _cplusplus
extern "C"
{
#endif
    // now prototype all those C functions
#ifdef _cplusplus
}
#endif

Well, it's called an upgrade, converting a source code to c++, will create more possibilities. Let me explain it in more detail. We have several files roughly 40 maybe more. All inter twining to create our program. We are modifying functions, so the idea is to make a cpp folder with with the files and have the new modifying functions be in the c++ folder, this would mainly be rewritten functions. We want to be able to leave the function in .c file, but have it pull the data from the cpp file? I have an idea whats needed to be doing, but I'm having trouble attempting it to pull the information from the C++ file. I was thinking more a long the lines of const string and arguments to make it work...Could be wrong

C programs can not use any of the c++ stuff, so if you are expecting a c function to call a c++ function which returns std::string, then forget it. The best you can hope for is that the c++ program returns const char*. Then you will have to declare that c++ function as extern "C" , which essentelly tells the c++ compiler to treat the function as a C function calling conventions, and not mangle the function name. That means the c++ function can not be a method of a class or an overloaded function.

Since you are doing that much recoding, it will probably be easier to just convert everything to c++ and not use any of the *.c files at all. If there are C functions that can be used without change then just copy/paste them into a *.cpp file.

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.