Question

I'm writing a chat server for class. I'd like to use C's network protocols, but I'm more comfortable programming in C++, particularly in terms of string manipulation.

As I understand it, you can combine the two in a single file and compile for C++ and it'll still accept the C code so long as the proper #include's are there.

What are the limitations on this? What should I look out for? Is there anything in particular from C that will not work in a .cpp file?

Was it helpful?

Solution

You should be able to #include and use standard headers including the networking headers in C++ without doing anything special.

There are some differences between C and C++, but its unlikely you'll run into any problems because of it.

One difference is enums. In C, an enum is just an int. In C++, an enum is an actual type. This code is valid C, but invalid C++.

enum sport {
    hockey,
    baseball,
    soccer,
    vollyball
};

enum sport s = 5;

Compiling this with g++ gives

test.c:11: error: invalid conversion from ‘int’ to ‘sport’

Here is more information on mixing C and C++.

OTHER TIPS

Don't combine the two in the same file. Writing C that compiles as C++ leads C people to yell at you, and vice versa. Instead, make a little C library, and have your C++ link against it. The only thing you need to do then is to add

#ifdef __cplusplus
extern "C" {
#endif

At the beginning of the header file for the C lib, and

#ifdef __cplusplus
}
#endif

At the end.

It's easy, and pretty, too: Create a Makefile, since you're using gnu make, this is really easy:

program: cstuff.o program.o

With that, running make will issue the following commands:

cc -c cstuff.c 
g++ -c program.cpp
cc cstuff.o program.o -o program

So a directory listing will have 4 files: program.cpp cstuff.c cstuff.h and Makefile.

  • cstuff.h contains all your structure definitions, and function prototypes, along with that extern "C" stuff,
  • cstuff.c is self-evident, and
  • program.cpp begins with #include "cstuff.h" can call the functions listed in the header file, and has all the C++-ey goodness you love.

Usually everything goes quite fine when mixing C and C++, after all the latter inherited from the first much of the syntax.

Probably the most notable difference is that you will have to explicitly cast dynamically allocated memory:

int *array = (int*) malloc(...)

This because in C void* can be assigned to any type of pointer, while this is not true in C++, while in C you wouldn't need to do it. But in C++ you could simply avoid malloc and use directly new and delete.

Take a look here in any case, many differences are summarized, but they mostly are just slight ones.

C++ is basically just an updated version of C with OOP methodology added. So combining them isn't really a problem. Most compilers support both anyway so you can combine them on the fly.

All libraries with "C interface" are usable in C++ code. In the worst case, you have to add extern "C" { ... } to specify that the "extern linkage" for the enclosed symbols is a la C and not a la C++. In the best case, the header to use that library already takes into account the possibility to be used inside C++ code, so all you have to do is to include it, and then use the functions of that library as you would do in C.

I am rather sure that in your case ("net"/sockets functions) it is enough to include the headers and add the libraries between those to be linked. (ADD) Of course, where the prototype wants char * you can't pass string, you have to use c_str() if you keep your string as C++ string object...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top