VC2010 C++ - organizing source files

后端 未结 5 1236
予麋鹿
予麋鹿 2020-12-12 10:35

I had some questions about how to organize source files in a VC 2010 C++ project. The default filters that are included won\'t be sufficient as ultimately, I\'m going to hav

5条回答
  •  天涯浪人
    2020-12-12 11:14

    I m late, but I advise against the accepted answer. The main reason if for code portability. Instead I recommend :

    1. To create the layout of folders and subfolders outside of visual studio (same as the accepted answer but the next points are different). Inside each subfolder create a include and src folder
    2. Then in Configuration Properties > C/C++ > General > "Additional Include Directories" (for All configuration and All Plateforms) add a single folder which is the base of all your subfolder.
    3. To add you src files and includes files in these subfolders.

    4. Finally include each header files using relatives paths to this base folder.

    To be clear : if the layout of your project is as follow :

    MyProjet
       Math
         include
           random.h
           functions.h
         src
           random.cpp
           functions.cpp
       Probability
          include
            normal.h
          src
            normal.cpp
    

    you should have in functions.cpp the following include statement :

    #include"Math/include/functions.h"
    

    if you also need to use the normal code in functions.cpp, then the top of functions.cpp should looks like this:

    #include"Math/include/functions.h"
    #include"Probability/include/normal.h"
    

    In doing so, you'll be able to re-use your Math subfolder in another project (B) without pain: just by adding the MyProject base folder into the "Additional Include Directories" of project B.

    The key point is to have only one base folder into the "Additional Include Directories" property.

    ps: the intellisense feature of VS 2015 helps a lot to write the #include...

提交回复
热议问题