I am writing a program to assemble code for another dynamic program written in the OpenCL language. For the purpose of this question, let’s just assume the dynamic program language is C99 with the restriction that all code must be brought into a single compilation block.
My larger program determines a sequence of steps that will occur in the main function and which C99 source files are needed to implement those steps. It then invokes a C99 preprocessor / compiler to produce the dynamic program executable. I can supply the compiler macro definitions using the -D SYMBOL=defn calling argument.
Here is an example scheme that works for me and avoids diving into utilizing a separate pre-processing library or other hefty string manipulation tools. I would dynamically create the string listed as mainfile.c, the other included files would actually exist.
Is there any way I can combine the separate declaration and definition files into one file?
// mainfile.c
#include "ParentDeclarationFile.txt"
#include "ChildDeclarationFile.txt"
#include "ParentDefinitionFile.txt"
#include "ChildDefinitionFile.txt"
int main()
{
return aFunctionParent( 5 );
}
// ParentDeclarationFile.txt
typedef float Type1;
int aFunctionParent( Type1 t );
// ChildDeclarationFile.txt
int aFunctionChild( Type1 t );
// ParentDefinitionFile.txt
int aFunctionParent( Type1 t ) { return aFunctionChild( t ); }
// ChildDefinitionFile.txt
int aFunctionChild( Type1 t ) { return 2*t; }
I’d like to have a single file like this that I can #include twice.
// SingleParentFile.txt
#ifdef DECLARATION_MODE
//declarations here
#else
// definitions here
#endif
but #define definitions do not pass into #include’d files ( edit: tured out to be incorrect. )and supplied -D definitions can’t change in the middle of pre-processing. Right?
Is there any sort of preprocessor state variable that I could permute after the first pass?
Note: I tried to provide a simplified example that requires the declaration and definition sections appear in separate code locations. The intent of my question is not to find a way around that in this simple example.
One simple solution would be to
#definea macro to distinguish the declaration from definition sections in the code. The#defineDOES propagate to the#includefiles.