1

First I am using Xcode 3.2.5 on a MAC OS X 10.6

I have an Cocoa Application project that builds and runs fine. I have some functions in this project that I am exporting out using:

#define CORE_EXPORT __attribute__ ((visibility ("default")))

extern "C" {
    CORE_EXPORT IUIEventHandler* GetIUIEventHandler();  
}

If I want another dynamic library project to be able to link to my original Cocoa Application so it can use the exported GetIUIEventHandler function, what do I need to do?

Do I need to build the original Cocoa Application also as a Dynamic Library also? Or is there a way to link the executable of the Cocoa Application?

Michael Wildermuth
  • 5,762
  • 3
  • 29
  • 48

2 Answers2

0

A single Xcode project is composed of several targets. Each target produces a product. A product is something like an application or a shared library.

Each file in a project can belong to zero or more targets, but typically belongs to one.

I would re-organize your project to have two targets, one would be a shared library or framework, and the other would be a application.

You'll probably wind up with a lot of your generic code in the framework, and only very application specific concepts in the app. This usually has the side-effect of clarifying your code, and helping maintain some good abstractions. Many projects are structured this way, even when the framework is only used by one client, so that they can get the benefits of the cleaner design that it guides you towards.

Jon Hess
  • 14,237
  • 1
  • 47
  • 51
  • So if I add a BSD Dynamic Library target to my original Cocoas application and drag the same code files into it, I can have a library that I can link to other libraries but access the GetIUIEventHandler() function from the original Cocoas application? GetIUIEventHandler() returns a global that is created in the original Cocoas Application. Or are you suggesting that I place the Exported function into a separate library that the original Cocoa Application and other library projects link too? – Michael Wildermuth Jul 27 '11 at 17:14
  • I'm not sure I understand your question, but I would expect each function to either live in the library or the app, and not both. If you need the application to create the UIEventHandler, but want the library to access it, you probably need to do something like provide a SetUIEventHandler() and have the application invoke that early on. – Jon Hess Jul 27 '11 at 19:05
  • Part of the problem is that this was originally a windows project, which when you build on windows it builds a matching .exe and .lib. An Xcode project doesn't do that, but I still need to have my GetIUIEventHandler function which returns a pointer to a global object from within the app to be used by another library. This library will be loaded by another library that is loaded by the app. I need the library that wants to use GetIUIEventHandler have a dependency on the app executable? Maybe this isn't possible to match what windows does exactly? Thanks for any help I'm new to Xcod. – Michael Wildermuth Jul 27 '11 at 21:00
  • It sounds like you should have GetUIEventHandler() and SetUIEventHandler() in your first library, call SetUIEventHandler() from your app, and call GetUIEventHandler() from your second library. – Jon Hess Jul 27 '11 at 22:12
  • That is one solution, but maybe this will help figure out my original problem. The linker option on Windows is /IMPLIB for Import Library, is there a way to duplicate this in Xcode on a executable – Michael Wildermuth Jul 27 '11 at 23:32
  • I know this is a purely technical/implementation question, but I think you'll have a hard time learning to make a Mac app that works the way users expect if you try to make it work exactly like your Windows version. When working on one platform you should learn the ways of that system and embrace them. The right way to make a library and an app with Xcode is to use two targets. – Jon Hess Jul 27 '11 at 23:48
0

This answer is kind of dirty but it works. I used the linker flag -undefined dynamic_lookup on my library that needs to import the functions from the main app executable, and I don't link in any library that holds the functions that need to be imported.

This forces the linker to not worry undefined symbols and to mark them as should link at load time. When the app loads my library that was built with -undefined dynamic_lookup it will link up the undefined symbols itself.

This is where I found my answer: http://lists.apple.com/archives/xcode-users/2008/Dec/msg00002.html

Michael Wildermuth
  • 5,762
  • 3
  • 29
  • 48