0

Is there a way to use external flags set during linkage inside a library code that is passed to gcc linker? It would be even better for me if this could be made with CMake, which is the tool used in this project.

I am working with an input/output shared library, which provides a server to apply dynamic changes in code. This server is automatically started during library loading, by using gcc attribute constructor, as shown in the actual code:

#include "RhIO.hpp"

namespace RhIO {

/**
 * Create a new thread at program start
 * for Server reply and another thread forcd
 * Streaming Server
 * (GCC specific)
 */
static void __attribute__ ((constructor)) initThreadServer()
{
    start();
}

}

However, for the application Im working at, we are not allowed to communicate with external processes when running Release binary, so this server should be only started by Debug compiled binary.

What i want is to add something like this in the application cmake project:

if(CMAKE_BUILD_TYPE MATCHES Release)
    # Pass a linker flag here
endif()

And then use this flag inside the library code to prevent the server from starting.

Is there any possible way to do that? I have also thought of just compiling two different versions of the library and linking against one of these versions depending on build type, but it just seems far from a good solution.

  • Instead of "linker flag" you probably mean "macro definition", which can be used in the code via `#ifdef` preprocessor expression. In CMake you may add macro definition with [add_definitions](https://cmake.org/cmake/help/v3.9/command/add_definitions.html) or [target_compile_definitions](https://cmake.org/cmake/help/v3.9/command/target_compile_definitions.html) command. – Tsyvarev Apr 22 '19 at 19:57
  • But if I add a macro definition to my application code, will this be defined in the library code as well, when linking? Because my objective here is passing information to the library when it's already compiled. – Juan Freire Apr 22 '19 at 21:46
  • "passing information to the library when it's already compiled." - Ok, for such purpose compiler flags are useless. But linker flags are of a little value too, unless you want to relink the library and play dirty games with a linker. You need a way for pass parameters to the library at start or at runtime. – Tsyvarev Apr 22 '19 at 21:54

0 Answers0