Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good directory structure for larger C++ projects using Makefile?

Tags:

c++

makefile

What's a good directory structure for larger C++ projects using Makefile ?

This is how my directory structure looks at the moment:

lib/ (class implementations *.cpp) include/ (class definitions *.h) tests/ (main.cpp for quick tests) 

Now, I'm not sure how my Makefile should look like... it doesn't seem to work when .cpp files and .h files aren't in the same directory. Could anyone point me to a common directory structure with an accompanying Makefile so that I don't reinvent the wheel ?

like image 464
Olivier Lalonde Avatar asked Mar 02 '10 03:03

Olivier Lalonde


People also ask

How Makefiles can be used to manage large C projects?

Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.

How do I organize a large C++ project?

In C++ you should avoid putting entire project in one class , irrespective of big or small. At the max you can try putting it in 1 or 2 namespace (which can be split across the files). The advantage of having multiple classes are, Better maintainability of your code.

What is Vpath in Makefile?

The value of the make variable VPATH specifies a list of directories that make should search. Most often, the directories are expected to contain prerequisite files that are not in the current directory; however, make uses VPATH as a search list for both prerequisites and targets of rules.


2 Answers

Separating the .cpp of the .h file is not always a good solution. Generally I separate both of them when it is used as a library (public header in include and private header with the source code).

If it is a library, this structure is ok.

lib/ (class implementations *.cpp .h) include/ (class definitions *.h) <- Only those to be installed in your system tests/ (main.cpp for quick tests) doc/ (doxygen or any kind of documentation) 

If it is a application

src/ (source for the application) lib/ (source for the application library *.cpp *.hpp) include/ (interface for the library *.h) tests/ (main.cpp for quick tests) <- use cppunit for this part doc/ (doxygen or any kind of documentation) 

Use the flag -I$(PROJECT_BASE)/include to specify the include path for the compilation

If it is a big project, it can be good to use tool like autoconf/automake or cmake to build everything. It will ease the development.

like image 154
Phong Avatar answered Oct 08 '22 18:10

Phong


For those who find this question after 2020, an alternative modern and reasoned vision of "Canonical Project Structure" for C++ has been presented by Boris Kolpackov: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1204r0.html

Briefly - no include/ and src/ split. All headers, sources, modules and unit tests go into one directory. Implementation details may be separated from public API by moving to <name>/<name>/details/ subdirectory.

<name>/ ├── <name>/ │   ├── headers... │   ├── sources... │   ├── modules... │   └── unit tests... └── tests/     ├── functional_test1/     ├── functional_test2/     ├── integration_test1/     ├── integration_test2/     └── ... 
like image 30
4LegsDrivenCat Avatar answered Oct 08 '22 18:10

4LegsDrivenCat