0

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?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • I'm doing my work in gedit with g++ in ubuntu, but I should think this would be a universal sort of question. – temporary_user_name Apr 10 '12 at 02:05
  • 2
    See http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples – Charles Salvia Apr 10 '12 at 02:07
  • C is *almost* a proper subset of C++, but there are some nuances. As far as I remember, some operator precedences / associativities have changed. But now I'm really just guessing. – Imp Apr 10 '12 at 02:09
  • C is mostly a subset of C++, meaning that a large percentage of C code will compile with a C++ compiler. However, there are some minor (but important) differences. These differences have been enumerated and elaborated elsewhere on stackoverflow – Charles Salvia Apr 10 '12 at 02:09
  • C has network protocols? When was that added to C's standard library? Or are you talking about a C networking library? – Nicol Bolas Apr 10 '12 at 02:51
  • Networking library. Forgive me. Arpa/inet, sys/socket, netinet/in, etc. – temporary_user_name Apr 10 '12 at 03:15

5 Answers5

3

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.
Dave
  • 10,964
  • 3
  • 32
  • 54
1

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.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

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++.

Jay
  • 9,314
  • 7
  • 33
  • 40
0

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.

0

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...

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39