I am trying include a file constructed from pre-processor macros, but running into a wall due to rules regarding tokens, it seems. I used the answer here as a reference: Concatenate string in C #include filename, but my case differs in that there are decimal points in the define I am using to construct my include. This is what I have currently that will not get through the preprocessor stage: main.c:
#include <stdio.h>
#include <stdlib.h>
#define VERSION 1.1.0
#define STRINGIFY(arg) #arg
#define INCLUDE_HELPER(arg) STRINGIFY(other_ ##arg.h)
#define INCLUDE_THIS(arg) INCLUDE_HELPER(arg)
#include INCLUDE_THIS(VERSION)
int main(int argc, char **argv) {
printf(INCLUDE_THIS(VERSION));
fflush(stdout);
#if defined (SUCCESS)
printf("\nSUCCESS!\n");
#endif
return EXIT_SUCCESS;
}
other_1.1.0.h:
#define SUCCESS
Were I to use #define VERSION 1_1_0 and renamed the header accordingly it would work (but not viable for my use as I have no control over the name of the header files the actual project uses), but 1.1.0 is not a valid preprocessor token.
EDIT:
After a bit more digging through the documentation, I see that 1.1.0 is a valid preprocessing number; it is the resulting concatenation of other_1.1.0 that is invalid. Regardless, the issue of not being able to construct the include remains.
#include "other_version.h"butother_version.his a symbolic link toother_1.1.0.hthis week, andother_1.1.1.hnext week, etc.-DVERSION=${version}in a shell script/makefile, you could consider doing-DVERSION_HEADER='"other_${version}.h"'where the single quotes prevent the shell from stripping off the double quotes — though you can also arrange to stringify VERSION_HEADER inside your code. It's a bit of a nuisance, but fixes the problems with the preprocessor by not using the preprocessor to fix them. In the code, you write#include VERSION_HEADERunder this scheme (or maybe#include STRINGIFY(VERSION_HEADER).#(stringification) variant, and SO 1489932 for the##(token pasting) variant.