[C++] Project Structure
😎😎 Let’s have a quick look of using macros #ifdef, namespace,… in a project
In this article, I’ll show you a simple way to manage Project Structure in case we have a big project, which supports for multiple clients (i.e : vietcombank, citibank).
HOME/client/vietcombank/include : contains a specific client.h
HOME/client/citibank/include : contains a specific client.h
HOME/include : contains common header files (*.h)
HOME/src : contains common sources (*.cpp)
1. Client’s configurations
- Configurations for each clients should be put in a particular “client.h” and under a particular folder. Let’s assump, we have 2 big clients : google and facebook.
- In this header file, we’ll define macros
2. Common header files
3. Common sources
3.1 Banking Functionalities
3.2 Insurance Functionalities
Advantage of using “#ifndef, #define, #endif” in header file
- This is called “Fileguards” as a protection to avoid cross duplidated when many sources include this .h file.
For more info about Fileguards, you can find (9. Fileguards in C or C++ Header Files)
Advantage of using namespace
For more info about Fileguards, you can find here.
- If we deploy this software to a client who is both BANKING and INSURANCE, then we’ll face a compile error “void __cdecl initialize(void)” (?initialize@@YAXXZ) already defined in banking_funcs.obj” due to name collision of func initialize().
- We could resolve it by renaming the func, or you could use namspace concept.
- In insurance_funcs.cpp(.h), we’ll wrap functions with namespace insurance.
- In the cpp file, service_insurance.cpp, we’ll add “using namespace insurance;” after the include of #include “insurance_funcs.h”. In this case, after “#include “service_insurance.h””
- Or we could preface the namespace before the API call
- Now, let’s see the result 😋