Is it possible to implement conditional compilation in a static library depending on the defaults defined in the installation, without rebuilding this library?

------------------------- defines.h (LIB): #define VARIS10 ------------------------- libfile.h (LIB): #include "defines.h" ------------------------- libfile.cpp (LIB): #include "libfile.h" #ifdef VARIS10 int var = 10; #else int var = -10; #endif ------------------------- test.cpp (EXE): #include "stdio.h" void main() { extern int var; printf( "var = %d. ", var ); } 

It would be desirable that when define is changed in defines.h, a different section of code in libfile is executed without rebuilding lib. Is it possible? Who does not understand, when building a lib with #define VARIS10, the program displays "10", and without it "-10" - regardless of whether exe was compiled with the presence or absence of define.

    2 answers 2

    depending on the defaults defined in the installation, without rebuilding this library?

    No longer! Preprocessor and "without reassembly" are incompatible things. The preprocessor is working “on the compiler,” with exactly the module for which these dependencies are required.
    -
    The static library works at the linker level, i.e. later compiler, therefore - to take into account the dependencies of already assembled libraries in this way will not work.
    -
    ps: just yesterday I explained what a static library is.


    In order not to rebuild the library, you need to #ifdef VARIS10 condition from the preprocessor of this library, for example, into the function:

     void Setup( bool VARIS10 ){ if( VARIS10 ){ var = 10 }else{ var = -10 } } 

    and in exe, let's say - leave it, if so:

     extern int var; void Setup( bool );//объявление функции стат. библиотеки ... void main() { #ifdef VARIS10 Setup( true ); #else Setup( false ); #endif printf( "var = %d. ", var ); } 
    • That is what I would like not to do in any way? - cyrax
    • In order not to rebuild the library, you need to #ifdef VARIS10 condition from the preprocessor of this library, for example, into the function: void Setup (bool VARIS10) {if (VARIS10) {var = 10} else {var = -10}} and in exe, Let's say - leave, if so necessary: ​​extern int var; void Setup (bool); // function declaration stat. libraries void main () {#ifdef VARIS10 Setup (1); #else Setup (0); #endif printf ("var =% d.", var); } - mega
    • one
      Thank. Make it the answer, I will mark as accepted. - cyrax
    • Edited the answer. - mega
    • > The preprocessor is working "on the compiler", I would replace "on" with "before". And that brings some confusion. - KoVadim

    compilation

    without rebuilding

    Doesn't it make you think?

    • one
      Well, exe, I will recompile. - cyrax
    • I suggest reading about the preprocessor. - falstaf
    • exe is not compiled but compiled (linked) and compiled source and machine codes - ProkletyiPirat