Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What issues can I expect compiling C code with a C++ compiler?

If you take an existing C code base and compile it with a C++ compiler, what sort of issues can you expect to crop up? For example, I think that assigning an integer to an value with an enumerated type will fail in C++, whereas it's legal (if a bit nasty) in C.

If I don't wrap all my C files in extern C { ... }, am I going to get name-mangling where I least expect it? Is there some reason why I really shouldn't do this?

For background, we have a very large code-base written in C. For a few years we've been jumping through hoops to do things that would come naturally via C++ ( homebrewe inheritance, for example). We'd like to start moving towards C++, but in a gradual fashion; getting our CORBA-like framework to support it, and refactoring modules as we go along to take advantage of the more natural approach C++ would provide.

like image 937
Chris Arguin Avatar asked May 14 '09 04:05

Chris Arguin


People also ask

What happens when you compile C code?

COMPILERS, ASSEMBLERS and LINKERS Preprocessing is the first pass of any C compilation. It processes include-files, conditional compilation instructions and macros. Compilation is the second pass. It takes the output of the preprocessor, and the source code, and generates assembler source code.

Can C code can be complied in CPP compiler?

Any C compiler that is compatible with the Oracle Developer Studio C compiler is also compatible with the Oracle Developer Studio C++ compiler. The C runtime library used by your C compiler must also be compatible with the C++ compiler.

What is the purpose of the C compiler?

Compilers analyze and convert source code written in languages such as Java, C++, C# or Swift. They're commonly used to generate machine code or bytecode that can be executed by the target host system.


2 Answers

I've done something like this once. The main source of problems was that C++ is more strict about types, as you suspected. You'll have to add casts where void* are mixed with pointers of other types. Like allocating memory:

Foo *foo; foo = malloc(sizeof(*foo)); 

The above is typical C code, but it'll need a cast in C++:

Foo *foo; foo = (Foo*)malloc(sizeof(*foo)); 

There are new reserved words in C++, such as "class", "and", "bool", "catch", "delete", "explicit", "mutable", "namespace", "new", "operator", "or", "private", "protected", "friend", etc. These cannot be used as variable names, for example.

The above are probably the most common problems when you compile old C code with a C++ compiler. For a complete list of incompatibilities, see Incompatibilities Between ISO C and ISO C++.

You also ask about name mangling. In absence of extern "C" wrappers, the C++ compiler will mangle the symbols. It's not a problem as long as you use only a C++ compiler, and don't rely on dlsym() or something like that to pull symbols from libraries.

like image 58
Ville Laurikari Avatar answered Oct 06 '22 01:10

Ville Laurikari


See Incompatibilities between ISO C and ISO C++ for a very detailed list of all of the incompatibilities. There are a lot of subtle issues, including some which don't immediately manifest in a compiler error. For example, one issue that can be a problem is the size of character constants:

// In C, prints 4.  In C++, prints 1 printf("%d\n", sizeof('A')); 
like image 42
Adam Rosenfield Avatar answered Oct 06 '22 02:10

Adam Rosenfield