在Linux上的Swift中使用C库 [英] Use a C library in Swift on Linux

查看:84
本文介绍了在Linux上的Swift中使用C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Mac OS上使用Xcode在Swift中访问C库,并且我知道在Linux上的import Glibc,但是我如何在Linux上将Swift这样的C库与OpenGL一起使用?

I know how to access C libraries in Swift using Xcode on Mac OS, and I know about import Glibc on Linux, but how can I use a C library like OpenGL with Swift on Linux?

推荐答案

使用系统模块导入OpenGL头文件: https://github.com/apple/swift-package -manager/blob/master/Documentation/SystemModules.md

Use a System Module to import the OpenGL header file: https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

假设您的目录布局如下:

Assuming you have a directory layout like:

COpenGL/
  Package.swift
  module.modulemap
  .git/

YourApp/
  Package.swift
  main.swift
  .git/

COpenGL/module.modulemap文件将类似于:

the COpenGL/module.modulemap file will look something like:

module COpenGL [system] {
    header "/usr/include/gl/gl.h"
    link "gl"
    export *
}

这必须在一个单独的git repo中创建,并带有一个版本标签:

This has to be created in a separate git repo, with a version tag:

touch Package.swift
git init
git add .
git commit -m "Initial Commit"
git tag 1.0.0

然后在YourApp/Package.swift文件中将其声明为依赖项

Then declare it as a dependency in YourApp/Package.swift file

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "../COpenGL", majorVersion: 1)
    ]
) 

然后在main.swift文件中可以将其导入:

Then in your main.swift file you can import it:

import COpenGL
// use opengl calls here...

这篇关于在Linux上的Swift中使用C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆