How to get complex C pre-processor behavior to simplify dynamic program assembly

Go To StackoverFlow.com

0

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.

2012-04-05 00:33
by NoahR
What's the issue? The order of the definitions should not matter as long as the declarations are always available before, right? Also, to my experience #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
I agree the order of definitions do not matter. But I have hopefully demonstrated that I need all the declarations before I can move on tot he definitions, thus appearing at different points in the code.

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

So GCC is your target environment - 0xC0000022L 2012-04-05 00:44
Ahh, no, I think you're right. Maybe I goofed up my test earlier. #define is propagating through. So that should be my solution here, right - NoahR 2012-04-05 00:45
defined macros are visible in any files included after that define - blueshift 2012-04-05 00:45
@NoahR: I think so - 0xC0000022L 2012-04-05 00:46
Oops. Well, I'll go with that route then. hah. so much time to draft the question. :) thanks - NoahR 2012-04-05 00:47


0

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
2012-04-05 00:54
by NoahR
Ads