10-03-2019 01:36 PM
I'm making a library for a GPIO device. This device has several macros for constant values. I'm attempting to make these replaceable inside the host application that attaches my library.
For instance:
Library
#ifndef LABJACK_CLOCK_BASE #define LABJACK_CLOCK_BASE LJ_tc4MHZ #endif #ifndef LABJACK_CLOCK_DIVISOR #define LABJACK_CLOCK_DIVISOR 14 #endif
...where LJ_tc4MHZ is itself a declared long value in the vendor header.
Now up one layer from here, my host application will set this macro to a relevant value:
#define LABJACK_CLOCK_BASE LABJACK_tc48MHZ_DIV
But what I'm finding is that the CVI compiler is making two different values for LABJACK_CLOCK_BASE! I was assuming that the compiler makes the determination of the value at the beginning of compilation and then substitutes all references of the macro everywhere. Not so!
I'm sure this is convoluted, but is there a more elegant way of going about this?
Solved! Go to Solution.
10-04-2019 01:28 AM
I admit I could not understand your problem fully, and you are probably experienced enough to know that already but have you considered making use of Preprocess Source File under Options menu?
10-04-2019 03:45 AM
Where do you make this #define in your sources? C Preprocessors will define macros as they appear in the source and reassign macro redefinitions as they appear in the logical sequence of source. So assume you have an include file "mymacro.h":
#ifndef MyMacro #define MyMacro 9 #endif
Then these things have different behavior:
1: #include "mymacro.h" 2: 3: printf("MyMacro %d", MyMacro);
This will print : "MyMacro 9"
1: #define MyMacro 11 2: 3: #include "mymacro.h" 4: 5: printf("MyMacro %d", MyMacro);
This will print "MyMacro 11"
1: #include "mymacro.h" 2: 3: #define MyMacro 11 4: 5: printf("MyMacro %d", MyMacro);
This will for most compilers generate an error in line 3: that MyMacro is already defined.
1: #include "mymacro.h" 2: 3: #undef MyMacro 4: #define MyMacro 11 5: 6: printf("MyMacro %d", MyMacro);
This will also print "MyMacro 11"