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.
I have tested the case (gcc) and seen several references that #define only has 'file scope'. If not, that would be perfect for my purpose, but probably hell for the rest of the world - NoahR 2012-04-05 00:43
One simple solution would be to #define
a macro to distinguish the declaration from definition sections in the code. The #define
DOES propagate to the #include
files.
// mainfile.c
#define DECLARATION_MODE
#include "ParentFile.txt"
#include "ChildFile.txt"
#undef DECLARATION_MODE
#include "ParentFile.txt"
#include "ChildFile.txt"
int main()
{
return aFunctionParent( 5 );
}
// ParentFile.txt
#ifdef DECLARATION_MODE
typedef float Type1;
int aFunctionParent( Type1 t );
#else
int aFunctionParent( Type1 t ) { return aFunctionChild( t ); }
#endif
// ChildFile.txt
#ifdef DECLARATION_MODE
int aFunctionChild( Type1 t );
#else
int aFunctionChild( Type1 t ) { return 2*t; }
#endif
#define
will have an effect on anything included afterwards and I have included files twice using a method similar to the one you propose. What CPP are you using? GCCs works to my knowledge, so does VS and MCPP - 0xC0000022L 2012-04-05 00:40