Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor macros in static library

In a Objective-C project I am using a static library, compilation of this static library depends on some preprocessor macros to be set.

When I set these macros in the project depending on the library the library does not see them. But when I set them in the library project it does work.

Since I want to reuse this library for other projects, I require to set the preprocessor macros for each project depending on the library separately. Is there a solution for this?

like image 996
Thizzer Avatar asked Feb 23 '23 16:02

Thizzer


2 Answers

Preprocessor maros only have any meaning at compile-time, so any library you build will be specific to the values of these preprocessor macros at the time you built the library. You will either need lots of different versions of your library, built with the different possible values of your preprocessor macros, or you could switch to using a different method to control the behaviour of your library code which will work at run-time, e.g. setting some appropriate parameters through the library API.

like image 110
Paul R Avatar answered Mar 03 '23 05:03

Paul R


This is not an answer per se, but something interesting I discovered while struggling with this same issue.

I have a static library (MyLib) that contains a header for logging (Log.h). I have an application project (MyApp) that uses MyLib. Log.h has some resemblance of this:

#ifdef LOG_LEVEL_DEBUG
#   define LogDebug(...) NSLog(__VA_ARGS__)
#else 
#   define LogDebug(...)
#endif

In MyApp build settings, I can use the preprocessor macro LOG_LEVEL_DEBUG to successfully turn off and on logging. This works when I use LogDebug() in source files found in MyApp. However, the MyLib source files that use LogDebug() are not affected by the MyApp build settings. I have to use the MyLib build settings to affect LogDebug() within the MyLib source files.

I am pretty sure I know what is happening but I'd be open to correction. Below is the scenario where MyApp defines LOG_LEVEL_DEBUG in build settings (enabling debugging) and MyLib does not define it (disabling it).

When MyApp builds, it first compiles MyLib where all of the LogDebug() are replaced within the MyLib source files as no-op (since LOG_LEVEL_DEBUG was not defined). After MyLib is compiled, MyApp is compiled and all of the LogDebug() methods within MyApp source are replaced with NSLog() statements because LOG_LEVEL_DEBUG was defined in the build settings.

like image 29
TPoschel Avatar answered Mar 03 '23 07:03

TPoschel