How to go about organizing my C++ files?

hi guys, c++ beginner here.

I'm reading 'Programming: Principles & Practice Using C++' and have just managed to compile and run my first .cpp file from the Mac Terminal after a bit of struggle configuring things - great feeling. I'll try to keep this short. My questions are:

For this file, I created a folder, 'hello', opened that in VS Code, saved a file to that folder, 'helloworld.cpp', and also placed 'std_lib_facilities.h' in that folder (the header Bjarne suggests for now). I then move to 'hello' from the Terminal, run 'clang++ helloworld.cpp -std=c++11' which gives me 'a.out', which I run with './a.out' . Repeating these steps with a new file inside the same 'hello' folder overwrites the previous 'a.out' -

-Should I be creating a new subfolder for every file I want to compile, so that it can hold 'a.out' without overwriting previous executables?

-If so, is there a different location I can place 'std_lib_facilities.h' so I would not need to copy it into every new subfolder?

-Lastly, as you can see, I had to include the '-std=c++11' compiler flag in order to get things to work - I was getting lots of errors without it. Is there anything I should change so I don't need to add this every time, which I can then change back when I move on from C++ 11 (the version this book uses)? Or should I just stick with adding that every time for now?

Any and all help is appreciated. Looking forward to learning more c++ and being a part of this forum :) thanks guys
you typically have folders yes.
eg
\c\include <---- your common, shared, includes, stuff you reuse
\c\src <------ your cpp files that go with the above includes.
\c\projectname <------- each project has its own folder

you also want to compile with a flag to name a.out to programsname.out (or whatever extension you want to use for the executable file)

your makefiles/ builds will pull the common folders into your projects. Hopefully your IDE makes this simple. If doing command line, its not too bad just more clutter. If your code is still very small, you can use a shell script or batch file (windows) to compile it instead.

you can have multiple projects in one folder if you rename the output, but its going to get messy in there after a bit.
Last edited on
thanks jonnin, I googled and have found the '-o' flag to rename the executable, I will start using that. I also made an include folder and have changed the include line in my program to

#include "../include/std_lib_facilities.h"


in order find it - works great. No need to add the file to every subfolder I see. As for the 'src' folder - what kind of files would I want to be putting in here exactly?
When you create a class that’s on the bigger side in a .h file

Car.h
1
2
3
4
5
6
7
8
9
10
class Car{
  int n;
  std::string var2;
  public:
  Car();
  void GetName();
  int func2();
  int func3;
  bool forSale();
};


You generally make a separate file for the implementation of the functions and stuff that instead of ending .h ends in .cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include "Car.h"

Car::Car(){
  // do stuff
} 

void GetName(){
  // do stuff
}


//... and the rest of the funcs 


Edit: so basically it’s just the implementation of your earlier .h files
Last edited on
Programmers typically create new folders for projects, but since your current projects are one or two files at most, that may be a bit much for starting.

We also end up with directories under that project directory representing the builds, because we'll build for several variants (usually debug & release, and in some operating systems both of those for 32 and 64 bit targets).

You won't be using command line operation of the compiler for long, though. Eventually you will either use some build system, or an IDE. These allow you to define the compilation, and so those flags you're asking about become part of the definition of the project for those build systems. That includes, particularly, the include directories to specify (we typically have several), and (something you're not yet read for) the binaries for the libraries we include, when required.

Most professionals use IDE's, though you will run into some who insist you must know the command line tools instead. Personally, after 40+ years doing this, the IDE is the most productive when applicable. However, it is true that we must use build systems in many projects, because we build for various operating systems.






ok, so just my .cpp files essentially. I've been googling around and see that the src folder is meant for source code so that would make sense.

and thank you very much niccolo, that overview really helps to clear some things up I've been curious about. you're right, I definitely dont need any serious folder structure for now, I was just curious about what to do with all the .out, .cpp, and .h files and how to tie them together a little more cleanly. i've got a better grip on it now. thanks guys!
one last thing.. eventually, you will want to clean intermediate files after you have a 'final' executable (or library, whatever target) file to save disk space and clutter. bigger programs can fill a hard drive with this stuff.
got it. i will keep that in mind down the road. thank you for your help jonnin
Topic archived. No new replies allowed.