简体   繁体   中英

Swift Project using objective-c framework

I have a custom framework which is written in objective-c and c++, and now,I try to use the framework in my swift project. I think I configured bridging header file correctly. but still get these errors while compiling the project.

在此处输入图片说明

make sure you use import the framework in your class file if installed via pods. example : import Cloudinary.

For the reason why we actually need a linker flag and we get into this problem is defined below in the part taken from this link :

The Linker

When a C program is compiled, each "source file" is turned into an "object file" that contains executable functions and static data. The linker glues these object files together into a final executable. That executable is eventually bundled into an app by Xcode.

When a source file uses something (like a function) defined in another file, then an undefined symbol is written into the object file, to "stand in" for the missing thing. The linker resolves these symbols by pulling in the object files that include definitions of undefined symbols when building the final executable.

For example, if main.c uses the function foo(), where foo is defined in another file, Bc, then the object file main.o will have an unresolved symbol for foo(), and Bo will include an implementation of foo(). At link time, Bo will be brought into the final executable, so that the code in main.o now references the implementation of foo() defined in Bo

A UNIX static library is just a collection of object files. Normally the linker only pulls in an object file from a static library if doing so would resolve some undefined symbol. Not pulling in all object files reduces the size of the final executable.

Objective-C

The dynamic nature of Objective-C complicates things slightly. Because the code that implements a method is not determined until the method is actually called, Objective-C does not define linker symbols for methods. Linker symbols are only defined for classes.

For example, if main.m includes the code [[FooClass alloc] initWithBar:nil]; then main.o will contain an undefined symbol for FooClass, but no linker symbols for the -initWithBar: method will be in main.o.

Since categories are a collection of methods, using a category's method does not generate an undefined symbol. This means the linker does not know to load an object file defining the category, if the class itself is already defined. This causes the same "selector not recognized" runtime exception you would see for any unimplemented method.

The -ObjC Linker Flag

Passing the -ObjC option to the linker causes it to load all members of static libraries that implement any Objective-C class or category. This will pickup any category method implementations. But it can make the resulting executable larger, and may pickup unnecessary objects. For this reason it is not on by default.

For the same reason, when we have to include the c++ features we have to add the linker flag for that which is -lc++ (refer this url ). (AFAIK, if you exlude c++ features -lc++ flag won't be needed)

A short procedure to avoid linker issues is to add -all_load (see what does this flag do )

These are my findings for this problem, hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM