Including files.

Hey guys so I think that I will write different functions in different .cpp files and include them into main.cpp. But I don't know how to do it. Do you keep them in the same folder and then
 
#include <function1.cpp> 
?
No.

Each *.cpp file is compiled independently into an object file, and then a linker takes all the object files and joins them together.

At this point, we can go one of two ways. We can ask you specific questions about what OS you're using, what IDE (if any) and what compiler, and give you steps to achieve this. In an IDE, it often involves whatever that IDE calls a "project".

Or, you can read about how source files become programs over here - https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries - and work it out for yourself. If you do read and understand that link, you will be in the top 25% of C++ programmers :)
Last edited on
Do you want to just separate define functions in a different file? You can do that.

But you should do this with "header" (.h) files and "implementation" files (.cpp), though that naming convention is not absoltely necessary. However, some compilers will require you to adhere to those conventions, so it best to do so.

Declare the functions in the header file and define them in the cpp file.

http://www.cplusplus.com/forum/articles/10627/

Although, however, note that you do not have use classes to define a function inside of a header file. But be careful of how you name your functions. You could get naming conflicts. This is actually the reason why so many APIs written in C may have long prefixes. These name collisions can be avoided by using namespaces.

Or use Moschops link if you wanted to leane about how source files get turned into binaries.
Thanks guys! For the record, I'm using Deuter IDE on Android, since my PC has turned to mush. I do seem to understand that a little! Will work on that link!
Topic archived. No new replies allowed.