How do I weak link frameworks on Xcode 4? - objective-c

I need to weak link some framework with my target.
But I can't find how to do it...
If I try to run my project on 3.2 iPad simulator i get the following error:
dyld: Library not loaded: /System/Library/Frameworks/iAd.framework/iAd
Reason : Image not found
Thanks !

Go to your project -> Targets -> Build Phases -> Link Binary with Libraries.
Then change the library you want to weak-link from "Required" to "Optional".

This doesn't work now. Please follow the link below:
https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html
Select the target you want to modify and reveal its build phases.
Expand the Link Binary With Libraries build phase to view the frameworks currently linked by the target.
If the framework you want to weakly link to is listed in the Link Binary With Libraries build phase, select it, and choose Edit > Delete to remove it.
Now you can tell the linker to use weak linking for that framework.
Select the target, open its Info window, and click Build.
To the Other Linker Flags build setting, add the following command-line option specification, where FRAMEWORK_NAME is the name of the framework you want to weakly link to:
-weak_framework FRAMEWORK_NAME
Build your product.

Related

Use libxlsxwriter without CocoaPods in a macOS project

My project (Objective C, macOS, Xcode 11) does not use CocoaPods and I'd like to keep it that way.
I want to use libxlsxwriter to export data in Excel format. I cloned libxlsxwriter from GitHub and built it. Now I have libxlsxwriter.a and lbixlsxwriter.dylib.
What I tried so far: I followed the instructions on this site (thanks for the link) to include the dylib. In the "General" settings lbixlsxwriter.dylib is shown as "Embed & Sign", in "Build Phases" it is listed under "Link Binary With Libraries" and it has its own "Copy Files" section. Running from Xcode gives me
dyld: Library not loaded: /usr/lib/libxlsxwriter.dylib
Referenced from: /Users/XXX/Library/Developer/Xcode/DerivedData/XXXefxsacdsioejobcumpvsbklqguri/Build/Products/Debug/XXX.app/Contents/MacOS/XXX
Reason: image not found
What do I have to add to my project: The include folder with all the header files and both of these libs? How do I have to adjust the General settings and Build Phases?
If you have static library and only one loader, eg. main executable, (so you don't need to share it between your components), then you don't need dynamic library at all.
Just add your libxlsxwriter.a into Target > General > Frameworks and Libraries section, and it will be linked in into your executable and just work.
If you really need shared dynamic library then you need to use rpath + LD_RUNPATH_SEARCH_PATHS combination. (Let me know if you need more details on this).

linking failed when building my own project using LLVM

I'm learning to build a llvm project, this is the reference: http://llvm.org/docs/Projects.html. I use the "llvm/projects/sample" directory as the primary project skeleton, and it works. Then I want to build tools from "llvm/examples" to my project, such as Fibonacci, it can't work. I do it this way: first copy the "llvm/examples/Fabonacci" directory to "MyProj/tools" ("MyProj" is top level of my project) and change Makefile to contain Fabonacci target, then configure & make. but the Fabonacci tool seems can't be built. It depends on some libs when linking. So what can I do if I want to build the source code from "llvm/projects/example" in my project?
You need to provide LLVM libraries to linker when building your own project. This means adding some flags, library directories and libraries themselves to link command. Build script probably needs some editing.
llvm-config tool can be used for providing necessary options to compiler/linker. Check documentation and examples.

Linking error when trying to use self-written Framework in Library of an App

I've build a Framework for an Objective-C App. I've tested it on minimalistic Programs where it worked. I was trying to use it in a real App now. Unfortunately the linker can't find the definition of my classes. =(
When I try to run it, I get the following Error Message:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_ClassInMyFramework", referenced from:
objc-class-ref in libMyLib.a(MyLib.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The Dependency:
App -> libMyLib.a -> MyFramework.framework
The usual suggestion for this error message is to add the framework into the "Link Binary with Library" Build Phase... I can assure you that this has happened ;)
My first thought is that something might be wrong with the Build Settings which results in this linking error.
On second thought It may have to do with the Project-Setup. Is it possible to statically link a framework into an '.a' library file?
Update:
I've linked the Framework into the App and now it's working. But I don't consider this a clean solution. Help still appriciated. =)
The short answer is no, you can't link the framework statically into your .a file. See this discussion.
The reason is, the static library doesn't include the object code (the definition) of classes from the dynamic framework. The static library links to object code in the framework the same way the app links to the framework code: at run-time.
From Apple's Framework Programming Guide: "Dynamic shared libraries have characteristics that set them apart from static linked shared libraries. For static linked shared libraries, the symbols in the library are checked at link time to make sure they exist. If they don’t exist, link errors occur. With dynamic shared libraries, the binding of undefined symbols is delayed until the execution of the program."
It depends on what you want to be able to do with your code. You could add a static "target" for your framework project, so your framework project outputs both a framework and a static library. You could include this static library into apps.
But, one benefit of frameworks is that you can include nibs, images, headers, etc. So, linking your framework into your apps directly is not a bad way to go. Otherwise, you need to include these assets directly into your project. If you want this framework to be distributed with your app, you'll need to package it inside the app wrapper.
It looks like some people create a "static framework" for inclusion into an iOS project, but this looks a bit hacky to me.
As an interesting exercise, you can explore the symbols in your object code. Let's say you are using a ClassInMyFramework (from your framework) somewhere in your static library, like:
ClassInMyFramework *myFramework = [[ClassInMyFramework alloc] init];
The static library will then include the _OBJC_CLASS_$_ClassInMyFramework symbol. You can see the list of symbols in your static library file at the command line:
$ nm /path/to/libMyLib.a
This will output a list of symbols, which will show that _OBJC_CLASS_$_ClassInMyFramework is undefined (note, the "U" designates that the class is undefined):
U _OBJC_CLASS_$_ClassInMyFramework
Whereas, if you were do to the nm command on your framework:
$ nm /path/to/MyFramework.framework/Versions/A/MyFramework
Your output would show that the symbol is defined in your framework (though the definition will still only linked at run-time), which would look something like this, showing an address of the definition:
0000000000001100 S _OBJC_CLASS_$_ClassInMyFramework

Why am I getting "_OBJC_CLASS_$..., referenced from:" linker error when I have correctly linked frameworks?

My Problem
I getting "_OBJC_CLASS_$..., referenced from:" linker error when compiling some Xcode projects (it happens in both iOS and Mac projects) I have correctly linked frameworks and imports.
Setup
One application target
One test target
All frameworks linked correctly
On compile I get the following linker errors:
"_OBJC_CLASS_$_JGCountdownTimer", referenced from:
objc-class-ref in JGCountdownTimerTestCase.o
for many classes that are used in tests.
What I've Tried
Checked that imports are all present
Removed all non standard frameworks
If I compile a class for both the test target and the app target it fixes the issue. But then I get other warnings from the compiler.
I faced a similar problem. I got the linker error:
_OBJC_CLASS_$_MyClass
The problem was that I had declared an #interface for MyClass but had not declared its corresponding #implementation.
The fix was to simply add
#implementation MyClass
#end
Quick Answer
Copy and paste the following line into your build settings:
GCC_SYMBOLS_PRIVATE_EXTERN = NO
In the target build settings look for "Symbols Hidden by Default". For the Debug configuration you want "No".
I've had this problem on and off for many months and I've just discovered why.
Not sure if this could be the problem, but with the new compiler, any obj-c that aren't explicitly referred to/invoked will not be linked from libraries. This causes problems when you implement categories in libraries for example.
Try adding '-ObjC' to 'additional linker flags' in the build settings panel for your target. shrug
objc_class_$textfieldvalidator referenced from
Compile doesn't include those copied files in its compile list. To fix this error
select the file or folder, then go to the Build Phases panel and open the Compile Sources step, then click the + button and add them all file or folder.
Right click on a project folder, click "Add Files to ...", select the file(s), click the Options button and select the target, then click Add.

How to compile specific files in objective-c++ and the rest of the project in objective-c

I'm currently busy on a project where I need to use an external accessory to read Mifare 1k tags.
The accessory was provided with an SDK, written in (Objective ?)C++ and I followed the instructions provided to set XCode to "Compile sources as: Objective-C++" and added "-Obj-C++" in "Other linkers flags.
The SDK compiles fine then, but trouble is I am already using several libraries in the project (such as ASIHTTPRequest, JSONKit, ...) and I get compilation problems because of those new settings in those libraries. If I switch back to the previous settings, I get compilation problems in the reader's SDK
The question is: is there a way to compile only the class from the SDK as C++ and the rest of the project as objective-c ?
Edit: the SDK files consists only of .h (and a linked library)
thanks for your help,
Mike
Select the file you want to compile as Objective C++ from the file navigator, and then select the File Type in the file inspector view. This is in Xcode 4, but there is a similar mechanism in Xcode 3.
Try renaming the files where you are including the library headers to myClass.h for interface and myClass.mm for implementation files. This forces the files to be compiled as objective-c++.
I have resolved this problem:
You should set "According to file type" to "Complile Sources As",
Set "-ObjC++" to the "Other Linker Flags"
The last,you should modify the files's suffix to .mm that reference
the library method
well, in Build phases tab, there is a section Compile sources. For file you want to use Objective-C++ compiler add flag: -xobjective-c++
tested in Xcode 12.5