Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

Conver C to C++

After 2 questions (connect to C and use web services), actaully if we can easily convert C source code to C++, then everything solved.

Can anybody suggest are there any tools to convert C source to C++, or keep the C source, but calling functions from other C++ file which with something like web services implemented?

Regs,
Bezier
0
bezier98
Asked:
bezier98
1 Solution
 
ankuratvbCommented:
Hi,

Most C Sources will run fine on a c++ compiler,i.e. if you save them as a .cpp as well.
Rename the file as a .cpp and try running it.

Then,if it runs fine,you can add the web service code that you have(which is in C++)
0
 
bezier98Author Commented:
Unfortunately no, we tried this before, and tones of error were generated.
0
 
PaulCaswellCommented:
Leave the 'C' stuff in '.C' files but when you include their headers use:

extern "C"
{
#include "header.h"
}

Paul
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
stefan73Commented:
Hi bezier98,
Keep in mind that in C++ you can have function and method overloading. To reflect this, the C++ compiler uses a technique called "name mangling" - the arguments of a function or method are added to its name.
C uses plain symbol names. To make a C++ function callable by C, you can also use extern "C" { ... } (which just switches off name mangling, BTW):

extern "C" {
    int cpp_func_reachable_by_c(){
    }
}

Cheers!

Stefan
0
 
x25Commented:
bezier98,
 can you show example of code where the 'C' code portion of your project fails to merge with other code?
 
 you can try separating the two portions of the code and compile them each independently to create two .obj files, then link them together in one .exe file.
0
 
Peter-DartonCommented:
The basic rules are

A .c file will be compiled as C code.  All global functions & variables defined therein will have nice simple symbol names, e.g. "int fred(int argument) { ... }" will create a symbol called _fred
C code can't call C++ functions directly.

A .cc or .cpp file will be compiled as C++ code.  All global functions & variables defined therein will, by default, have nasty complicated symbol names - the names are "mangled" - a process that encodes the return type and arguments into the name, e.g. "int fred(int argument) { ... }" will create a symbol called _fred_R3intN1A3int (this is just an example, not an actual occurance).
C++ code will assume any functions it calls are C++ functions and expect mangled names unless told otherwise.
C++ code can both call and define C functions & variables if informed the function/variable uses the "C" naming convention using the 'extern "C" { ... }' syntax.


Basic recipe for success:

Divide your code into "pure C", "pure C++" and "C++ with an API for C code".
Pure C you put in .c files and .h files.
Pure C++ you put in .cc (or .cpp) and .h files.
The C++ with the C API you put in .cc (or .cpp) and .h files.

In the header files (the .h files), those files relating to C code should be of the following layout (replacing HEADER_FILE_NAME as appropriate for each header file)

#ifndef _HEADER_FILE_NAME_INCL_
#define _HEADER_FILE_NAME_INCL_
#ifdef __cplusplus
extern "C" {
#endif

/* Put your declarations here */

#ifdef __cplusplus
}
#endif
#endif


Files that contain both C++ and a C API are a bit more complicated:

#ifndef _HEADER_FILE_NAME_INCL_
#define _HEADER_FILE_NAME_INCL_
#ifdef __cplusplus
extern "C" {
#endif

/* Put the declarations of functions etc you need to be able to call from both C and C++ here */

#ifdef __cplusplus
}
#else

/* Put the declarations of methods etc you want to be accessable only from C++ here */

#endif
#endif

Header files for the C++ code should be as follows:

#ifndef _HEADER_FILE_NAME_INCL_
#define _HEADER_FILE_NAME_INCL_
#ifdef __cplusplus

/* Put your declarations here */

#endif
#endif



The net effect of this is that C++ methods are invisble to the C compiler at compile time (as well as link time), and if the C++ compiler starts looking for methods belonging to C modules, it finds them in an 'extern "C" { ... }' wrapper so that it knows they're C methods and will therefore have simple names.

Also note that few C compilers allow the use of a structure name to refer to the structure without the use of typedef, whereas this is automatic in C++.
e.g. C++ you can go
  struct fred { int a; int b; }
  void printToStdout( fred *ptr );
  void printToStdout2( struct fred *ptr );
and both those two declarations are effectively identical.
whereas in C you'd have to do
  struct fred { int a; int b; }
  void printToStdout2( struct fred *ptr );
OR
  typedef struct { int a; int b; } fred;
  void printToStdout( fred *ptr );
OR (more commonly done IME)
  struct fred_s { int a; int b; }
  typedef struct fred_s fred;
  void printToStdout( fred *ptr );
  void printToStdout2( struct fred_s *ptr );

i.e. if your C++ files define lots of structures, you might need to tinker with the syntax to make them acceptable to a C compiler.
Note: You'll need to hide any class definitions as C++ only, as well as any structure with public, private or protected keywords.  This might mean that you need to carefully consider the API between C and C++ and write a module specifically to provide that API.
0
 
Peter-DartonCommented:
Oh yes, and comments:  It's important, when you've got header files being included by both C and C++ compilers, to ensure that the comments in the C++ header files are "C style" and not "C++ style" - all compilers understand /* comments */ but few C compilers understand and accept // comments
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now