Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid "Redefinition" and "Duplicate Protocol" definition errors in Bridging Header

I want to use a objc library and a objc class in a swift class. So I place the following in the Bridging-Header.h:

#import <FooLibrary/FooLibrary.h>
#import "FooClass.h"

The problem is that the FooClass.h has

#import "FooLibrary.h".

So when I compile I get hundreds of errors like:"Redefinition of enumerator" and "Property has previous definition" and "Duplicate protocol definition" and "Typedef redefinition"

How do I avoid this? It seems like this is just a stupid mental block I am having but I can't get past it so here I am asking.

PartiallyFinite suggested I watch for #include

I did a project wide search and I and not using it at all. There are a few in the library. I chose one of the errors. The file the decoration is in is never included in any file with #include

like image 295
Lucas Goossen Avatar asked Oct 06 '14 12:10

Lucas Goossen


3 Answers

Sounds like something is causing the preprocessor to believe that the FooLibrary.h header imported indirectly by the second #import is somehow not the same file as the one you include just above it. My best guess as to what is that your first, framework-style import is referencing header files that are copied to a build location during build, while your second, direct file import is referencing the header file as it is in your project directory, meaning that the preprocessor sees them as two completely separate files, resulting in it being imported twice.

Proposed ways to fix:

  1. If you can include FooClass.h using the framework-style import syntax (like #import <FooLibrary/FooClass.h>), that will probably fix the problem.

  2. If you're absolutely sure that FooClass.h will always include FooLibrary.h, you can probably just omit the first import entirely, as everything will get imported indirectly via the second one.

  3. Otherwise, you can try some nice, old-fashioned include guards (assuming you have write access to the library headers):

    // FooLibrary.h
    
    #pragma once // Maybe even throw in one of these for good measure;
                 // since we're dealing with an obscure-sounding bug,
                 // may as well try to fix it in all of the possible ways
    
    #ifndef FOOLIBRARY_IMPORTED
    #define FOOLIBRARY_IMPORTED
    ... // actual file contents
    #endif
    

    This will define a preprocessor macro the first time the file is imported, so the second time the preprocessor tries to import the file, the already defined macro will prevent the contents from being imported a second time. I fail to understand why the #import isn't doing this in your case, as that's literally its only purpose and advantage over #include, but if this fixes it, ¯\_(ツ)_/¯

like image 192
Greg Avatar answered Oct 03 '22 06:10

Greg


This can also be caused by cocoapods - try upgrading or downgrading to a different version and re-running pod install

like image 26
Jacek Lampart Avatar answered Oct 03 '22 06:10

Jacek Lampart


For me it was happening when I upgraded Xcode. All I did was to Clean Build folder and run again and it worked!

like image 36
Kirandeep Kaur Avatar answered Oct 03 '22 05:10

Kirandeep Kaur