Adding library to Xcode project - xcode

I'd like to add the libevent library to my Xcode project. I want to include it in the executable, because libevent isn't installed by default on Mac OS X.
I can compile the library from source using ./configure && make. I expected to find a .a library file, but there isn't. What do I do then? What files are relevant and how do I add them to Xcode?
Sorry for this very basic question, but I don't even know where to start.

First off, let's find out if your library truly got built. In the terminal, type in cd / and then find . -name libevent\* -print and see if the path for your libevent.a file actually appears.
If you can't find it, try running sudo make install from the top level of the library source code and then the library may end up getting installed in /usr/local/lib or some other appropriate place.
Once you do find the library, you can drag & drop it into your Project's list of Files in Xcode. Or you can include -levent in the link settings for your project.

Note that Xcode has this nasty "feature" that if it sees both dynamic and static versions of a library, it will always link against the dynamic one, even you specify the static one (.a) in your project. There's no way to override this "feafure" and you have to move or delete the dynamic one out of the library search paths.

Related

Linking Mac Frameworks using Premake and GNU Make

I have a "cross platform" application that uses two code repositories at the moment, maintained relatively independently, and built with VS / Xcode depending on the target platform (win or mac respectively). I fell in love with Premake after using it on a few previous projects and am trying to pull all of my code for this application together into a single cross-compilable codebase.
I don't want to rely on Xcode, and instead want any developer to be able to build on Mac using either Xcode or gmake. I have a non-standard framework that I want to link to and include in the repository (it won't be located in /Library/Frameworks or any of the default mac framework search paths). I've added the framework file in a directory in my project /lib/TheFramework.framework. My premake file contains the following under the project definition:
includedirs {".", "lib", "lib/TheFramework.framework/Headers"}
libdirs {"lib"}
links {"TheFramework.framework"}
When I compile, (running $ premake5 gmake and then $ make), I get a header file not found error. Is there something wrong with my search paths? Am I missing a path or a flag somewhere?
Thanks!
Before looking at what you need to do with premake, let's first look at what needs to happen under the hood.
When compiling a mac program with a non-standard framework on gcc or clang (which is what your resulting make file does) it is necessary to do two things:
Specify the name of the framework, via -framework TheFramework - This is what premake does when you provide it with links {"TheFramework.framework"
Specify the location of the framework, via -F /Path/To/Framework/ - This is currently not being handled automatically by premake.
I've made a simple test c program that uses the SDL2 framework and compiled it with gcc: https://gist.github.com/JohannesMP/6ff3463482ebbdc82c2e - notice how when I leave off the -F /... flag I get an error that is probably similar to what you described.
So what is happening is, although you are providing premake with the include dir, premake will not add that the proper -F flag.
One way around this is to do the following:
configuration {"macosx", "gmake"}
buildoptions {"-F /Path/To/Framework"}
linkoptions {"-F /Path/To/Framework"}
(See here for an example project: https://gist.github.com/JohannesMP/9a9b5263c127103f1861#file-premake5-lua-L24-L26 )
In premake5 this will blindly append the code provided to both the build step as well as the link step. It is necessary to do it both for build as well as link.
Just keep in mind that, because premake doesn't process or check the build/link options for being valid, a user will receive an error if the provided path doesn't exist on their machine. For example while you might have a framework in your user-specific directory ~/Library/Frameworks, since that folder doesn't exist by default another user might be using the global /Library/Frameworks instead, and when they try to compile your premake project with gmake they will get a warning:
ld: warning: directory not found for option '-F/Users/<NAME>/Library/Frameworks'
At this point, it seems that there is no 'safe' way to get premake5 to try to include the framework path, but that may change in the future.
Check out this issue I posted on the premake repo: https://github.com/premake/premake-core/issues/196

Python C Extension and Xcode 4.5

I would like to create a python C extension using XCode 4.5.2 so that I can use the xcode debugger. The extensions require the C file to be linked as a .so file. I have not been able to make .so file in xcode, only dylib files.
I tried as follows:
1. Used the C/C++ Library template, specified dynamic library
2. Change the Mach-O type to Bundle
3. Still could not make an .so so I changed the wrapper extension to .so, with no luck.
The post link below seems to say that I can just manually change the extension. This did not work for me, but I could have made another mistake.
how to make python load dylib on osx
Any thoughts on my problem?
.so files serve the same purpose on Linux (and some other Unixes) that .dylib files on OS X - they're dynamically linked libraries. If everything else fails, and Python cannot recognize your platform (and the fact that on OS X, it should look for .dylib files instead), make a symbolic link from the .dylib file with the same name but with the extension .so.
(Yes, this is a quite ugly hack, but it's simple, straightforward and it works.)

How Can I import and compile my code with gtk.h in Mac OS Xcode?

I have downloaded and installed Gtk2 with MacPorts:
sudo port install gtk2
And I have made sure that the system configuration with this too:
pkg-config --cflags gtk+-2.0
But when it comes the time that I put down this line in Xcode:
#include <gtk/gtk.h>
I still get the following error msg:
Gtk/gtk.h: No such file or directory
What am I missing here? Any configuration in XCode or something?
Thanks
You need to tell your project where gtk lives.
Best way to do this is to go into your project's "Build Settings" and look for the setting named "Header Search Paths". Add a direct path to your Gtk's include directory there. I suggest just a direct path like /usr/local/lib/gtk or whatever the true location of it is (and don't use fancy aliases or relative paths or things like this just yet, until you get really accustomed to them).
Then, make sure the framework (.framework, .dylib) or static library (a .a lib file) or whatever is included in your project so the linker knows which Gtk library to link against.
I also noticed a potentially useful tutorial here.

Xcode, building and dylibs

I've looked at a few related questions and cannot seem to find a solution for myself.
Basically I'm using the libmp3lame.dylib in my Xcode project. The install process for lame produced the .dylib and placed it in usr/local/lib and to get Xcode to build and run the project I changed the Library Search Paths to include the above folder. This works fine.
Now it's come to producing a release version and I want to include the .dylib in the bundle so that the user doesn't have to put up with an install phase or anything similar. I created a copy files phase of the target and this dumps the .dylib into the Frameworks folder in the contents of the bundle. However, running otool on the binary shows that instead of using the .dylib inside the bundle, the compiler has linked it to the usr/local/lib version (even if I delete that version).
Looking at the other results in the otool output I have other frameworks being linked to inside the bundle, just not the .dylib. Looking at the properties for both these frameworks and the .dylib in Xcode I can't see any differences other than file type.
My searching suggests I need to use rpath or similar, but I've no clue where to apply this in Xcode.
My workaround for this issue was to do the following in terminal (I've used Automator to, well, automate this):
install_name_tool -id #executable_path/../Frameworks/libmp3lame.0.0.0.dylib ~/path/to/lib/in/app/libmp3lame.0.0.0.dylib
install_name_tool -change /usr/local/lib/libmp3lame.0.dylib #executable_path/../Frameworks/libmp3lame.0.0.0.dylib ~/path/to/app/Contents/MacOS/AppName
Basically swapping the path to the library from the one in the usr local lib to the one included in the bundle.
My tip is to look at the build output, how does it actually run ld?

Help on installing a library like libical into Xcode

I would like to use the libical library in my project, but I have never used an external library before. I have downloaded the libical files, but I am pretty much stuck there. I do not how how, or even if, I need to build/extract them and then how to get them into Xcode. Any help would be greatly appreciated. Thank you.
If this a pre-built library then you can just drag it into your Xcode project (or use Project => Add to Project…) in the same way that you would for source/header files.
If it's not pre-built then you'll need to build it for whatever environments and architecture you want to target. If it comes with an Xcode project then this is easy. If it's just the usual open source type of distribution then you usually do something like this:
$ ./configure
$ ./make
$ sudo ./make install
That will typically put the built library(ies) and header(s) into somewhere like /usr/local/lib and /usr/local/include. In your main Xcode project you can then just add these header(s) and library(ies) to your project.
Note that if you're cross-compiling, e.g. for iPhone, then you'll need to add some flags to the ./configure command so that you target the correct architecture, e.g. ./configure -build=arm-apple-darwin9.0.0d1.
Note also that it's usually a good idea to check MacPorts to see if they have already fixed up a given open source project for Mac OS X - this can save you a lot of work.
See also this blog about building and using libical on iPhone.
Getting libical to configure and build for arm is more tricky then ./configure -build=arm-apple-darwin.
See this question and answer for more details: Compiling libical

Resources