Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » Convert c to cpp (sample code conversion from c to cpp ? )
Convert c to cpp [message #1770824] Thu, 17 August 2017 08:22 Go to next message
Eclipse UserFriend
I have a sample code build as C application.
Is there an easy way to convert to cpp or should I start with base cpp (main) and just copy the sample files to it?

I am not that comfortable changing make if that is the answer, but willing to try .
Re: Convert c to cpp [message #1770828 is a reply to message #1770824] Thu, 17 August 2017 08:43 Go to previous messageGo to next message
Eclipse UserFriend
its easiest to just create a new project.
Re: Convert c to cpp [message #1770832 is a reply to message #1770828] Thu, 17 August 2017 09:42 Go to previous messageGo to next message
Eclipse UserFriend
OK, I managed to copy all the files into clean cpp project Run into a little snag.
I can compile / build RgbRequred,c but I cannot link it.
Any suggestion where how to fix it ?

08:29:05 **** Build of configuration Debug for project ESA_SCT_CPP_0 ****
make all
Building file: ../src/RgbRequired.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/RgbRequired.d" -MT"src/RgbRequired.o" -o "src/RgbRequired.o" "../src/RgbRequired.c"
Finished building: ../src/RgbRequired.c

Building file: ../src/gpio_util.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/gpio_util.d" -MT"src/gpio_util.o" -o "src/gpio_util.o" "../src/gpio_util.c"
Finished building: ../src/gpio_util.c

Building file: ../src/main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp

Building target: ESA_SCT_CPP_0
Invoking: GCC C++ Linker
g++ -o "ESA_SCT_CPP_0" ./src/RgbRequired.o ./src/gpio_util.o ./src/main.o
./src/main.o: In function `runCycle':
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:26: undefined reference to `rgb_runCycle'
./src/main.o: In function `main':
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:40: undefined reference to `rgb_init'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:41: undefined reference to `rgb_enter'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:43: undefined reference to `sc_cycle_runner_start'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:52: undefined reference to `rgbIface_raise_red'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:55: undefined reference to `rgbIface_raise_green'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:58: undefined reference to `rgbIface_raise_blue'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:61: undefined reference to `rgbIface_raise_end'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:62: undefined reference to `rgb_runCycle'
/home/os64/Desktop/ESA_SCT_CPP_0/Debug/../src/main.cpp:67: undefined reference to `rgb_isFinal'
collect2: error: ld returned 1 exit status
make: *** [ESA_SCT_CPP_0] Error 1

08:29:05 Build Finished (took 506ms)

Re: Convert c to cpp [message #1770855 is a reply to message #1770832] Thu, 17 August 2017 20:41 Go to previous messageGo to next message
Eclipse UserFriend
You need to declare C functions as extern "C" in a C++ module.
I suspect you didn't.

https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c/1041880#1041880

In the header file you could use #ifdef __cplusplus
https://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work

[Updated on: Thu, 17 August 2017 23:04] by Moderator

Re: Convert c to cpp [message #1771338 is a reply to message #1770855] Thu, 24 August 2017 04:14 Go to previous message
Eclipse UserFriend
The steps are:

1. Create a new directory and do a CVS checkout for your package.
2. Work in stardev so that when it comes to compile and test your code the most up to date libraries are used, minimizing the chance of conflicts.
3. Build your code in the usual way e.g. cons +pams/global. This is optional but by doing this you check that it does compile to start with which can save you work later on.
4. Change the name of your file with C code in e.g. track_propagator.c becomes track_propagator.cc, following the naming conventions in our coding standards document
5. Try to rebuild the code using cons -noR +pams/global. The option -noR instructs
cons not to use the repository. If you omit this instead of compiling your new C++
file it will use the original C file from the repository.
Now fix any errors that are reported.
Generally you will find errors connected with the prototypes for any external
functions or subroutines. They may be missing or incomplete since C allows things
like extern void prop_circle_param_(); which gives and error at the line where you
call it such as "Error: Too many arguments in call to prop_circle_param_()".
The correct declaration for an externally defined function has this form:
extern "C" void prop_circle_param_(float *trk , float *xc, float *r);
If there are a number of these that will be used in more than one file they can be
collected in a header file. See for example pams/global/inc/global_prototypes.h.
If the external functions are defined in C then you can produce a header file using a
utility provided by Victor. Details are in his email.
7. Once you have a version which compiles test your changes by running bfc. There are instructions on how to do this.
8. Perform a CVS add of the new files and a CVS remove of the old .c files. CAUTION Once you do this you cannot re-introduce your original .c file to cvs in the normal way.
9. Check-in your package as you would usually.

[Updated on: Thu, 24 August 2017 04:16] by Moderator

Previous Topic:Project->properties settings
Next Topic:Using Multiple Templates but getting only one Template to work
Goto Forum:
  


Current Time: Mon Mar 17 21:45:59 EDT 2025

Powered by FUDForum. Page generated in 0.03289 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top