Quantcast
Channel: Package Manager - Swift Forums
Viewing all articles
Browse latest Browse all 180

Problem including a C Library with Swift executable using SwiftPM

$
0
0

I am having a really frustrating time trying to get a non-system C library to work with the SPM. I have tried everything including talking to ChatGPT 3.5 to help me out. Everything I have looked at on the internet has led me to feeling that I am doing it correctly but it just doesn't work. So here it is:

I have a Clibrary called "prism". This library comes in both a static (libprism.a) and shared version (lib prism.dylib). These are not installed via a package manager on the system. These libraries should be included in the project.

Here is my project structure:

.
├── Package.swift
└── Sources
├── libprism
│ ├── includes
│ │ ├── prism
│ │ │ ├── ast.h
│ │ │ ├── defines.h
│ │ │ ├── diagnostic.h
│ │ │ ├── encoding.h
│ │ │ ├── node.h
│ │ │ ├── options.h
│ │ │ ├── pack.h
│ │ │ ├── parser.h
│ │ │ ├── prettyprint.h
│ │ │ ├── regexp.h
│ │ │ ├── static_literals.h
│ │ │ ├── util
│ │ │ │ ├── pm_buffer.h
│ │ │ │ ├── pm_char.h
│ │ │ │ ├── pm_constant_pool.h
│ │ │ │ ├── pm_integer.h
│ │ │ │ ├── pm_list.h
│ │ │ │ ├── pm_memchr.h
│ │ │ │ ├── pm_newline_list.h
│ │ │ │ ├── pm_state_stack.h
│ │ │ │ ├── pm_string.h
│ │ │ │ ├── pm_string_list.h
│ │ │ │ ├── pm_strncasecmp.h
│ │ │ │ └── pm_strpbrk.h
│ │ │ └── version.h
│ │ └── prism.h
│ ├── libprism.a
│ ├── libprism.dylib
│ └── module.modulemap
└── test
└── main.swift

7 directories, 30 files

Here is the contents of the module.modulemap file:

module libprism {
    header "includes/prism.h"
    link "prism"
    export *
}

Here is my Package.swift file:

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "test",
    products: [
        .executable(name: "test", targets: ["test"])
    ],
    targets: [

        .systemLibrary(
            name: "libprism",
            path: "Sources/libprism"
        ),

        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "test",
            dependencies:["libprism"]
),
    ]
)

Here is the output of the swift build command:

❯ swift build        
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "includes/prism.h"
        ^
/Users/bruce/Developer/swift/test/Sources/libprism/includes/prism.h:10:10: note: in file included from /Users/bruce/Developer/swift/test/Sources/libprism/includes/prism.h:10:
#include "prism/util/pm_buffer.h"
         ^
/Users/bruce/Developer/swift/test/Sources/libprism/includes/prism/util/pm_buffer.h:9:10: error: 'prism/defines.h' file not found
#include "prism/defines.h"
         ^
/Users/bruce/Developer/swift/test/Sources/test/main.swift:1:8: error: could not build Objective-C module 'libprism'
import libprism
       ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "includes/prism.h"
        ^
/Users/bruce/Developer/swift/test/Sources/libprism/includes/prism.h:10:10: note: in file included from /Users/bruce/Developer/swift/test/Sources/libprism/includes/prism.h:10:
#include "prism/util/pm_buffer.h"
         ^
/Users/bruce/Developer/swift/test/Sources/libprism/includes/prism/util/pm_buffer.h:9:10: error: 'prism/defines.h' file not found
#include "prism/defines.h"
         ^
/Users/bruce/Developer/swift/test/Sources/test/main.swift:1:8: error: could not build Objective-C module 'libprism'
import libprism
       ^
error: fatalError

I have been trying to get this to compile and link for hours. I have tried different combinations of things with no avail. Any help would be appreciated.

Thanks

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 180

Latest Images

Trending Articles



Latest Images