Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a dynamic shared library in a C++ xcode project?

Tags:

c++

xcode

dylib

I have created a new project in XCode (using the template "STL C++ library"), and have made a little test function void hello(); that prints to the console.

It compiles fine, and I am able to export a dylib file.

What I can't figure out is how to create a project that then links to this file and can call the hello() method.

Any advice would be greatly appreciated.

Alex.

like image 365
Alexander Rafferty Avatar asked May 14 '12 11:05

Alexander Rafferty


1 Answers

Create another Xcode project, such as an application project. In your example you could create a command-line tool project that uses C++.

After creating the project add the dylib to the target's Link Binary with Libraries build phase. In Xcode 4, select the project from the project navigator to open the project editor. Select the target from the target list on the left side of the project editor. Click the Build Phases button at the top of the editor. Click the disclosure triangle next to the Link Binary with Libraries build phase. Click the + button to add your library.

When you finish adding the dylib to your project, include the header file for your library in your code, and you should be able to call the hello() function. You may also need to add a search path to your library to the Library Search Paths build setting to use your library.

like image 151
Swift Dev Journal Avatar answered Sep 22 '22 21:09

Swift Dev Journal