Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
How to use a C dylib from a Swift file in a framework

I'm looking at starting to use Swift in a framework, which uses libz.dylib, but it looks like there's no way to import it from within Swift. I tried import zlib and import libz, which didn't work. ZLib is already linked to the target.

It seems like the only way to get my Swift code to see the zlib classes is to import the necessary headers in a bridging header, but framework targets can't have a bridging header, so is there a way to use a dylib?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I have added a C/C++ dylib library, with C Public API headers to Swift Project.

Assume that the library is, libModule.dylib.

  1. Create a Directory with "Module"

    mkdir Module; cd Module
    
  2. Create a file module.map inside Module directory

    Touch module.map
    
  3. create a directory, this will be the header files directory, any name will serve.

    mkdir Headers
    
  4. copy all headers into Headers directory

    cp -R path/Headers Headers/
    
  5. open module.map & copy below contents to it, Swift team suggest, adding prefix C in front of library name, in case if its a C library.

    module CModule {
      umbrella "Headers"  // for multiple files
      header "filename.h" // for single header file, in this case even the Header directory is not needed.
      export *
    }
    
  6. Go to Build Settings -> Swift Compiler - Search Paths

  7. Add complete Module directory path to "Import Paths" key.
  8. Go to Build Phases, Add New Copy Files Phase.
  9. Drag libModule.dylib to the Copy Files phase. Select Destination to "Frameworks".
  10. If required add the libModule.dylib to Link Binary With Libraries section of Build Phases.

If step 9 is missed, we get famous @rpath/ Image not found crash.

Now Import module in swift as below:

    import CModule // name given inside module.map

For system library, most steps don't require.. just create directory & module map file with correct header path and do 6, 7. Its seems step 10 not even required as its a system library.

    module CommonCrypto [system] {
      header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
      export *
    }
over 4 years ago · Santiago Trujillo Relatório

0

You can import system modules as described in this answer: Importing CommonCrypto in a Swift framework

TLDR

  1. Add a Folder named ZLib to your framework directory
  2. Add a module.map file with the following contents

module ZLib [system] { header "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/zlib.h" export * }

You might have to change the path to your zlib header. The example works for XCode 7 Beta and El Capitan.

  1. Add the currently generated module.map file to your build settings
  2. Import ZLib in your swift file and you will be ready to go
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda