Where do you put source files?

Hello everyone, so I was messing around with Vexcode pro and I noticed that you can create headers and source files. I understand the difference between them and how to use them, but I noticed that you can create header files and source files in include or src. Is there a specific place to put them or does it matter and what is the best practice?

Thank you in advance! :grin:

Generally I’d put headers in include directory and cpp files in src. I am not sure if it matters though, because you can also add multiple directories.

(Credit to 2158 for teaching me about file management and headers)

6 Likes

I am having trouble with my code. When it tries to read my source files it says that it can’t find them. I included them in my code, but it still won’t read it. Is it because I have my source code in src instead of include? If so, then how do I fix it?

Could you screenshot your file hiearchy from the vexcode software? I think I’ve made a similar problem before but I’m not sure.

Screen Shot 2020-08-01 at 9.09.33 PM

JJKFunc.h is where I included all my source files to.

Guess it wasn’t what I was thinking (I was thinking you could’ve made a file outside of the directories which is possible, but it doesn’t look like it). Does it cause a compilation error when you compile your code or something?

Yes it shows this:
Screen Shot 2020-08-01 at 9.12.05 PM

In C++, a variable name cannot start with a number. I assume the same would apply to the file names as well. It seems that the only file you have starting with a number is causing the issue so try changing that.

2 Likes

Why are you doing #include"4Drive.cpp" in a header file. Why are you including cpp files anyway?

7 Likes

Yep, it’s generally unnecessary to include cpp files in header files, or rather bad practice

2 Likes

Oh, I didn’t know that.
Then what’s the point of using a header file?

Not just bad practice, but it will generally make your code not compile. If your code does compile it probably will not link because there are multiple definitions of the same thing. All a #include statement does is tell the preprocessor to go find that file and stick it in where the #include is.

4 Likes

See the bottom half of this post:

For more details: https://www.learncpp.com/cpp-tutorial/header-files/

8 Likes

A header file is to create an access file and organize accessor variables to source files. When you create methods and variables in a source file to organize them, you can organize them more by creating header files in which the accessor variables are defined.

4 Likes

A header file is meant to keep code organized and provide function/variable declarations; this is different from function definitions. You can put code into headers, but it is generally bad practice unless it is something really tiny and trivial.

https://www.learncpp.com/cpp-tutorial/header-files/

7 Likes

Header files are also where classes are typically created to allow them to be reused much more easily. If you go advanced enough to start creating classes, the header file is where it is the easiest to do so and is traditionally where it is done.

3 Likes

Oh, this makes a lot more sense now!! Thanks! :nerd_face:

2 Likes