3

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.

2
  • If the header isn't getting the number of segments, then your problem is: how do i pass the number of segments ? Commented Dec 19, 2011 at 14:12
  • I believe C and C++ are different in their treatment of const. Tag changed to reflect the text of your question Commented Dec 19, 2011 at 14:17

1 Answer 1

5

If you want the other libraries to take the value externally you should declare it as external in the header:

extern const int segments;

You define it in one code file (.cpp) like you describe above:

#ifdef SEGMENTS
const int segments = SEGMENTS
#else
const int segments = 20
#endif
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.