Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
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?
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.
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.
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.
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.
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
Bottom Line: sources and headers that are still changing go in /src. Code that has crystallised should go in /lib & /include (actually you could keep all .libs and their .hs in /lib).
Keep own sources and headers together, provided they are (a) specific to this project or (b) have not yet been factored out as a shared library.
Once certain sources in the main project have been factored out as a (relatively stable) library, place the .a or .lib into /lib, and its public interface header into /include.
All third party libraries and their public interface headers also go into /lib & /include.
As others note, it is often more compatible for tools / IDEs to access .h/.c from one folder. But from an organisational view it can be useful to separate changing local code from stable lib code.
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.
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.
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.
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
Related
I am fairly unused to compilation and building projects so pardon me if my approach to compilation and build seems a bit odd. Any tip is welcome.
I am currently working on a 3D geometry C++ project (Which is a dll). This project uses external dll's such as BOOST. So when building the project I have to define the directories in which the .dll, .lib and .h/.hpp files are.
Currently I am using scons to build the project and define those paths straight in the SConstruct file.
However those paths are later reused for other build operations. (In the present case compiling the C++ code in MEX for matlab but that's not really the point here).
Thus I currently have to define the same path in different places which is inefficient. In addition the project has to be easy to set up for another user. So having to change and update path in many different files is something I would like to avoid.
From where I stand I see two alternatives:
First I could ask the user to define environment variables and read them from inside my various build scripts. However I am not really satisfied with this solution for it asks the user for additional manipulation and, as far as I've understand, I lose the cross-platform portability that scons offers. (I know it might still possible but requires some extra steps and I'd like to keep things as simlpe as possible)
Second I could define all my path in a single .txt (or something similar) file at the root of my project and read it from my various build scripts. However this makes the process sensitive to typos and parsing errors which is not really to my taste.
So my question is the following:
Is there a better way or good practice to have the user input the paths necessary for compilation that satisfies the following:
Has the user only input once every path
Is done within the project folder through a file or something else
Is as foolproof as possible
Does not require too much additional download/installing (I don't really want to have the user install a brand new software for this. However I'm fine with something like a simple light .exe that I can add in my project files)
SCons's Variables are likely your best choice here.
See: https://scons.org/doc/production/HTML/scons-user.html#sect-command-line-variables
It allows reading defaults from a file:
vars = Variables('custom.py')
You'd have to craft some logic to save any variables specified on the command line.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Several years ago I created a C++ project. I used to write the code using nano and I compiled and linked it using a Makefile.
I simply had all the files into a single directory (including .cpp, .h and the Makefile itself).
For instance, I had a single inc.h file which was included in every single .cpp file, for example, something I now acknowledge as not very clean.
I would like to upload it to Github to make it accessible for anyone that wants to use it, with the problem being I can't seem to find any guide on how should I arrange the files (As there is, e.g., for creating packages in R). I have tried using Code::Blocks but I haven't managed to make the project work, so,
Can anyone point me to a guide that explains how to properly organize the code and data for C++ projects?
C++ itself doesn't have any style guide as to how to organize project files. It's one of the many little things that make package managers so difficult to realize. Every developer chooses their own organization, so if it works for you, just keep it in one folder. If you want to experiment with, e.g., separate header and cpp folders, there are examples around the internet, to be found when searching something similar to "makefile headers and source in separate directory".
For makefiles, you'd probably have to include something like
# preprocessor flags
CPPFLAGS := -I[path-to-headers]
in the line calling the compiler makefile
Most of the rules for C++ projects are project-specific, probably it's the main reason why there's no definitive guide. A couple of most generic recommendations I can think of (can be not relevant in your case):
Will your users need to include your header files? In this case put such headers into a separate "include" folder. Rationale: to avoid mistakes of including wrong header (one that is not part of your API), and to simplify analysis of your public API. I don't understand the rule to put all headers separately from .cpp files (e.g. default filter of VisualStudio projects), some headers can be for internal usage only.
Build to different folders (e.g. "bin" for shared libraries and executables or "lib" for static libraries) for each supported architecture and configuration. e.g.:
bin
|-- debug
|--|-- x32
|--|-- x64
|-- release
|--|-- x32
|--|-- x64
...
Rationale: simplify distribution and usage from external code.
you asked "how to properly organize the code and data". If you allow to provide some external configuration, or just general data, make sure you allow to specify its location instead of just hardcoding it. It's fine to hardcode the default location though. Ideally your API allows to specify the location in runtime if this makes sense in your case, or compile time option e.g. as an argument to your build script. Rationale: it will be much easier to test on different datasets/configs.
This link mentions wildcards as a way to automatically list the SOURCES and HEADERS in the Makefile.am file. It also mentions that some people write external scripts to generate those files.
Do you know of the standard way of automatically including all the *.h *.cpp here, or should I just write my own Perl script to generate them. Do you have such a script already that you use?
PS: I organize the source files in my project according to the following purely-logical separation of directories:
src/dog/woof.h
src/dog/woof.cpp
src/cow/moo.h
src/cow/moo.cpp
Automake won't add this feature. It makes assumptions that a particular .h or .cpp file is associated with a particular project. That assumption holds for a number of common project layouts and fails for any layout that differs.
For example, I've had projects that were laid out as
src/module/code
src/app/code
src/library/code
include/headers
built from one central makefile in the root. Other times, I've had the same layout built from four makefiles in the appropriate local directories.
There's a lot of variability in projects. Some keep public header files mixed with private headers files in the code directories, some keep them separate. Some build shared object libraries, some don't. Some ship code that's not SUPPOSED to compile on incompatible platforms.
To put in a wild card inclusion would actually pose a great risk of limiting the functionality, and for those odd people who do things like 'file.template.c' and such it would be fatal.
If you consider it a flaw of automake, that's fine; however, it's one of those flaws that automake embraces as it's preserved in the effort to make things more flexible. Automake doesn't impose the "how" you do things, it provides a lot of enabling tools, but it goes out of it's way to ensure that you aren't forced into one "method" of laying out or building your code.
Currently, I program in Java and use Maven quite a bit. As so I've become accustom to the naming schemes and folder structures that I've used over the past 4 or 5 years.
As I have recently started to learn C++, I'm realizing that I have no idea where to put all my files. Should I keep everything broken down by namespace, or by what tier it is in? Where, for example, would I keep a series of files devoted to UI, as apposed to files meant to help store data?
Are there any standards for this sort of thing?
Clearly, there is no definitive answer to this question. I'm simply looking for a good guide. I do not want to start learning C++ by spending too much time worrying about how my files are laid out. I'd rather have some good models, and just get to the coding.
The following is fairly typical...
third-party library
release
obj
debug
obj
include
src
sublib 1
sublib 2
mylibrary
release
obj
debug
obj
include
src
sublib 1
sublib 2
myapp
release
obj
debug
obj
subapp 1
subapp 2
mylittleapp
release
obj
debug
obj
Basically, subfolders for subprojects is common for larger projects, but mostly a particular project has folders for src, include etc. A folder for each build configuration is common, and keeping the obj files and other intermediates in a subfolder of that is a good idea. It may be tempting to put subproject folders in obj folders, but usually that's unnecessary - the obj folders don't need to be well organised, so the only concern is a filename conflict, and the best fix for that is to have unique source filenames within (at least) each project.
The "include" folders should IMO only contain headers that will be #included by other projects - internal headers belong in the "src" folder.
Putting UI stuff in a separate folder isn't a bad idea, if it's big enough. I've seen UI stuff done as a separate static-linked top-level project, and I do mean app-specific here, not (e.g.) wxWidgets. Usually, though, that level of division is sub-project if it's worth separating at all. How you divide subprojects is more a matter of application-specific blocks in general, so it depends on whether UI stuff is best handled as a separate block or as separate chunks mixed in with task-specific logic.
Namespaces aren't the most used language feature, possibly because a lot of people use "using" so much they don't make much difference. A namespace for a main library project makes sense, but associating subfolders to namespaces 1:1 isn't something I've seen. I personally have a namespace that encompasses most of my library code, with a couple of sub-namespaces for things rarely used in general, but used a lot in a few places (e.g. a "bitwise" namespaces). The sub-namespaces are limited to single source/header pairs, so no need for subfolders. Most of the library-specific selection is done by including the right header - except that I usually include the lot through a main-project top-level header anyway.
Basically, namespaces are a way of avoiding naming conflicts. They don't necessarily associate with abstractions or functional blocks or anything. Within a particular project, you're probably better off just making sure the names don't conflict. As with the "std" namespace, it's fine to put a lot of stuff in one namespace.
As you say, though, this isn't a definitive answer - there are of course minor variations and quite different approaches.
On small projects my team groups all the files together by a link unit ie library, DLL, EXE. If the unit is very large we will sometimes breakup the files by functional unit or subsystem so that if you need to edit a component they are generally in the same place.
I break my projects by theme, one directory for theme:
menu_planner
src
recipes
debug -- contains debug object files and libraries
release -- contains release object files and libraries
obsolete -- contains obsolete source files
ingredients
debug -- contains debug object files and libraries
release -- contains release object files and libraries
obsolete -- contains obsolete source files
references
debug -- contains debug object files and libraries
release -- contains release object files and libraries
obsolete -- contains obsolete source files
meals
debug -- contains debug object files and libraries
release -- contains release object files and libraries
obsolete -- contains obsolete source files
menus
debug -- contains debug object files and libraries
release -- contains release object files and libraries
obsolete -- contains obsolete source files
docs
designs
My experience with C and C++ has shown to me that header and source files should be in the same directory. Often, finding a header file is more difficult when it is not in the same directory as the source file.
One directory (folder) per concept is a good idea. Any concept that is complex or compound should be split into multiple folders or concepts.
I've also learned to make libraries. I use libraries to contain code that doesn't change much. The linking step performs faster with libraries than directories of object files.
However, work places (a.k.a. shops) may have different style rules that must be followed.
It is not necessary to have your header files and cpp files in the same folder. I have done this many times. You can have the in different folders and use another file to fetch/include both files on file, which you will use as your include.
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.
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
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.
If you must include third-party code instead of just linking with a pre-compiled version (e.g., perhaps you need to make modifications or tweaks to it), consider branching it in whatever you use for source-control:
/trunk/... --- your code goes here
/thirdparty --- pristine copies of third-party libraries go here
/thirdparty/lib1
/thirdparty/lib2
etc.
/trunk/lib1 --- branched from: /thirdparty/lib1, perhaps with local changes
this is the version that you build/link with.
Assuming you use a decent source-control system, this scheme will allow you to easily upgrade to newer versions of third-party libraries and then merge those changes with the changes you've made locally.
For example, suppose "lib1" releases a new version:
Commit the change to /thirdparty/lib1.
Merge from /thirdparty/lib1 to /trunk/lib1
Fix any merge conflicts.
This is, IMO, the only sane way to handle upgrading third-party libraries to which you've made local modifications.
First: Add all used directorys to your project include paths. Add them as relative paths if possible.
Second: You must add all used librarys/source files to your project. This can be either done in the project explorer, or in the Project->Linker tab. In the latter case, you'll have to add the used directories to the projects library paths as well.
Usually its not a good idea to use paths in #include directives.