Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source file organisation

Tags:

c++

include

path

I am having a bit of trouble organising my source files.

I have my own small, but growing collection of code that I would like to use in various projects. The file and folder layout is something like this:

library\sub1\source.h

library\sub1\source.cpp

library\sub2\source.h

library\sub2\source.cpp

One of my problems is that I want to include this code, as needed, in my other projects. To date I have used absolute paths to point to the libary code, but there must be a better way.

Futhermore, I need to add every library file I use to a project's files Visual Studio in order for it to compile correctly.

So my question in short is how do I fix this? What is the proper/best way to handle the above situation.

like image 877
links77 Avatar asked Sep 05 '09 11:09

links77


People also ask

What is an example of a source file?

Source files are the files you, the designer, have used to create your designs. The more well-known files include Adobe's Photoshop, Illustrator and Indesign. These files should be provided to clients so they can print scalable versions of the design or edit them as they see fit.

What is source file format?

Source files contain the data that is being extracted from the source system before it's being transformed to the Common Data Format. Source files typically contain the data in its raw form. The data can be divided into any number and types of files, representing the way the data is stored in the source system.

What is called a source file?

(1) A file that contains program instructions. See source code. (2) A file that contains original or essential data that is the starting point for a system of publishing or other processing.


2 Answers

You shouldn't, in general, add source files from libraries directly to other projects. Compile them separatly as a library and use those.

For organising the library's directory structure itself, right now I settled on something like the following structure

  • library1/widget.h
  • library1/private/onlyinlib.h
  • library1/private/widget.cpp

(and if applicable)

  • library1/private/resources/widget.jpg
  • library1/private/project/widget.xcode

I put all headers directly in the library path, and have a subfolder private which will contain everything that's only used by the library, but should never be shared / exposed.

The greatest advantage is that every project I start only needs a include path pointing at the directory containing my libraries, then every (public) include is done like

#include "library1/widget.h"

private includes are simply

#include "onlyinlib.h"

This has a number of advantages:

  • If new libraries are introduced, there's no messing with project /compiler settings to get the headers 'visible'.
  • Moving to other compilers / platforms is also very little hassle.
  • The headers are automatically 'namespaced', i.e. by including part of the path too, it's next to impossible to get a nameclash with the includes
  • It's immediatly obvious where a header comes from, and if a header is part of the public interface or not
like image 126
Pieter Avatar answered Nov 07 '22 03:11

Pieter


I don't think that there's a proper way to do this - it's going to depend on exactly what you are trying to achieve.

Here's some things you might not be aware of:

  • You can use relative paths in your projects.

  • You can use environment variables in paths.

  • You can add directories to Visual Studio's search rules.

This gives you a little more control over where you put the include files and if you add your folders to Visual Studio's search rules you don't have to include any paths at all.

like image 30
ChrisF Avatar answered Nov 07 '22 02:11

ChrisF