hi every one ,who can help me? 3Q ~


(Suipengfei) #1

Hi all, I want to make a simple emample to show swift can call c library.

So ,I make a simple example,like this:

$ cat a.c

int add(int a,int b){

        return a + b;

}

And I make a lib,

$ gcc -o a.o -c a.c

$ ar rcs libmylib.a a.o

And I put libmylib.a in the same director with the main.swift

$ cat main.swift

import mylib

var b = add(10,20)

print(b)

when I compile :

$ swiftc main.swift

main.swift:1:8: error: no such module 'mylib'

import mylib

       ^

I am just confused with this. Can you explain that with you pressure time. And what should I do? Thank you a lot ~


(none) #2

You'll need to create a module map to import C modules. In practise it'll
be easier to use swiftpm for such things. I had written a tutorial for this
:

http://ankit.im/swift/2015/12/27/ship-c-code-with-swift-packages-using-swift-package-manager/

···

On Friday 29 January 2016, Suipengfei via swift-users <swift-users@swift.org> wrote:

Hi all, I want to make a simple emample to show swift can call c library.

So ,I make a simple example,like this:

$ cat a.c

int add(int a,int b){

        return a + b;

}

And I make a lib,

$ gcc -o a.o -c a.c

$ ar rcs libmylib.a a.o

And I put libmylib.a in the same director with the main.swift

$ cat main.swift

import mylib

var b = add(10,20)

print(b)

when I compile :

$ swiftc main.swift

main.swift:1:8: error: no such module 'mylib'

import mylib

       ^

I am just confused with this. Can you explain that with you pressure time.
And what should I do? Thank you a lot ~

--
Ankit


(Ankit Agarwal) #3

You'll need to create a module map to import C modules. In practise it'll
be easier to use swiftpm for such things. I had written a tutorial for this
:

http://ankit.im/swift/2015/12/27/ship-c-code-with-swift-packages-using-swift-package-manager/

···

On Friday 29 January 2016, Suipengfei via swift-users <swift-users@swift.org> wrote:

Hi all, I want to make a simple emample to show swift can call c library.

So ,I make a simple example,like this:

$ cat a.c

int add(int a,int b){

        return a + b;

}

And I make a lib,

$ gcc -o a.o -c a.c

$ ar rcs libmylib.a a.o

And I put libmylib.a in the same director with the main.swift

$ cat main.swift

import mylib

var b = add(10,20)

print(b)

when I compile :

$ swiftc main.swift

main.swift:1:8: error: no such module 'mylib'

import mylib

       ^

I am just confused with this. Can you explain that with you pressure time.
And what should I do? Thank you a lot ~

--
Ankit


(Suipengfei) #4

Hi ankit,

Really thank you very very much. I can’t solve these problem for a lot of time.
You document is very clear and easy to understand.

Thank you again~

···

发件人: Ankit Agarwal [mailto:ankit.spd@gmail.com]
发送时间: 2016年1月29日 15:32
收件人: Suipengfei
抄送: swift-users@swift.org
主题: Re: [swift-users] hi every one ,who can help me? 3Q ~

You'll need to create a module map to import C modules. In practise it'll be easier to use swiftpm for such things. I had written a tutorial for this :

http://ankit.im/swift/2015/12/27/ship-c-code-with-swift-packages-using-swift-package-manager/

On Friday 29 January 2016, Suipengfei via swift-users <swift-users@swift.org<mailto:swift-users@swift.org>> wrote:

Hi all, I want to make a simple emample to show swift can call c library.

So ,I make a simple example,like this:

$ cat a.c

int add(int a,int b){

        return a + b;

}

And I make a lib,

$ gcc -o a.o -c a.c

$ ar rcs libmylib.a a.o

And I put libmylib.a in the same director with the main.swift

$ cat main.swift

import mylib

var b = add(10,20)

print(b)

when I compile :

$ swiftc main.swift

main.swift:1:8: error: no such module 'mylib'

import mylib

       ^

I am just confused with this. Can you explain that with you pressure time. And what should I do? Thank you a lot ~

--
Ankit