I'm trying to configure cocoalumberjack and when I've added ddLogLevel
set to LOG_LEVEL_VERBOSE
XCode throws "use of undeclared identifier" error. Why is that? How to avoid?
This question indicates that clearing DerivedData
and restarting Xcode solves this kind of error.
However you should not include variables in the pre-compiled header as it will be included in every source file and prefix files are somewhat complicated compared to normal header files.
Better is to have use a Constants.h
file which contains:
extern int ddLogLevel;
and #import
that into your prefix file.
Then create an Constants.m
with:
int ddLogLevel =
#ifdef DEBUG
LOG_LEVEL_VERBOSE;
#else
LOG_LEVEL_ERROR;
#endif
This way there is only one instance of ddLogLevel
and it can be easily changed at runtime if necessary.
See this question for hints about prefix file best practices.
extern
) but should be defined in a single source file. –
Rosabelle What solved it for me was changing #import <CocoaLumberjack/CocoaLumberjack.h>
to @import CocoaLumberjack;
, when using Xcode 8.0 for an Objective-C project.
Droppy’s post is correct and I recommend doing that, but I would like to address the question directly. There is a flaw in your code that may be resulting in the error.
LOG_LEVEL_VERBOSE
is defined in DDLog.h
. Your header file only imports DDLog.h
if __OBJC__
is defined, but uses LOG_LEVEL_VERBOSE
without this condition. Therefore if __OBJC__
is not defined, LOG_LEVEL_VERBOSE
will be undefined.
Why would __OBJC__
not be defined? The prefix header is prepended to C, C++, Objective-C and Objective-C++ files. Since __OBJC__
is only defined for the latter two, if there are any C or C++ files in your project then the error will occur.
Knowing this, it is clear the ddLogLevel
definition should be inside the #ifdef __OBJC__
check. However, you should do what Droppy said, and also make sure all Objective-C imports go inside the check.
For people who use "CocoaLumberjack 2.X" and still facing same issue after pod update, please try to import "DDLegacyMacros.h".
For prefix file users, try something like this :
#ifdef __OBJC__
...
...
#import <DDTTYLogger.h>
#import <DDLog.h>
#import <DDLegacyMacros.h>
#endif
Hope this helps someone else.
© 2022 - 2024 — McMap. All rights reserved.