I have a C++ library which does some numerical work. The main parameter is the number of segments. For speed it matters a factor 3 that the number of segment is const, however I would like to define it at compile time using -DSEGMENTS 32. The header looks like this:
#ifdef SEGMENTS
const int segments = SEGMENTS
#else
const int segments = 20
#endif
That works. However, programs linking with this library don’t get the -DSEGMENTS and thus segments is always 20. Without the const I know the solution, but with I don’t know. I can imagine some extern trick or installing the header after precompiling if that is possible with cmake.
If you want the other libraries to take the value externally you should declare it as external in the header:
You define it in one code file (.cpp) like you describe above: