Solved

Conver C to C++

Posted on 2004-10-07
7
458 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now