views:

287

answers:

9

This questions concerns mostly Unix/Linux style C++ development. I see that many C++ libraries store their header files in a "include" folder and source files in an "src" folder. For the sake of conformance I adopted this in my own code. But it is not clear to me whether this should be done for application code as well. I've seen a few cases where a flat directory structure is used for that. What would be the recommended approach?

+1  A: 

There is no clear advantage to either in my view. I finally decided to keep program and header files together because my editor (Visual SlickEdit) happens to provide additional referential features when they are not separated.

Amardeep
Funny how one's tools can influence many aspects of a project. I agree that organizing the code in a way that is compatible with the IDE is usually a good idea.
StackedCrooked
+3  A: 

I don't do this; there seems little advantage in it. Since headers tend to have a different extension from source files, you can have your editor show them separately if you really feel the need -- Visual Studio does this by default, but I disable it since I prefer seeing them together

Michael Mrozek
+1  A: 

I almost always create include and src folders to split up my source code. I think it makes the folder less cluttered and files are easier to find in my IDE. But I think this is just a matter of taste.

Either method is valid. It depends on the coding style you want to follow how you do this.

Kungi
My motivation is similar in that I dislike the clutter.
StackedCrooked
+3  A: 

A lot depends on the size of project involved. Up to a few dozen files or so, keeping them in one directory tends to be more convenient. For a bigger application that includes hundreds or thousands of files, you start to look for ways to separate them (though in the projects I've worked on, it was done more on functional lines than src/include). In between those, it's probably open to question.

Jerry Coffin
Yes, it's when the number of files is getting big that I start to want extra organization. It's funny because it probably doesn't really have much benefit. Maybe it only satisfies some sort categorizing obsession :)
StackedCrooked
+6  A: 

I also separate them, but not strictly on the extension, but on the access of the file.

Suppose you have a module that manages customer information and uses 2 classes to do this: Customer, CustomerValidityChecker. Also suppose that other parts in your application only need to know about the Customer class, and that the CustomerValidityChecker is only used by the Customer class to perform some checking. Based on these assumptions I store the files like this:

Public folder (or include folder):

  • customer.h

Private folder (or source folder):

  • customer.cpp
  • customervaliditychecker.h
  • customervaliditychecker.cpp

That way, it becomes immediately clear for callers of your module which parts are accessible (public) and which parts aren't.

Patrick
I've used this style at one of my previous jobs as well. I found the public/private separation a bit burdensome, but it definitely makes sense if you want to separate your API in a public part for your customers and a private part for internal use.
StackedCrooked
+1, I use the very same separation at work. When you interact daily with at least a dozen components, it comes handy that they don't clog the API files with those you don't care about.
Matthieu M.
+1  A: 

It makes sense to separate them for shared libraries because they may be distributed in a compiled form without the source. I've seen projects that separate out "public" headers (headers that may be accessed from code outside your project or library) while leaving "private" headers and source files in the same directory. I think it's good to use a consistent approach whether you're writing shared library or application level code because you never know when you may want to turn something that you've written at the application level into a lower level library that is shared by multiple projects.

bshields
+1  A: 

I place include (header) and source files in the same directory (folder). I create different folders for different themes. I get frustrated when trying to find header files (while debugging and also for researching). In some shops, there are only two folders: source and includes. These directories tend to grow exponentially. Reusing code becomes a nightmare at best.

IMHO, I believe organizing by theme is better. Each theme folder should build into at least one library. Different projects can easily include the themes by searching or including the folders. The projects only need to include the libraries. Smart build engines can list the theme folders as dependencies. This speeds up the build process.

The theme organization also adds a bit of safety to the project. Accidental damage to files (such as removing the wrong ones or replacing with different versions) is reduced since files are located in different directories. Deletion of files in the "Person" folder will not affect files in the "Shape" folder.

This is just my opinion, Your Mileage May Vary.

Thomas Matthews
This is similar to the codebase at my current work. The main application consists of many utilities which each have their own svn repository. The main repository collects all of it through svn externals.
StackedCrooked
+3  A: 

We have a build system that auto-generates our makefiles. One thing it does is recursively descend any subdirectories and build them as libraries, linking them together with the main directory's objects to make the application. (In practice, these "subdirectories" are usually symbolic links.) Libraries are static unless the directory name ends in ".so". One thing that's nice about this is that a full build of our system, which has many executables, doesn't have to repeatedly compile the common libraries.

However, as a result of this, there's no separation of headers and sources. And it has never been a problem. Honestly, I think it's better this way because headers and source files have commonality of location, and you can grab a directory and know you got everything you need to use it. It also works great with Subversion's "externals" feature, and similar features in other VCSs.

One last place where an include/src separation fails is if you use any code generators, such as flex, bison, or gengetopts. Figuring out where these tools should put their outputs so they get built is tricky if you've spread things out.

Mike DeSimone
Honestly I like to have the header and source files together as well. Also in one Visual Studio filter, so that header and source files are together.
StackedCrooked
A: 

We have a build system which use this rule. This build system is sconspiracy a set of scripts to configure SCons and dedicated to the C++ world. You can see an example which use this tools : fw4spl

Johan Moreau