I have created a new cocoa framework in Xcode, removed all the libraries and files it includes at the beginning except the supporting files.
I have 2 files:
add.h
#ifndef add_add_h
#define add_add_h
void add(void);
#endif
and
add.c
#include <stdio.h>
#include "add.h"
void add(void)
{
printf("adfding");
}
in build phases I add add.c to compile sources and add.h to compile headers public. The project build without a problem but in the framework there is no dylib file and when I drag and drop the framework to another project it says that dylib file could not be found.
dyld: Library not loaded: @rpath/add.framework/Versions/A/add
Referenced from: /Users/vjoukov/Desktop/Projects/test/build/Debug/test.app/Contents/MacOS/test
Reason: image not found
How can I make a simple framework and keep dylib files inside it ?
I think you're misunderstanding the error message.
A
.framework
works as a dynamic library, but there won't be any Mach-O loadable object file with an actual.dylib
filename extension inside the .framework folder.There are a couple of reasons you might be getting that error message from
dyld
, the dynamic link library loader, at runtime. The first is that you forgot to copy the .frameworks into the built application bundle during the build process. While they can be copied to about any location inside the app bundle, the traditional place is in AppName.app/Contents/Frameworks/. If you haven't done so already, choose Project > New Build Phase > New Copy Files Build Phase. Change the Destination popup to Frameworks like in the image below.You'll then drag the icon of the framework into the folder so that it's copied during the build process.
The second and more likely reason the framework can't be found at runtime is that you haven't specified any runpath search paths for your main executable. (This is needed, because, as we saw from your error message, your framework was built using the newer
@rpath/
style install name (@rpath/add.framework/Versions/A/add
) rather than the older@executable_path/
or@loader_path/
styles).Provided you copy the custom frameworks to the location mentioned above, you'd add a runpath search path entry of
@loader_path/../Frameworks
, like shown in the image below:The following excerpt that explains how dynamic libraries are found at runtime is from the manpage of
dyld
: