-2

1) I want to use variable n_threads inside my library code (that is distributed in shared library form on Windows and Linux) in multiple .cpp files.

2) I want to make library user to set it.

How to do such thing in C++?

  • I tried global file with static variables - it leads to each .cpp file having its copy;
  • I have tried just to keep it in a namespace which leads to variable being already defined in other translation units and thus library not compiling
  • I have tried external (which works on Linux with .so and does compile on Windows MSVC14) which leads to library not compiling due to unresolved externals.

What can be done to make global variable used in multiple library .cpp files be setable from outside (from library user code)?

DuckQueen
  • 772
  • 10
  • 62
  • 134

1 Answers1

-1

It's possible for a shared library to export a variable even in Windows. However, better just leave the variable as an internal singleton and provide an exported function to set it.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331