Including one Xcode project in another -- linker errors - xcode

I am trying to do this, and running into problems. The parent project needs to access the class SettingsViewController from the child project. I have put the child project path into my header search paths. Everything compiles OK, but I get linker errors, as follows:
Undefined symbols: "_OBJC_METACLASS_$_SettingsViewController",
referenced from:
_OBJC_METACLASS_$_StatisticsViewController in StatisticsViewController.o "_OBJC_CLASS_$_SettingsViewController",
referenced from:
objc-class-ref-to-SettingsViewController in SelectionViewController.o
_OBJC_CLASS_$_StatisticsViewController in StatisticsViewController.o ld: symbol(s) not found collect2: ld
returned 1 exit status
How can I fix this?

I assume the child project is a static library. Currently, your parent project knows how to find the header files of the child project (otherwise it wouldn't compile), but it doesn't know that it has to link to the library (.a) file of the child project.
You should probably add the library file to Targets > {your app} > Link Binary with Libraries. Furthermore, you probably need to add the linker flags -ObjC and possibly -all_load.
There are many detailed descriptions on the net, e.g. Build iPhone static library with Xcode.
Update:
If it's not a static library, then it's a rather strange project setup. The best thing you can do is to add the shared files (.h and .m) to both projects. They will then be independently compiled in both projects. That should work if you have few shared files.
But I recommend anyway to use a project setup with a static library. It nicely works if you properly set it up. I'm successfully using it. And - as I've told before - there a several good descriptions on the net how to set it up.

You may be missing a framework. Can't really tell from what you have posted here though.

I know this is very old but might be helpful for others.
You need to setup the included project in Target Dependencies in "Build Phases" to get the included projet to be compiled and you also should add the static library of the included project in the "Link Binary with Libraries".
Click on your Target->Build Phases and set these up.

Undefined symbols
Undefined symbols is a linker error
1.Check if you have added a library or framework
Project editor -> select a target -> General -> Frameworks, Libraries, and Embedded Content(Linked Frameworks and Libraries)
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries
2.If you try to use Swift code from Objective-C
Add empty .swift file to the Objective-C project. When Xcode ask press Create Bridging Header

Related

XCode build fail: C++ lib within iOS lib within (test) consumer app

I've got three layers:
- core C++ engine
- iOS audio wrapper
- demo iOS consumer project
So I am:
1) compiling engine.a, which links against accelerate framework.
2) compiling wrapper.a, with a dependency on engine and linking against engine.a AND accelerate framework.
So far so good. I can build wrapper.a. But something looks wrong. My wrapper code is using CoreAudio calls. It is fetching real-time microphone data. It should be reporting errors, surely? It should be requiring me to link AudioToolbox or AudioUnit frameworks.
So I don't see why that library even compiles.
3) create a fresh iOS project that links against wrapper.a.
Now I'm getting a 30+ build errors:
```
Undefined symbols for architecture x86_64:
"vtable for std::exception", referenced from:
std::__1::bad_function_call::bad_function_call() in libfftDecoder.a(FFTDecoder.o)
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"std::__1::__thread_struct::__thread_struct()", referenced from:
std::__1::thread::thread(void (DecoderThread::&&)(), DecoderThread&&) in libfftDecoder.a(FFTDecoder.o)
"std::__1::__throw_system_error(int, char const*)", referenced from:
std::__1::thread::thread(void (DecoderThread::&&)(), DecoderThread&&) in libfftDecoder.a(FFTDecoder.o)
:
```
Can anyone suggest what might be the problem? That first error kind of looks like 'failing to process a C++ construct'.
I should mention that Xcode is very disappointing in the context of this task. The items I link against in the 'build phases' tab are not consistently reflected in the project navigator's "frameworks" folder for the respective project. Also sometimes a .a appears red even when it built successfully.
First point (thanks #wiliz on #iphonedev Freenode) is that a static library has no concept of linking against something. So I should be removing all the links from my .a files and putting all of the dependencies in my consumer app.
The problem was that the library contains compiled C++ code. And the consumer app doesn't have any .mm files. So it isn't using ObjC++ anywhere. So it isn't linking against the C++ stdlib.
Simply renaming one of the .m files (say ViewController.m) to .mm fixes the problem.

Find unresolved external symbol using similar project that builds fine

I have two C projects that pull in the same library.
One project compiles and links fine, the other gets an "unresolved external reference" linker error for a symbol referenced inside a function which both projects call from the same static library.
As far as I can tell, all the linker and code generation properities of importance are equal between the two.
Is there a way to use the working project, to figure out where the linker in THAT project finds the symbol? I've been using trial and error, including more and more of the libraries from one project into the other with no success.
I found what I wanted! In the Project Properties, under Linker->Debugging, there is an option named "Generate Map File". This can be done on the command-line with /MAP.
The generated map file gets the same name as the project (by default) with .map as file extension. It is a text file containing, among other things, the library name where each symbol is defined.
Using the map file for my project that builds, I was able to quickly find the definition of the symbol missing from my broken project.

Do I need to link libs that dependencies link?

I'm trying to write a simple game engine for iOS in Objective C and C++ using Xcode.
I've made a game project and a game engine project. The latter is added to the former as a subproject. The engine is also added as a target dependency and as a binary to be linked in the game project.
My engine uses CADisplayLink so I add QuartzCore.framework in the engine project's "Link binary with libraries list" (found in Build phases).
Now, when I try to build my game project (the project with the subproject), I get this error:
Undefined symbols for architecture i386: "_OBJC_CLASS_$_CADisplayLink", referenced from: objc-class-ref in libVoya-iOS.a
This error only happens when building from the game project - doing it from the engine project works fine. If I add QuartzCore.framework to the game project building works fine.
Can it really be true I have to specifically require frameworks that one of my target dependencies already have required? In this case: My engine (sub project) already links QuartzCore - is it really necessary to also do this in the projects using this engine? It feels like double work for no reason.
Or perhaps I've just completely misunderstood something? :)
Add QuartzCore framework to fix this issue.
I've now found the answer to my question and I'd like to share it.
Static libraries are nothing more than a grouping of the compiled versions of the library's source files. They do not include any libraries they themselves might depend on.
Fusing all dependencies together happens only when you build the actual executable in the very end. For this reason, your application should indeed link against your dependencies' dependencies.
As for the example in my question, that means that my game should link QuartzCore while my game engine should not (even though it is my GameEngine who is using it).
Learn more here:
Duplicate symbol: Include static lib A in static lib B, also include lib A and B in XCode Project
Linking static libraries, that share another static library
I believe this is because you're using a target dependency instead of linking against a precompiled library.

XCode not finding statically linked library

Ok so I have an app that I'm writing that uses a library that someone at work developed. I've included the xcode project in my project and included the output target of that project in my project's target as a framework. I also included the .a file in my target under the "Link Binrary with Libraries" folder. I also have put in the configuration window the header and library search paths.
The problem is that when I try and build the project, it says that symbols are not defined in my project and xcode won't build it. I'm not sure what I'm doing wrong, or if there is something that I'm missing.
btw... The library builds just fine on its own.
Thanks,
Robbie
Check these things...
1- In the target you want to link the lib... Right click -> Get Info -> General Tab, and make sure that the lib is listed as a direct dependency.
2- Select the .xcodeproj in your groups and files window and make sure that you have the library product (should end in .a) checked.

Problem with static library in C++

I'm trying to use a static library created by me in Visual C++ 2005 (unmanaged C++). I declare one function "int myF(int a);" into a .h file, I implement it in a .cpp file, I compile it - the .lib file is produced.
I create a new project (a separate solution) in VC++ 2005 (also native C++), I add the paths for the include file and the lib file; when I invoke the function myF the linker reports an error: "error LNK2019: unresolved external symbol _myF referenced in function _main". if I create the client project in the same solution as the library project and then add a reference to the library projects, it works, but I'm not going to implement everything like this, but rather to add external libraries to my projects...
What is wrong?
Thank you.
You need to also include the actual .lib file in your 2nd project (not just the path to it).
There should be an option in the linker settings to do this.
It is not sufficient to list the folder in which MyStatic.lib can be found. You have to explicitly tell the linker that Dependant.vcproj is using MyStatic.lib.
In VS2005 you do this by project properties->Linker->Input->Additional Dependencies. You can also sprinkle some preprosessor stuff in the .h file to tell the compiler to tell the linker to use MyStatic.lib.
Edit:
The preprocessor magic goes like this
#pragma comment(lib, "MyStatic.lib")
(EDIT: This was a response to the question of getting the /NODEFAULTLIB error in link phase which has now been deleted... shrug)
You are mixing compiler settings if your are getting the defaultlib error. For example, if you build your library in debug and the build your main in release, you will get this error since they are built to use different versions of the CRTL. This can also happen if you use different settings for linking with the C Runtime as a object library or as a DLL. (See the C/C++ options, the "Code Generation" section, under the "Runtime Library" setting)
In many projects there isn't much you can do if you can't correct the settings of the library (for example, 3rd party libraries). In those cases you have to use the /NODEFAULTLIB switch which is a linker option in the "Input" section called "Ignore Specific Library".
But since you are in control of both the main and the library, build a debug and a release version of your LIB file or make sure your "C/C++;Code Generation;Runtime Library" settings match in both projects.
Try setting additional dependencies in the linker input for a project properties.

Resources