简体   繁体   English

如何在 Xcode 11 package 管理器中处理 C 文件?

[英]How to handle C-Files in Xcode 11 package manager?

I have a C-function, which I call in a Swift wrapper function.我有一个 C 函数,我在 Swift 包装器 function 中调用它。 Now, I want to create a Swift-package with this Swift wrapper function.现在,我想用这个 Swift 包装器 function 创建一个 Swift 包。 However, I am doing something wrong.但是,我做错了什么。

Here is the C-function which adds 2 double values.这是添加 2 个双精度值的 C 函数。

//  addDoubles.c
#include <math.h>
#include "addDoubles.h"

double addDoubleWithC(double val1, double val2) {
    return val1 + val2;
}

Here is the Swift file with a wrapping function:这是带有包装 function 的 Swift 文件:

//  WrappedCFunctions.swift
import Foundation
public struct WrappedCFunctions {    
public static func addDoubles(val1:Double, val2:Double) -> Double{
    let resulFromCFunction = addDoubleWithC(val1, val2)
return resulFromCFunction
}
}

In the viewController I am using this function:在 viewController 我使用这个 function:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var textLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    let val1 = 1.11
    let val2 = 2.22
    let result = WrappedCFunctions.addDoubles(val1: val1, val2: val2) 
    let txt = String(val1) + " + " + String(val2) + " = " + String(result)
    textLabel.text = txt
}
}

The project structure is the following:项目结构如下:

在此处输入图像描述

This app works correctly, as expected.此应用程序按预期正常工作。

In the next step I created with File-> New-> Swift Packe a package with name Math.在下一步中,我使用 File-> New-> Swift 创建了一个名为 Math 的 package。 Then I moved the file WrappedCFunctions.swift from the AddDoubles Folder into the CMath/Source/CMath folder of the package.然后我将文件 WrappedCFunctions.swift 从 AddDoubles 文件夹移动到 package 的 CMath/Source/CMath 文件夹中。 In the next step I included the package into the project.在下一步中,我将 package 包含到项目中。 This leads to the following structure:这导致以下结构:

在此处输入图像描述

Last but not least I added an Import CMath statement in ViewController.swift and built the project.最后但同样重要的是,我在 ViewController.swift 中添加了 Import CMath 语句并构建了项目。

However, the package is not recognised.但是,无法识别 package。

在此处输入图像描述

How to fix this problem?如何解决这个问题?

Without seeing your Package.swift file it is impossible to know if you package is setup correctly.如果没有看到您的Package.swift文件,就不可能知道您的 package 是否设置正确。 Looking at the pictures you did post the best guess I have is that it is not configured properly.查看您发布的图片,我最好的猜测是它没有正确配置。

The reason I am guessing this is that you only have 1 directory in your Sources/ when you will need 2 targets (usually with SwiftPM you have 1 directory per target and I don't see any.c files in the package).我猜测的原因是,当您需要 2 个目标时,您的Sources/中只有 1 个目录(通常使用 SwiftPM,每个目标都有 1 个目录,而我没有看到包中的任何 .c 文件)。 The C code will need to be a target that the Swift target depends on. C 代码需要成为 Swift 目标所依赖的目标。 Then you have a product that depends on the Swift target.然后你就有了一个依赖于 Swift 目标的产品。 That product is what you will link and import in Xcode.该产品就是您将在 Xcode 中链接和导入的产品。

If you look at this repo I have, I use both C and Swift so you can see how I have setup the Package.swift to build the 2 targets. If you look at this repo I have, I use both C and Swift so you can see how I have setup the Package.swift to build the 2 targets.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM