15

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?

enter image description here

oguz ismail
  • 1
  • 16
  • 47
  • 69
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

4 Answers4

8

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.

Community
  • 1
  • 1
Droppy
  • 9,691
  • 1
  • 20
  • 27
6

What solved it for me was changing #import <CocoaLumberjack/CocoaLumberjack.h> to @import CocoaLumberjack;, when using Xcode 8.0 for an Objective-C project.

mllm
  • 17,068
  • 15
  • 53
  • 64
2

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.

Douglas Hill
  • 1,537
  • 10
  • 14
1

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.

MGY
  • 7,245
  • 5
  • 41
  • 74
  • I imported CocoaLumberjack as framework and I' getting this error: "use of undeclared identifier ddloglevel". I add it #import "DDLegacyMacros.h" but I'm getting this error "DDLegacyMacros.h file not found" – user2924482 Apr 10 '17 at 17:08