DXsmiley
DXsmiley

Reputation: 539

Defining different functions depending on project

I currently have two c++ projects, which share many of the same source and header files. One of them is a game server, the other is the client. Some of my classes have functions which are unique to the client or server, and thus should only be compiled for one of them. I am unsure of how to go about this however. My attempts so far have been:

  1. Using #define IS_CLIENT in Client.h (which includes the main function) and #define IS_SERVER in Server.h. This failed because the other headers then had to #include them (so I ended up having to modify all the headers when I wanted to compile something)

  2. Using #include "CompileType.h" where CompileType.h defines either IS_CLIENT or IS_SERVER. This is a major improvement other the previous method, but still leaves me changing the file whenever I want to compile.

Both attempts used #ifdef and #endif around select function definitions in the .cpp files.

A possible solution would to be to #define something on a global scale (so It affects all source and header files) if possible. There may also something in my IDE's (Code::Blocks) settings that I have not seen yet.

Any insight into this problem would be appreciated.

Upvotes: 0

Views: 59

Answers (2)

Étienne
Étienne

Reputation: 4994

For one project I worked on we had a program which had a core functionality, and several optional modules. Then every files belonging to a module were writen in some #ifdef MODULENAME / #endif blocks, and it was then possible to decide for which module to compile by defining MODULENAME at the project scale (in Visual Studio with property sheets, but there are some equivalents for Code Blocks). And on the disk every module files were in different folders so that it doesn't get too messy, this solution could also work for you.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129374

The best way is to completely separate your functionality, such that all the "server" functions are in one or more file(s) that is only used for the server, and all your "client" are in it's own file. So, basically, you have three directories, one with "server" files, one with "client" files, and one with "common" files.

Using #if or #ifdef in source files should not be done, unless the effort to solve that is really, really huge.

Upvotes: 1

Related Questions