0

By finding-c++-static-initialization-order-problems, I want to define a macro in a header file, e.g. "check_fiasco.h", then put this header file to the top of every cpp file, as Warren Stevens' idea. However, I tried his code under linux, and it does not work(it compiles, but crashes when it gets executed).

I tried the following macro:

#define TOKENCONCAT(x, y) x ## y
#define file_init_(x, y) TOKENCONCAT(x, y)
#define FIASCO_FINDER_DEC \
     void file_init_(file_, __FILE__)(void) ((constructor)) ;

#define FIASCO_FINDER_IMPL \
     void file_init_(file_, __FILE__)(void) __attribute__ {\
        printf("Good Morning, %s!\n", __FILE__);\
        return ;\
     }

However, this does not work, as the pasting file name with "/" is not allowed for function name. I cannot use __COUNTER__ either, as the declaration and implementation has to be the same.

Any ideas?

Community
  • 1
  • 1
pepero
  • 7,095
  • 7
  • 41
  • 72

1 Answers1

0

You don't need the actual symbol to be a global symbol, hence the need to make it unique. A static symbol should work fine:

static void file_init_() ((constructor)) ;

static void file_init_()
{
    printf("Good Morning, %s!\n", __FILE__);
}

Or, if you insist, you can put your global function into an anonymous namespace, which the compiler will ensure will be unique per translation unit.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • can you elaborate more? i mean, how the header looks like, and how cpp file uses the header? e.g., I tried Warren Stevens' idea, it compiles, but does not run with my platform (arm). so far, what works is I put the functions declaration and implementation directly in cpp file. but as I have hundreds of thousands files, so i'am trying with header file, but did not make it work :-( – pepero Jul 01 '16 at 12:58
  • I have no idea what you need to elaborate on. This is what would get compiled, so a macro would just define this. – Sam Varshavchik Jul 01 '16 at 16:38