Linking Dynamic Libraries on XCode

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Linking Dynamic Libraries on XCode

Post by ajtgarber »

How do you do this?

So far I've added the .dylib to the project, It showed up under the "Link Binary With Libraries" section under my target. I try to compile the program, it acts like it doesn't exist.
The compile errors basically say that the header from the library doesn't exist.

In case this helps either now or later, the library only contains two files: a header that defines a class (Game) under a namespace called FBEngine, and a cpp that implements the methods found in Game.
So:
Game.h
Game.cpp
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Linking Dynamic Libraries on XCode

Post by Ginto8 »

1 thing: Libraries don't include headers. Headers are just text that get included in source files.
What you need to do is take the header file and place it in your compiler's include directory. Once you do that, it should work.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Linking Dynamic Libraries on XCode

Post by ajtgarber »

Ahh, thank you!

The errors from before are gone but I have some new ones :D.
Now its saying that 3 of the methods are inaccessible, but I don't really see any difference between them and
any of the other methods.

Game.h: http://pastebin.com/FBGZezYa
Game.cpp: http://pastebin.com/0FcwMRH4
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Linking Dynamic Libraries on XCode

Post by Falco Girgis »

Hah, welcome to the most UN user friendly IDE ever.

It would seem as though dragging and dropping your dylib into the project would cause the IDE to allow your application to link against it, right? WRONG! That would be too convenient, and XCode is too much of a pain in the ass.

You STILL have to go to your project->Build Settings and specify the path for the directory location of both your headers AND your library. Why the fuck can't XCode do that itself (like any other IDE worth anything), you ask? The world will never know.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Linking Dynamic Libraries on XCode

Post by N64vSNES »

GyroVorbis wrote:Hah, welcome to the most UN user friendly IDE ever.

It would seem as though dragging and dropping your dylib into the project would cause the IDE to allow your application to link against it, right? WRONG! That would be too convenient, and XCode is too much of a pain in the ass.

You STILL have to go to your project->Build Settings and specify the path for the directory location of both your headers AND your library. Why the fuck can't XCode do that itself (like any other IDE worth anything), you ask? The world will never know.
I speak a bit of Falco, Here is the translation
GyroVorbis wrote: Xcode sucks.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Linking Dynamic Libraries on XCode

Post by ajtgarber »

Lol nice. The combined unfriendliness of XCode and the fact that I'm horrible with the c++ linker anyway :).

I've added my paths to the header search locations and the library search locations, seems as though I've lost a few errors but gained some more :p.
It can find the header file (thank you gods of programming), but its still saying that the methods are inaccessible.
Messages being:
/Users/ajtgarber/Desktop/Game.h:22:0 /Users/ajtgarber/Desktop/Game.h:22: error: 'void FBEngine::Game::requestFPS(unsigned int)' is inaccessible
/Users/ajtgarber/Desktop/Untitled/main.cpp:31:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:31: error: within this context
/Users/ajtgarber/Desktop/Untitled/main.cpp:31:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:31: error: 'FBEngine::Game' is not an accessible base of 'TestGame'
/Users/ajtgarber/Desktop/Game.h:30:0 /Users/ajtgarber/Desktop/Game.h:30: error: 'void FBEngine::Game::setup(int, int, int)' is inaccessible
/Users/ajtgarber/Desktop/Untitled/main.cpp:32:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:32: error: within this context
/Users/ajtgarber/Desktop/Untitled/main.cpp:32:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:32: error: 'FBEngine::Game' is not an accessible base of 'TestGame'
/Users/ajtgarber/Desktop/Game.h:31:0 /Users/ajtgarber/Desktop/Game.h:31: error: 'void FBEngine::Game::start()' is inaccessible
/Users/ajtgarber/Desktop/Untitled/main.cpp:33:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:33: error: within this context
/Users/ajtgarber/Desktop/Untitled/main.cpp:33:0 /Users/ajtgarber/Desktop/Untitled/main.cpp:33: error: 'FBEngine::Game' is not an accessible base of 'TestGame'
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Linking Dynamic Libraries on XCode

Post by avansc »

GyroVorbis wrote:Hah, welcome to the most UN user friendly IDE ever.

It would seem as though dragging and dropping your dylib into the project would cause the IDE to allow your application to link against it, right? WRONG! That would be too convenient, and XCode is too much of a pain in the ass.

You STILL have to go to your project->Build Settings and specify the path for the directory location of both your headers AND your library. Why the fuck can't XCode do that itself (like any other IDE worth anything), you ask? The world will never know.
First of all, just dragging ANY file to the xcode project will let you added it to any of your targets, if you drag a dylib and add it to some executable/target, it will TRY to link it for you, it does not use that path, because that would make no sense in terms of portability. A project has dependancies, and they should be in standard locations. they can be in arbitrary locations, but that your job to take care of that.

In some cases, even setting the path(in xcode) may not work, you have to understand how osx/xcode handles static vs dynamic libraries, and ultimately what you should be using, which is a framework.

This all boils down to just not knowing how dynamic libraries in particular are handled, the apple developer pages have a plethora on this.

btw, windows does this the same with either the local location or system32 or whatever.

"Before you can use a dynamic library as a dependent library, the library and its header files must be installed on your computer. The standard locations for header files are ~/include, /usr/local/include and /usr/include. The standard locations for dynamic libraries are ~/lib, /usr/local/lib, and /usr/lib.

You may also place the .dylib file at a nonstandard location in your file system, but you must add that location to one of these environment variables:

LD_LIBRARY_PATH
DYLD_LIBRARY_PATH
DYLD_FALLBACK_LIBRARY_PATH"

Also, you can look on the Apple pages about "run-path dependent library"

EDIT: you can also add a copy files build phase to your target that you can have it copy the library to the location where you can access it. This method works particularly well if you are building the library at the same time.

Image
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Linking Dynamic Libraries on XCode

Post by ajtgarber »

Okay I've resorted to just dragging the files into the project that needs them, and still it gives me the "method is inaccessible" compile error. What does that even mean?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Linking Dynamic Libraries on XCode

Post by Ginto8 »

It means that you're trying to access non-public methods. If you inherit something and you want its methods exposed, do it like this:

Code: Select all

class derived : public base {};
If you have methods you want available, do this:

Code: Select all

class blarg {
public:
    void publicMethod();
};
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Linking Dynamic Libraries on XCode

Post by avansc »

If you have or you install teamviewer I'll be happy to assist you.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Linking Dynamic Libraries on XCode

Post by ajtgarber »

"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: _SDL_main)
ld: symbol(s) not found
collect2: ld returned 1 exit status
*facepalm*, okay I've changed the code to ": public Game" and now its giving me the _main error. This is probably going to be something stupid (like last time) that I forgot to include isn't it?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Linking Dynamic Libraries on XCode

Post by Ginto8 »

... are you sure this is the mac build? because anything about _SDL_main or the like should only come up in windows :|
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Linking Dynamic Libraries on XCode

Post by ajtgarber »

Yeah.. :P, woohoo another strange error! (well... its strange to me anyway.. :P)

Main.cpp: http://pastebin.com/BrFiQ69g
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Linking Dynamic Libraries on XCode

Post by MrDeathNote »

I hate to resurrect an old topic but being fairly new to mac myself I had the same problem a while ago, turned out you just need to add two files SDLMain.h and SDLMain.m both of these are created by xcode when you make a new SDL project but if your using a different IDE this wont happen. I wouldn't normally post on such an old topic, only someone else may have the same problem and they might find this useful.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply