Sam
Sam

Reputation: 17

How can I Split c++ code into seperate files

I would like to know if anyone can give me a solution to splitting my various functions out of main to separate files. I understand that you can do this with a single class with a class.h and a class.cpp, but how do you do this if you want to split up your code in your own way.

Let us say I have a bunch of functions that have to do with my Bluetooth connection and another bunch that have to do with working with JSON files, and then a few more that have to do with math calculations. Is there a way to split these up into single separate files without using the .cpp and .h system? ( I was thinking of a single file for declarations, includes and global variables, and then single .cpp files for each category for the rest of the functions) However, I cannot see how to do it.

How would you handle such a senerio?

Thanks for any advice.

Sam

Right now I have just linked them all together with each one including the next one (as a .h file) in the list like a chain. And although this works, it is a pain when I create a new bunch of code and need to split the link and insert them in the chain.

Upvotes: -1

Views: 113

Answers (1)

pm100
pm100

Reputation: 50210

place each bundle of functional code (json, bluetooth...) into separate .cpp files (they dont have to be classes)

For each .cpp file have a .h file that callers of those functions will need. This is usually

  • function declarations
  • constants
  • structs
  • typedefs

Then callers of these functions #include the .h file

Then at compile time you need to compile all the cpp files into .o files, then link all the .o files. The syntax for that depends on the tool chain you are using

Upvotes: 2

Related Questions