-2

I'm trying to get the benefits of splitting without two files. Split compilation without splitting storage.

I understand the benefits of separating .h and .cpp files, but I really dislike having the files be separate, specifically when the classes are tiny and each file could fit on the same page.

Is there a precompiler option, or perhaps any other trick which would allow me to keep the benefits of separation, while having the text all in the same place? For example:

EDIT: please do not focus too much on this example. It was meant to show off an imaginary pre-processor arg #CPP_SPLIT. The actual code is unimportant, please, please ignore it.

// TinyClass.h
class TinyClass {
  TinyClass();
  int answerToLife();
}

// the following is a fake compiler arg
// in this example it would be totally unnecessary, 
// but many of my classes have some form of circular referencing
// and can not include all the code in the .h file
#CPP_SPLIT

TinyClass::TinyClass() {}
TinyClass::answerToLife() { return 42; }

#CPP_SPLIT_END
Seph Reed
  • 8,797
  • 11
  • 60
  • 125
  • 2
    `because of circular references` Then give more adequate example. You can declare all functions as static, thus have same symbols per transaction unit, but that's bad. If you have circular references, then using header file to declare the functions and using the source file to define the functions is the proper way anyway – KamilCuk Dec 15 '18 at 20:47
  • I'm trying to get the benefits of splitting without two files. The question was good, it is you who assumed the worst of me. There are good reasons to split, and logical reasons to not want extra files. – Seph Reed Dec 15 '18 at 20:50
  • @SephReed To resolve circular dependencies there are other solutions: https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – πάντα ῥεῖ Dec 15 '18 at 20:56
  • This is not a question about circular dependencies. I know about forward declarations. Please, can we keep discussion to the question rather than the impetus? – Seph Reed Dec 15 '18 at 20:58
  • @SephReed _"I need a this because of circular references."_ Why are you mentioning that in your question then? – πάντα ῥεῖ Dec 15 '18 at 21:03
  • Because otherwise it would seem, just use `inline` or `put directly in the header` are solutions. The example did me a disservice here, everyone keeps looking at it, and not the question. – Seph Reed Dec 15 '18 at 21:06
  • 1
    @SephReed Don't edit already answered questions that way please. That's disrespectful. Ask another more clear question if the results here don't satisfy or solve your problem. – πάντα ῥεῖ Dec 15 '18 at 21:08
  • What "benefits of separation" specifically do you want, if you don't actually want the separation? – aschepler Dec 15 '18 at 21:15
  • I want function separation, but not storage. This post has some good reasons for wanting the function: https://stackoverflow.com/questions/27456772/should-i-separate-cpp-and-h-file-in-c. I don't want the storage because I don't like having two **text files** where one could do. They are both just text, they can be in the same file, but still function separately. – Seph Reed Dec 15 '18 at 21:19

3 Answers3

1

I'm not sure it's worth the effort, but you could place the contents of your .cpp file into #ifdef'd sections, like this:

#ifdef PART_ONE

[...]

#endif

#ifdef PART_TWO

[...]

#endif

#ifdef PART_THREE

[...]

#endif

... and then recompile the file over multiple passes, like this:

g++ -DPART_ONE   -opart1.o myfile.cpp
g++ -DPART_TWO   -opart2.o myfile.cpp
g++ -DPART_THREE -opart3.o myfile.cpp
g++ -o a.out part1.o part2.o part3.o
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • 1
    Though for most programs, things like class definitions that normally belong in a header file will need to be visible in more than one "part". – aschepler Dec 15 '18 at 21:30
0

You can simply put the implementation directly in the header like this:

// TinyClass.h
class TinyClass {
  TinyClass() {}
  int answerToLife() { return 42; }
};

Also inline might help to do what you want:

// TinyClass.h
class TinyClass {
  TinyClass();
  int answerToLife();
}

inline TinyClass::TinyClass() {}
inline int TinyClass::answerToLife() { return 42; }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Sorry, I added details. There are reasons I'm asking this question. I'm very aware you can put all code in a .h file, I'm trying to get the benefits of splitting without two files. – Seph Reed Dec 15 '18 at 20:43
  • @SephReed You may use some `#ifdef` statements, but that requires that the code including your header sets the necessary macros using `#define` prior to the `#include` statement. Also `inline` definitions might solve your problem without ending up in multiple definitions. – πάντα ῥεῖ Dec 15 '18 at 20:46
  • I'll take what I can get. I just really don't like having to rummage through so many files. – Seph Reed Dec 15 '18 at 20:47
  • I'm trying to get the benefits of splitting without two files. This would not get the benefits. – Seph Reed Dec 15 '18 at 20:51
  • @SephReed With `inline` definitions you don't need to split your code into two files, you probably misunderstood my answer. – πάντα ῥεῖ Dec 15 '18 at 20:52
  • Does inline compile after all the .h files, similar to a .cpp file? – Seph Reed Dec 15 '18 at 20:55
  • @SephReed I don't get what you mean? – πάντα ῥεῖ Dec 15 '18 at 20:57
  • Can I split compilation without splitting storage? .cpp files compile after .h files. I want to do that, but without having two files. It's all just text, it's totally possible. But maybe the feature does not exist. – Seph Reed Dec 15 '18 at 20:59
  • All files compile in the order you present them to the compiler, regardless of them being .h or .cpp Are you looking for a way to make the compiler ignore part of a .cpp file? – Lev M. Dec 15 '18 at 21:01
  • @SephReed I still don't get what you mean? `.h` files aren't compiled prior to `.cpp` files or such, they are just expanded by the preprocessor as plain text when compiling the `.cpp` files. You should read a bit more about how the compilation and linking process really works. – πάντα ῥεῖ Dec 15 '18 at 21:02
  • Okay, you're right. It is the order of presentation. But usually that means .h first and .cpp later. I want to start over. Can you imagine a scenario in which you need to separate your cpp and h files or else it won't compile? An example might be circular referencing, but anything you can imagine works. – Seph Reed Dec 15 '18 at 21:05
  • @SephReed When something is put in to an .h file, it is usually a declaration that must be inserted in to multiple .cpp files. So the .h file saves the need to copy paste. Why do you need the separation in your scenario? Are you getting errors about duplicate code? – Lev M. Dec 15 '18 at 21:07
  • It does not matter why I need the separation. Can you imagine a situation where you would? Please, yes or no. We can move deeper into discussion from there, but I need to know if you can imagine any reason these two files must be separated. – Seph Reed Dec 15 '18 at 21:11
  • @SephReed There's no situation that really requires you to separate declaration and definition, unless your buddy coders start complaining about utterly long compilation times if you do changes in your definitions. – πάντα ῥεῖ Dec 15 '18 at 21:14
  • @SephReed I can imagine several reasons to separate the files. But I can not imagine a single solution that would replace all those reasons without separation. So, if you are looking to eat your cake and have it too, help us out by explaining what flavor you are looking for - what are the features of file separation you want to achieve in a single file? – Lev M. Dec 15 '18 at 21:15
  • Thank you. I would like to have circular referencing without forward declaration. I would also like to have the shorter compile times. If you have an answer to the former problem, I'd asked another question here: https://stackoverflow.com/questions/53796867/how-can-i-pass-this-to-a-constructor-without-circular-references-or-losing-ty/53796897#53796897, and you're welcome to suggest a non separate file solution. – Seph Reed Dec 15 '18 at 21:17
  • @SephReed Well, there's at least one dead you'll have to die. With the current standard there's not really solution for that problem available. Maybe proposed [c++ modules](https://medium.com/@wrongway4you/brief-article-on-c-modules-f58287a6c64) might solve that better in future. – πάντα ῥεῖ Dec 15 '18 at 21:25
0

Another potential solution to this appears to be the proposed c++ modules standard. If you happen across this many years down the line, look there.

Seph Reed
  • 8,797
  • 11
  • 60
  • 125