Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

src/ folder structure in C++?

i'm coming into C++ from Java/AS3-land, and i'm used to the package-cum-folder structure for my classes. and i like it.

i understand the very basics of namespaces in c++, and i'm happy to leave it at just the basics. but, as my project gets more complex, i'd like to keep my folder structure organized in a way i can keep in my head. i.e. something similar to Java/AS3.

1) is there any reason to not have a folder structure like:

src/
 model/
 view/
 controller/

possibly with subfolders? (this is just an MVC example, the folder structure could be whatever depending on the project's needs.) it just seems unruly to have a src/ folder with a huge pile of header and source files within.

2) if the answer to 1) could be "go ahead and do what you want", would it be unwise/unnecessary to create a namespace for each folder, similar to Java/AS3's way of creating a package for each folder? my understanding is that namespaces are not usually used like this, nested deeply and folder-related.

like image 663
ericsoco Avatar asked Nov 21 '10 23:11

ericsoco


People also ask

What is src in C programming?

System Resource Controller (SRC) commands are executable programs that take options from the command line. After the command syntax has been verified, the commands call SRC run-time subroutines to construct a User Datagram Protocol (UDP) datagram and send it to the srcmstr daemon.

What should be in src folder?

The src stands for source. The /src folder comprises of the raw non-minified code. The /src folder is used to store the file with the primary purpose of reading (and/or editing) the code. The /src folder contains all the sources, i.e. the code which is required to be manipulated before it can be used.

Why use src directory?

A src directory makes certain configurations easier to manage since sources are clearly separated from config files, static asset or data and node-modules and such. For example having webpack resolve all directories in src for easy import statements, or running some tool like prettier-eslint over all js files.


1 Answers

I've always liked the namespace for each folder. Mostly because when I have to maintain somebody else's code, the namespace helps me find where the class was originally defined.

Well named header files can also help with this though. I also wouldn't suggest going more than 2-3 namespaces, as then it just becomes obnoxious. You'll find yourself using "using namespace blah;" a lot which I always find to be a red flag for C++ code. And you can't use "using namespace" inside a header file without some severe problems occurring.

It's all completely optional though in C++.

like image 146
Jonathan Sternberg Avatar answered Sep 19 '22 11:09

Jonathan Sternberg