[C++] Project Structure

binh12A3
2 min readNov 21, 2021

😎😎 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)
HOME/main.cpp

1. Client’s configurations

HOME/client/vietcombank/include/client.h
  • 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

HOME/include/bank_domestic.h

3. Common sources

//sources
HOME/src/banking_funcs.cpp
HOME/src/banking_funcs.h
HOME/src/insurance_funcs.cpp
HOME/src/insurance_funcs.h
//services
HOME/src/service/service_banking.cpp
HOME/src/service/service_banking.h
HOME/src/service/service_insurance.cpp
HOME/src/service/service_insurance.h

3.1 Banking Functionalities

3.2 Insurance Functionalities

4. main.cpp

HOME/main.cpp

Additional Notes

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 😋

--

--