Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use libxml in Swift? [closed]

I try to use this new language to start a new project with Some of my old code, I used Libxml2.2 in my old project,so xcode shows "libxml.h/parse.h file not found" after my putting the code in my new project. i have already imported the "libxml2.2.dylib"

like image 752
Xianng Avatar asked Jun 19 '14 07:06

Xianng


4 Answers

I 've made it work with CommonCrypto https://github.com/onmyway133/CommonCrypto.swift but libxml should be the same. Things to keep in mind

module.modulemap

Read http://clang.llvm.org/docs/Modules.html#module-map-language

Create a folder named libxml2 (in my case, it is at the same directory as my project file), then create a module.modulemap file inside

module libxml2 {
  header "/usr/include/libxml2/libxml/tree.h"
  export *
}

Here I add just tree.h, but you can add more for your need

SWIFT_INCLUDE_PATHS

Go to Target Build Settings, add this into Import Paths

${SRCROOT}/libxml2

HEADER_SEARCH_PATHS

Read Why am I getting this "libxml/tree.h file not found" error?

Go to Target Build Settings, add this into Header Search Paths

$(SDKROOT)/usr/include/libxml2

Interesting related posts

  • Swift Framework with libxml
  • https://spin.atomicobject.com/2015/02/23/c-libraries-swift/
  • Importing CommonCrypto in a Swift framework
  • https://github.com/onmyway133/Reindeer
like image 181
onmyway133 Avatar answered Oct 21 '22 01:10

onmyway133


To get this working with my project in Xcode 6, I navigated to "Build Settings" for my project, and selected my target. I then searched for "Header Search Paths" in the Build Settings, and added the following line to both debug and release:

$(SDKROOT)/usr/include/libxml2
like image 32
FredL Avatar answered Oct 21 '22 01:10

FredL


I found that you can leave out importing 'libxml2.2.dylib' if you add '-lxml2' to OTHER_LDFLAGS.

like image 39
Mark Knopper Avatar answered Oct 21 '22 01:10

Mark Knopper


You will have to create a custom module map file containing the header of libxml/tree.h like so :

module libxml [system] {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/tree.h"
    export *
}

Then you place this file (module.modulemap) inside a folder -module for example- and add it to your project's and target's build settings.

You can now use @import libxml; in your code.

like image 2
Loegic Avatar answered Oct 21 '22 01:10

Loegic