Swift Objective-C interoperability on Linux, error

2019-08-19 03:55发布

I'm trying to compile a swift program that uses code written in Objective-C on a VM running Linux (Ubuntu 16.0.4, Swift 4.0.3).

I get this error when compiling :

fatal error: 'Foundation/Foundation.h' file not found
#import <Foundation/Foundation.h>
        ^
1 error generated.

On Mac OS

The same program works perfectly on Mac OS.

What have I tried ?

I've tried reverting to swift 3.1.1 (I remember this working on swift 3 a while ago).

I've googled the problem, extensively, and had no success.

I've followed instructions on how to install the Objective-C runtime on Linux and installed GNUStep (see http://www.techotopia.com/index.php/Installing_and_Using_GNUstep_and_Objective-C_on_Linux)

All with no success.

why am I using objective-C ?

(Anticipating this question).

I need to call a python script from the program. Python has a library for C, which I can only use with C or Objective-C.

In fact, commenting out the line #import <Foundation/Foundation.h> makes compilation fail when importing the Python library :

fatal error: 'Python/Python.h' file not found
#include <Python/Python.h>
         ^
1 error generated.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-19 04:30

Swift on Linux is not interoperable with Objective-C, it is interoperable with C.

According to https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#c-language-targets, you can have plain C code as part of your package, in a separate target.

You can also link your package with a C library, see https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#c-language-targets.

In your case I would use the later approach and would link your code with the Python C library.

查看更多
登录 后发表回答