Handmade Network»Forums
1 posts
How do you deal with C/C++ not looking up symbols in advance in a compilation unit?

Hey,

This is a generic programming question. In theory I could get a ton of answers by posting on StackOverflow etc., but I'm affraid they'd all be stemming from the same, current dogma, and I'm looking for unconventional approaches.

I am trying to more or less follow Casey's approach to code organization. My problem occurs when I'm trying to minimize the number of transalation units in my C++ code, ideally to one (this is done to minimize compilation time.). However, unfortunately C++ compiler is not allowed to look ahead in the translation unit for the symbols that it needs (any reference to a previously undeclared or undefined symbol is an instant compilation error). This leads to either having to painstakingly rearrange functions in the source file so that there are no forward calls (which incidentally leads to entire program being stored in a backward order in the source file), or having to add those annoying function declarations.

Coming from languages where such nonsense was not required, I hate it with passion :) Now, my question is - what are your approaches to dealing with this in practice?

Thanks, Easterner

Simon Anciaux
1335 posts
How do you deal with C/C++ not looking up symbols in advance in a compilation unit?

For the last few months I tried to have a meta program that runs before the compilation and generates a .h file that contains forward declaration of types and functions. I mark functions with a macro so that I don't have to parse C to find function prototypes, I just do a text search.

It's been working quite nicely, I still had an issue at some point but I don't remember exactly what it was.