Sebastian Irazabal

Conditional Compilation

Discussion created by Sebastian Irazabal on Sep 21, 2007
Latest reply on Sep 25, 2007 by Sebastian Irazabal
Hello everybody, I need your help again....

Don't be scared, it's long but it's not hard to understand

Today's about conditional compilation... I had a project with 2 different hardware interface, but the same sources code (almost). I've been working on these 2 projects separately and now I want to create an UNIQUE project containing the prevoius 2. There only little differences between them... Before continuing I want to illustrate what I'm saying: (those are examples of what I have in each file)

code_1.c:
                byte var 1;
                void func_1 (void){
                    CODE
                }
               
code_1.h:
                extern byte var 1;
                extern void func_1 (void);


code_2.c:
                byte var 2;
                void func_2 (void){
                    CODE
                }

                func_1();      // ***** FUNCTION FROM CODE_1.c*********
               
code_2.h:
                extern byte var 2;
                extern void func_2 (void);

So what I have is 2 .c files with variables and functions definitions and each .c file has its header file with the variables and functions declarations. I also have a file main.c where I include the prev 4 files:

main.c:
    #include code_1.h
    #include code_2.h

    #include code_1.c
    #include code_2.c

I don't want to mess with your head, so I'll try to be as clear as I can. Like I said before I had 2 hardware interfaces... well the first one uses the 2 code files (code_1.c and code_2.c) but the second one only uses code_2.c... So my first attempt to create an unique project was to use compiler directives (I'm starting to learn about that) and I change my main.c file:

main.c:
        #include code_1.h
        #include code_2.h

        #ifdef   HARD_1
            #include code_1.c
       #end if
        #include code_2.c  

So, by defining or not the macro HARD_1 I choose to compile code_1.c or not... I do not use the conditional compilation for the header files since they only contain declarations and not any code... So I guess I can leave them like that (is that correct?)...
But now, here's the problem... If I don't declare HARD_1 code_1.c is not compiled. But code_2.c uses a function from code_1.c and so the linker says that this function is not defined... How can I solve that??... Because the shared function (func_1) is not necessary for the second project but it is for the first project... At first I think that since func_1 is declared in code_1.h that error wouldn't appear.... I know there's a way to solve it by modifing code_2.c like that:

code_2.c:
               byte var 2;
                void func_2 (void){
                    CODE
                }
               
                #ifdef HARD_1
                    func_1();      // ***** FUNCTION FROM CODE_1.c*********
                #endif

But I don't want to do that because I had a LOT of sources files and I don't want to touch them....


Please help!!! THANKS!!!!
      





Outcomes