Xcode dylib constructor destructor

0

Posted by Jaime | Posted in Coding | Posted on 26-02-2015

Tags: ,

I meant to post this link a while ago, but here it is anyway:
TP40002013-SW17

So, in a dylib, you can have code execute when the dylib is loaded and execute code when the dylib is being unloaded:

__attribute__((constructor))
static void initializer1() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
}
 
__attribute__((constructor))
static void initializer2() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
} 
__attribute__((destructor))
static void finalizer1() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
}
 
__attribute__((destructor))
static void finalizer2() {
    printf("[%s] [%s]\n", __FILE__, __FUNCTION__);
}

C++ tips and tricks for MacOS X

0

Posted by Jaime | Posted in Coding | Posted on 12-07-2011

Tags: , , , , ,

Xcode 4

I had to do some fancy debugging of a C++ dylib and while I was searching around the internet, I came accross this article from Apple: C++ features in MacOS X.

I also found two cool articles that describe well how the dlopen application works and what happens to your symbols when an application bundle loads your dylibs:

Dynamic Linking of Imported Functions in Mach-O

Redirection of Imported Functions in Mach-O

I also found a cool programs that shows you what the load might look like in a particular dylib. This is a very useful tool:

MachOView

Related posts
MacDependency

MacDependency

3

Posted by Jaime | Posted in Coding | Posted on 20-04-2011

Tags: , ,

I found this useful tool, thanks to my coworker, that is an alternative to using the otool tool: MacDependency

How do I know if a method is available?

0

Posted by Jaime | Posted in Coding | Posted on 03-12-2010

Tags: , , , ,

This may seem like a silly post, but I have been talking to PC developers working on Mac OS code who are creating code like they are still working on the PC. So this is a note for them; things they should keep in the back of their mind when they are coding.

Mac OS SDKs
When using different SDK’s in Mac OS Programming, there are ways for checking what SDK you have available.

At build time, you can use the macros provided by Apple to check what’s available:

#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
#endif

Checking for functionality
At runtime, if you are crossing library boundaries and calling a library that you didn’t build, you should check that the class you are sending a message to responds to that message by calling the respondsToSelector method. You can at that point decide what path to take, which is better then calling the method and proceeding with your code as if the previous call succeeded:

- (void)hornMethod
{
    if ( [m_windClass respondsToSelector:@selector(blowThatHorn)] )
    {
        [m_windClass blowThatHorn];
    }
}

Changing library dependency paths

0

Posted by Jaime | Posted in Coding | Posted on 01-12-2010

Tags: , ,

If you want to change what your host application is dependent on, without having to rebuild it, you can use the install_name_tool app for changing this:

install_name_tool -change oldreference newreference appname

install_name_tool -change /usr/lib/libItsAllAboutMe.dylib @executable_path/../Frameworks/libItsAllAboutMe.dylib MySuperApp

This is pretty useful for cases where you might have a dynamic library located in a static location, but you want to bundle it with your application instead of having to use PackageMaker to install the library on the users local filesystem.

2016-10-26 Update
When I wrote this posting, I had not yet worked with @rpath, which is what you should be using in your Xcode project.

There is a good article that illustrates how to do this at dribin.org

And, if the library you are linking against does not use @rpath like …

$otool -L libSomeWeakLookingLib.dylib
libSomeWeakLookingLib.dylib:
/Users/weaklibnightly/build_bot/slave/mac-64-trunk/weaklibbuild/bin/libSomeWeakLookingLib.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)

Then you can change that long path to a @rpath using
$install_name_tool -id /Users/weaklibnightly/build_bot/slave/mac-64-trunk/weaklibbuild/bin/libSomeWeakLookingLib.dylib @rpath/libSomeWeakLookingLib.dylib ./libSomeWeakLookingLib.dylib

Xcode and static libs

0

Posted by Jaime | Posted in Coding, Not-so-funny | Posted on 19-10-2009

Tags: , , , , ,

I’m writing this blog entry just in case other people come across the same problem and can’t seem to figure out what’s going on.

I found it in Xcode 2.5 that hasn’t been fixed in the latest version of Xcode that I have, version 3.1.3.

The bug I’m referring to is when you create a target as a static lib output, then you change the target later to output a dylib. The problem is that everything looks fine… heck, it even puts a dylib in the file name extension, but the file is actually a static library.

You have to go to Terminal.app and use the file command to see that the file is in fact, a static lib and not a dylib. You may not notice the problem at first until you go to debug the project and notice some weirdness.

Hopefully, you haven’t gotten bit by this problem, but if you have, the only solution I found was to create a new target from scratch.

Dumping symbols from a library

0

Posted by Jaime | Posted in Coding | Posted on 04-09-2008

Tags: , , , ,

When you need to dump symbols from a dylib on Mac OS or a DLL on Windows, you can use the following commands to do so:

Mac OS

nm -m mySpecialLibrary.dylib

Windows

dumpbin /exports mySpecialLibrary.dll

Now sometimes, you have have a lot of symbols in one library, or you may want to find one particular symbol. On the Mac, that’s pretty simple to do. Just use the grep command to find the symbol you are looking for:

nm -m mySpecialLibrary.dylib | grep someSpecialFunction

On the PC, you can dump your symbols to a file so that you can use a text editor like Notepad++ to search for symbols:

dumpbin /exports mySpecialLibrary.dll > c:\MyProjectFolder\mySpecialLibrarySymbols.txt