20

I'm trying to build DLIB for an iOS project. Running the cmake results in a libdlib.a and a load of .o files.

When I add the library to an Xcode project I get warning that the library hasn't been built for arm64.

My question is two-part:

  1. How can I build DLIB for iOS (I tried cmake **path_to_source** -DCMAKE_OSX_ARCHITECTURE="arm64" but it caused loads of errors e.g. unknown type name '__uint32_t'; did you mean '__uint128_t')?
  2. What is the purpose of all the .o files that get built when you run cmake? Do I need to include them in an Xcode project?
Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • Object files (*.o) are just bi-product of the build. And resulting `libdlib.a` is static library You're probably would like to use. What's Your Xcode version? – Kamiccolo Jan 04 '16 at 15:30
  • Im using the latest version 7.2 – Rob Sanders Jan 04 '16 at 15:42
  • The source i have is for linux/OSX so I'm not sure if it's possible to build for iOS? – Rob Sanders Jan 04 '16 at 15:44
  • Yes it is that one [this is the website](http://dlib.net). There are a load of makefiles in the examples folder. [This is the compilation instructions](http://dlib.net/compile.html) – Rob Sanders Jan 06 '16 at 11:57
  • Thanks @ruslo I'll check it out. – Rob Sanders Jan 08 '16 at 09:08
  • Thanks all, I am trying to build DLIB for IOS using CMAKE. I got error "pthread" not found. Can you tell how to build DLIB for iOS as Static Library.? – balajichinna Feb 22 '16 at 12:51
  • @balajichinna have you installed all the dependencies and have the latest versions of Cmake and Xcode? I'm afraid I'll need more information to help you further. – Rob Sanders Feb 22 '16 at 14:12
  • thanks for reply.How to install all dependencies for DLIB for IOS. I used Xcode 7.2 and IOS.cmake toolchain and make version is 3.2 .Can you send Xcode Project of DLIB or DLIB static lib for IOS.? – balajichinna Feb 22 '16 at 15:42
  • You need libX11 and opencv and that's what I can remember off the top of my head. X11 you can install using the tool in utilities and you'll need to download opencv using homebrew. I can't send you the project unfortunately. It's a work one and I don't have the rights to it. – Rob Sanders Feb 22 '16 at 15:50
  • Thanks for your reply. I am going install libX11 for IOS . Are you using DLIB CMakelist.txt for building DLIB for IOS.? – balajichinna Feb 23 '16 at 04:50
  • x11 is able to build Mac OS X only. Then How to build X11 for IOS/Iphone Device.? – balajichinna Feb 23 '16 at 06:06
  • You use cake to make the Xcode project and then Xcode to build for iOS. X11 gets bundled up in the binary but yes you can only use X11 related code on OS X. – Rob Sanders Feb 23 '16 at 08:12
  • Thanks @RASS i builded DLIB for IOS. I created Xcode project and included source code and set X11 headers in header path from Mac OSX and then i builded the lib static lib for IOS. Am i followed steps are correct.? – balajichinna Feb 23 '16 at 10:08
  • Sounds like you've got somewhere. You shouldn't have to include any more source code in the Xcode project, Cmake should do that for you. – Rob Sanders Feb 23 '16 at 10:22
  • But when i used DLIB CMakelist from downloaded github link. I got CMake error "pthread" not found . Thats why I added source code in Xcode.? So if i install thread for Mac OS X and then CMake gives no error. Am i correct.? – balajichinna Feb 23 '16 at 10:40
  • If you're not getting any errors and the library works then that's good. There are a lot of settings in the CMakeList, honestly I cant really tell any more without looking at your project. – Rob Sanders Feb 23 '16 at 12:21
  • Thanks @RASS . Which DLIB commit you are used from Github.? and DLIB static lib is working slow speed .? – balajichinna Feb 23 '16 at 12:24
  • I didn't download it from GitHub I downloaded it from [here](http://dlib.net/). Also you need to build in release configuration to speed it up. There are a also a load of instruction sets that can be activated (e.g. AAX) in the make file that speed up execution. – Rob Sanders Feb 23 '16 at 14:24
  • Thanks @RASS. Let me check it out. – balajichinna Feb 24 '16 at 04:46

4 Answers4

20

I finally figured out how to do this:

Requirements

  • X11 (on a mac you can just open the X11 app and if X11 isn't installed it'll take you to the download).
  • Xcode
  • cmake (you can use home-brew for that)

Steps

  • In terminal make the lib-xx.xx/examples your root
  • Run:

    mkdir build

    cd build

    cmake -G Xcode ..

    cmake --build . --config Release

This will create a folder called dlib_build in which you can find an Xcode project that compiles the library. In the build settings of that Xcode project you can set the build architecture and SDK for any Xcode supported OS you like!

EDIT:

You have to include a lot of custom compiler flags and 3rd party libraries to get dlib to work in a project. Check out the examples.xcproject build settings.

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • I did exactly like you said, Tho it gave me an xCode project that only complies on Mac(OSX). Am i missing something? – Roi Mulia May 25 '16 at 11:33
  • Yea all you have to do is go into the build settings and change the supported platforms. – Rob Sanders May 25 '16 at 14:34
  • @RASS hi! I also try to build my XCode app for iOS with dlib library, but I get a lot of problems due to architecture type(e.g. `ld: building for iOS simulator, but linking against dylib built for OSX, file '/opt/X11/lib/libICE.dylib' for architecture x86_64`). Did you used some architecture flags on the dlib building stage or on the XCode app building stage? – konstantin_doncov Jul 03 '16 at 01:31
  • @don-prog you can change the active architecture at the dlib build stage in the Xcode build settings. As default it is set to OS X but you can change it to iOS or simulator. – Rob Sanders Jul 03 '16 at 13:03
  • @RASS have you used iOS toolchain for building dlib for iOS? I ask this because I have [some problems with this](http://stackoverflow.com/questions/38175825/build-dlib-for-ios-using-cmake-ios-toolchain). It would be very kind of you if you could give me some particular advices about dlib building stage for iOS. Maybe I even don't need to use iOS toolchain or something else? – konstantin_doncov Jul 11 '16 at 03:47
  • No I haven't used the toolchain for building it. There are quite a few things that you need to do to the makefile (optimisations) and then at the dlib Xcode build stage. Check out [this project of mine](https://github.com/RedHandTech/Object_Detector) hopefully it will help. – Rob Sanders Jul 11 '16 at 09:44
  • @RASS thanks, this is a very detailed tutorial, all clear! But I got only a few problems with libs: `ld: building for iOS simulator, but linking against dylib built for OSX, file XXXXX for architecture x86_64` where XXXXX: libjpeg.dylib, liblapack.dylib, libpng.dylib, libcblas.dylib, It is obvious, because it's really dylib for OSX but I'm building iOS project. Also I have ld warning: `ld: warning: ignoring file /usr/local/lib/DLIB/iOS/libdlib.a, file was built for archive which is not the architecture being linked (i386): /usr/local/lib/DLIB/iOS/libdlib.a`. How can I fix these issues? – konstantin_doncov Jul 12 '16 at 15:44
  • @don-prog it just seems like you need to change the architecture that you build dlib for. This needs to be done in the Xcode project that is generated when you build it. If i remember correctly you use the -G flag with cmake to use Xcode as the build tool. – Rob Sanders Jul 12 '16 at 20:26
  • @RASS are you sure? Because the problem not in the dlib, but in the libjpeg.dylib, liblapack.dylib, libpng.dylib, libcblas.dylib. And if you are sure, which command I need to use for this? – konstantin_doncov Jul 12 '16 at 20:36
  • @don-prog have you set your header search paths correctly? I used home-brew to install lib jpeg and open cv and I put the DLIB headers in the include folder also. I also used Cocoapods to install opencv for iOS. – Rob Sanders Jul 12 '16 at 20:51
  • @RASS yes, I think I have set header search paths correctly. By the way, I checked libraries with which I have a problems using `file x.dylib` command and got an output which says that these libraries built only for i386 and x86_64 architecture, so I can't use them on arm64, armv7, armv7s. But I also checked libraries with which I HAVEN'T problems(e.g. /usr/lib/libpthread.dylib) and got the same output. So now I totally confused. Can you say how you installed libjpeg, liblapack, libpng, libcblas for use on iOS? And what should I do with linker warning which says about ignoring file libdlib.a? – konstantin_doncov Jul 13 '16 at 02:05
  • @RASS now I got rid of warning about libdlib.a, this was because I forgot to change iOS Device to iPhone simulator. But I still get errors due to `lapack`, `jpeg`, `png`, `cblas` libs. Nobody can help me, so any your suggestions will be very useful! Thanks. – konstantin_doncov Jul 15 '16 at 03:16
  • @RASS I am really sorry for the disturbance, but I still have no progress with this issue. I asked a few question on the SO, tried to post topics on the Apple forum but it's really gave nothing! I very need your help with this misunderstanding. Can you provide some advices about it? If you want I can pay for this using Upwork or using some another way, this is no problem. I really very need to know how I can do this. Regards. – konstantin_doncov Jul 23 '16 at 02:28
  • @don-prog the best thing to do might be for you to let me clone your project and see what needs to be done. Is it on github? – Rob Sanders Jul 23 '16 at 21:19
  • @RASS many thanks for your response! [I uploaded my test project to the github](https://github.com/don-prog/dlib_ios), hope this will help... This project causes `ld: building for iOS simulator, but linking against dylib built for OSX, file XXXXX for architecture x86_64` error. Also in this project I get this error if I use only `libdlib`: `ld: in /usr/local/lib/DLIB/iOS/libdlib.a(entropy_decoder_kernel_2.o), building for iOS simulator, but linking in object file built for OSX, for architecture x86_64` P.S. By the way, I need to set up Build Settings for project or for target? – konstantin_doncov Jul 24 '16 at 05:16
  • @don-prog the issue is not with the project you gave me it's with the installed dependencies, including DLIB. you said you were having problems with `libjpeg` and `libpng` e.t.c. how did you install them? Are they in the right directories? I'm sure you're aware that Xcode uses the library search paths to locate the lib files and the header search paths to get the includes. – Rob Sanders Jul 24 '16 at 10:26
  • @RASS sorry, now I have added the dependencies and paths where I have originals of these libraries. I think I have these libraries by default, but I also tried to use libjpeg and libpng which have been installed using homebrew, should I have to use some other way? How you did it? Also I use full paths of dependencies(as in your github project), so I think it's does not metter which library search paths I use. – konstantin_doncov Jul 24 '16 at 15:58
  • @don-prog I was using `OpenCV` quite a lot and `libjpeg` and `libpng` are dependencies of `OpenCV` which I installed using home-brew. The error you have: `/usr/local/lib/DLIB/iOS/libdlib.a ... building for iOS simulator, but linking in object file built for OSX, for architecture x86_64` is definitely a fault at the `dlib` build stage. Firstly did you build dlib for the simulator because it uses a different architecture than iOS devices? Also it seems you've built `dlib` for OS X which is a different architecture again. – Rob Sanders Jul 24 '16 at 17:59
  • @RASS wow... I think I did it! The problem was due to using iPhone-simulator instead of real iPhone. I switched target to iPhone and successfully built project with dlib and all of "problematic" libs. Many thanks for your support and one more time sorry for the this stupid issue :D I just have little experience in such iOS details. – konstantin_doncov Jul 25 '16 at 04:53
10

To compliment RASS's answer, I am attaching screenshots showing how to change this to and from an iOS and OSX lib

After opening the project,

  1. Select the project file from the project navigator
  2. Select the dlib target all the way down the bottom
  3. Select 'Build Settings'
  4. Expand 'Base SDK' drop down

enter image description here

  1. Select either iOS or macOS (OSX)

enter image description here

I hope this helps some people out! gl

mylogon
  • 2,772
  • 2
  • 28
  • 42
0

Rob Sanders and mylogon already show how to build dlib for ios, here is how to use it:

  1. add libdlib.a to project, and add path to library search paths
  2. add all source to include directory(do not add to project), and add path to header search paths.
  3. add accelerate framework, which contains blas symbols.
  4. add preprocessor macros, from building settings, "custom compiler flag"/"other c flags". these macros make sure the header files match the lib.

    -DDLIB_JPEG_SUPPORT
    -DDLIB_NO_GUI_SUPPORT
    -DNDEBUG
    -DDLIB_USE_BLAS
    -DDLIB_USE_LAPACK
    
andrewchan2022
  • 4,953
  • 45
  • 48
0

I have erros

Could not build the precompiled application for the device.
Error (Xcode): Undefined symbol: dlib::entropy_decoder_kernel_2::get_target(unsigned int)

Error (Xcode): Undefined symbol: dlib::entropy_decoder_kernel_2::set_stream(std::__1::basic_istream<char, std::__1::char_traits<char> >&)

Error (Xcode): Undefined symbol: dlib::entropy_decoder_kernel_2::decode(unsigned int, unsigned int)

Error (Xcode): Undefined symbol: dlib::entropy_decoder_kernel_2::entropy_decoder_kernel_2()

Error (Xcode): Undefined symbol: dlib::entropy_decoder_kernel_2::~entropy_decoder_kernel_2()

Error (Xcode): Undefined symbol: dlib::base64::base64()

Error (Xcode): Undefined symbol: dlib::base64::~base64()

Error (Xcode): Undefined symbol: dlib::base64::decode(std::__1::basic_istream<char, std::__1::char_traits<char> >&, std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const

Error (Xcode): Linker command failed with exit code 1 (use -v to see invocation)```
Hellomik U
  • 496
  • 5
  • 12