Take the 2-minute tour ×

I am writing an app that is one part PHP and one part C. The two pieces will talk via sockets. As part of the protocol for the IPC, the app will rely on a "message type" byte. For example, if the first byte of the data block is 1, then the rest means something like Show System Info, etc.

In the C header file, I could define these message types like...

#define GET_SYSTEM_INFO 0x1  
#define SET_SECURITY_LEVEL 0x2  

I would really like it if I could define these values in one file that is shared between the php portion and the c app. That way, they are working off a common set of config. It doesn't have to be a header file, but i was wondering if there was some easy mechanism for both technologies to parse a common file.

Thanks for any suggestions.

share|improve this question
Config files generally mean that the values can be changed. If you include these in a C header file, the values are compiled into the program. Do you want these values to be dynamically configured? Or are they just magic numbers and you want an easy place to define them both? –  Falmarri Nov 22 '10 at 19:07
The latter. Magic numbers and an easy place to define them for sharing. –  DoranKatt Nov 24 '10 at 20:00
add comment

3 Answers

up vote 2 down vote accepted

What I would suggest, since you really want to do it for development and the end user will not need to access the internals of your protocol, is to parse a php file for example and create an include for C, or the opposite through your build script.

share|improve this answer
add comment

I've never worked with C before, but I'll assume it can easily parse an INI file like PHP can.

See PHP's parse_ini_file().

share|improve this answer
add comment

This is the kind of problem that projects like Google Protocol Buffers and Apache Thrift try to solve.

If you want to do this yourself, however, there's two ways to go about it. One is to generate code (the #defines in your example) from some description. The other is to determine the message types at runtime (which I'm guessing you don't want).

Either way, you'll need to settle on some description format. You could for example write a small PHP script that parses an INI file, JSON file, YAML file, etc., takes a template containing your C program, and fills in blanks to output the final C source file. It depends a bit on your build environment, but it's usually a small step to integrate such a command line tool into your build process.

share|improve this answer
add comment

Your Answer


