Solved

Conver C to C++

Posted on 2004-10-07
7
462 Views
Last Modified: 2010-05-19
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
Comment
Question by:bezier98
7 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12256042
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
 

Author Comment

by:bezier98
ID: 12256263
Unfortunately no, we tried this before, and tones of error were generated.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12256808
Leave the 'C' stuff in '.C' files but when you include their headers use:

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

Paul
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 12

Expert Comment

by:stefan73
ID: 12257332
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
 

Expert Comment

by:x25
ID: 12265592
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
 
LVL 2

Accepted Solution

by:
Peter-Darton earned 500 total points
ID: 12504088
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
 
LVL 2

Expert Comment

by:Peter-Darton
ID: 12504115
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stack 22 172
SQL handling single and double quotes 3 97
Problem to show menu 5 93
Using popen() and gunzip() to open file in HTTPServer 6 67
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question