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:
-
Using
#define IS_CLIENTin Client.h (which includes the main function) and#define IS_SERVERin Server.h. This failed because the other headers then had to#includethem (so I ended up having to modify all the headers when I wanted to compile something) -
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.
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
#ifor#ifdefin source files should not be done, unless the effort to solve that is really, really huge.