Binding Regular C Functions (Gtk, Cairo, etc.)


(Gage Morgan) #1

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

···

Sent from Outlook


(Mish Awadah) #2

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

···

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook <http://aka.ms/Ox5hz3>
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #3
···

From: Gage Morgan gagemorgan@outlook.com
Sent: Monday, December 7, 2015 9:40 PM
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
To: Mish Awadah mawadah@apple.com

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, “Mish Awadah” mawadah@apple.com wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

  • mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users < swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I’d like to know how to import C functions to Swift. Once I can do this successfully, I’ll be all set.

Sent from Outlook


swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #4

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.
To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.
For instance, in the example from the sysmodules doc link I sent, you just
import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.
- mish

···

From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:
Linking Executable: .build/debug/example/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.soclang: error: linker command failed with exit code 1 (use -v to see invocation)<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)<unknown>:0: error: build had 1 command failuresswift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]
What went amiss?
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from
Outlook

_______________________________________________

swift-users mailing list

swift-users@swift.org

https://lists.swift.org/mailman/listinfo/swift-users


(David Turnbull) #5

I think you're looking for this book: Using Swift with Cocoa and Objective-C
https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773
It's also in the Apple Developers web site as HTML. It's free either way.
Look for the chapter: Interacting with C APIs

-david

···

On Mon, Dec 7, 2015 at 6:49 PM, Gage Morgan via swift-users < swift-users@swift.org> wrote:

I read that. What I want to know is how to bind the functions themselves
after creating a system package. How to use the c functions that got
imported, if you will.

Sent from Outlook <http://aka.ms/Ox5hz3>

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> > wrote:

Please see the reference at
https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish


(Kwame Bryan) #6

There’s also this video by chris eidhof https://realm.io/news/pragma-chris-eidhof-swift-c/ . This will help with seeing a live implementation.

Kwame

···

On Dec 7, 2015, at 10:23 PM, David Turnbull via swift-users <swift-users@swift.org> wrote:

I think you're looking for this book: Using Swift with Cocoa and Objective-C
https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773
It's also in the Apple Developers web site as HTML. It's free either way.
Look for the chapter: Interacting with C APIs

-david

On Mon, Dec 7, 2015 at 6:49 PM, Gage Morgan via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook <http://aka.ms/Ox5hz3>

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com <mailto:mawadah@apple.com>> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #7

Okey, so I see what they're trying to say, but what about functions from C that actually take input? For example, in Cairo, cairo_move_to() would not make the cut on its own. Matter of fact, it would take a surface in the form of a pointer and an x and y value. So we would have
cairo_surface_t *crcairo_move_to(cr, x, y);
among countless others. Is Swift able to pull this out of thin air, or do we have to take each individual function and convert it to Swift ourselves? I guess what I'm asking is: Is Swift able to understand something like
import CCairofunc cairo_line_to: Void(let cairo_t = MutableUnsafePointer(cr), x: Double, y: Double)
after I include just a header file and map it? The function draws a line on a surface. Assuming the syntax is correct, then you're telling me the process is just:
1) Map the cairo.h file to a system module named CCairo2) Include that in a file3) Convert any functions you've entered in C into a Swift format, no binding "workarounds" necessary like other lagnguages, and4) Swift recognizes you meant to use that function that is written specifically in C and is included in that header file mapped by CCairo. If Swift cannot automatically know that you meant to pull a function from a C header file, what must I do if it cannot?
I came here because I've read tutorials but am still confused on whether or not the CCairo module itself can just recognize a function without having to do more.

···

Sent from Outlook

On Mon, Dec 7, 2015 at 6:49 PM -0800, "Gage Morgan" <gagemorgan@outlook.com> wrote:

    _____________________________
From: Gage Morgan <gagemorgan@outlook.com>
Sent: Monday, December 7, 2015 9:40 PM
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
To: Mish Awadah <mawadah@apple.com>

              I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

        Sent from Outlook

      On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

         Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md
            - mish
                         On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users < swift-users@swift.org> wrote:
                       For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

                  Sent from Outlook
                 _______________________________________________
swift-users mailing list
        swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Max Howell) #8

Probably this is a link path issue. Can you open a ticket including your module map and an example source?

https://bugs.swift.org

Thanks,

Max

···

From:
gagemorgan@outlook.com <mailto:gagemorgan@outlook.com>
To: mawadah@apple.com <mailto:mawadah@apple.com>
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:

Linking Executable: .build/debug/example
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]

What went amiss?

From: mawadah@apple.com <mailto:mawadah@apple.com>
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com <mailto:gagemorgan@outlook.com>

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.

To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.

For instance, in the example from the sysmodules doc link I sent, you just

import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.

- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com <mailto:gagemorgan@outlook.com>> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook <http://aka.ms/Ox5hz3>

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com <mailto:mawadah@apple.com>> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook <http://aka.ms/Ox5hz3>
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #9

Okay, I've filed a report. To make it easier, the thing's code is SR-152.
https://bugs.swift.org/browse/SR-152

···

From: gagemorgan@outlook.com
To: mawadah@apple.com; gagemorgan@outlook.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:11:03 -0500

What I'd like to know is, what is the "link: " part of the module.modulemap for? I pasted in the cairo.so part, but this only said that after doing so. I'd honestly just like to make sure this works.

To: mawadah@apple.com; swift-users@swift.org
Date: Tue, 8 Dec 2015 18:07:25 -0500
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
From: swift-users@swift.org

From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:
Linking Executable: .build/debug/example/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.soclang: error: linker command failed with exit code 1 (use -v to see invocation)<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)<unknown>:0: error: build had 1 command failuresswift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]
What went amiss?
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.
To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.
For instance, in the example from the sysmodules doc link I sent, you just
import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.
- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from
Outlook

_______________________________________________

swift-users mailing list

swift-users@swift.org

https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #10

Okay, thanks. I'll experiment tomorrow evening. Cheers,--MGage--

···

Sent from Outlook

On Mon, Dec 7, 2015 at 7:39 PM -0800, "Gage Morgan" <gagemorgan@outlook.com> wrote:

Okey, so I see what they're trying to say, but what about functions from C that actually take input? For example, in Cairo, cairo_move_to() would not make the cut on its own. Matter of fact, it would take a surface in the form of a pointer and an x and y value. So we would have
cairo_surface_t *crcairo_move_to(cr, x, y);
among countless others. Is Swift able to pull this out of thin air, or do we have to take each individual function and convert it to Swift ourselves? I guess what I'm asking is: Is Swift able to understand something like
import CCairofunc cairo_line_to: Void(let cairo_t = MutableUnsafePointer(cr), x: Double, y: Double)
after I include just a header file and map it? The function draws a line on a surface. Assuming the syntax is correct, then you're telling me the process is just:
1) Map the cairo.h file to a system module named CCairo2) Include that in a file3) Convert any functions you've entered in C into a Swift format, no binding "workarounds" necessary like other lagnguages, and4) Swift recognizes you meant to use that function that is written specifically in C and is included in that header file mapped by CCairo. If Swift cannot automatically know that you meant to pull a function from a C header file, what must I do if it cannot?
I came here because I've read tutorials but am still confused on whether or not the CCairo module itself can just recognize a function without having to do more.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:49 PM -0800, "Gage Morgan" <gagemorgan@outlook.com> wrote:

    _____________________________
From: Gage Morgan <gagemorgan@outlook.com>
Sent: Monday, December 7, 2015 9:40 PM
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
To: Mish Awadah <mawadah@apple.com>

              I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

        Sent from Outlook

      On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

         Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md
            - mish
                         On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users < swift-users@swift.org> wrote:
                       For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

                  Sent from Outlook
                 _______________________________________________
swift-users mailing list
        swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jens Alfke) #11

Yes. Swift understands that this is a C function, and follows C calling conventions in the machine code that implements the call, so there’s no glue code necessary.

(That is, this is like C++’s `extern “C”`, not like SWIG.)

—Jens

···

On Dec 7, 2015, at 7:39 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

1) Map the cairo.h file to a system module named CCairo
2) Include that in a file
3) Convert any functions you've entered in C into a Swift format, no binding "workarounds" necessary like other languages,


(Jason Dusek) #12

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift'

···

On Tue, 8 Dec 2015 at 18:22 Gage Morgan via swift-users <swift-users@swift.org> wrote:

Okay, I've filed a report. To make it easier, the thing's code is SR-152.

https://bugs.swift.org/browse/SR-152

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com; gagemorgan@outlook.com

Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:11:03 -0500

What I'd like to know is, what is the "link: " part of the module.modulemap for? I pasted in the cairo.so part, but this only said that after doing so. I'd honestly just like to make sure this works.

________________________________
To: mawadah@apple.com; swift-users@swift.org
Date: Tue, 8 Dec 2015 18:07:25 -0500

Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
From: swift-users@swift.org

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:

Linking Executable: .build/debug/example
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]

What went amiss?

________________________________
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.

To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.

For instance, in the example from the sysmodules doc link I sent, you just

import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.

- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Harlan Haskins) #13

Just covering all the bases here: did you commit and add Package.swift before making the tag?

···

Sent from my iPhone

On Dec 8, 2015, at 11:08 PM, Jason Dusek via swift-users <swift-users@swift.org> wrote:

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift'

On Tue, 8 Dec 2015 at 18:22 Gage Morgan via swift-users > <swift-users@swift.org> wrote:

Okay, I've filed a report. To make it easier, the thing's code is SR-152.

https://bugs.swift.org/browse/SR-152

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com; gagemorgan@outlook.com

Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:11:03 -0500

What I'd like to know is, what is the "link: " part of the module.modulemap for? I pasted in the cairo.so part, but this only said that after doing so. I'd honestly just like to make sure this works.

________________________________
To: mawadah@apple.com; swift-users@swift.org
Date: Tue, 8 Dec 2015 18:07:25 -0500

Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
From: swift-users@swift.org

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:

Linking Executable: .build/debug/example
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]

What went amiss?

________________________________
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.

To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.

For instance, in the example from the sysmodules doc link I sent, you just

import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.

- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Max Howell) #14

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift’

    rm -rf /Users/solidsnack/Downloads/Sources/example/Packages

The PM does not update the git repo, by default this is the right behavior since you should not change your dependency sources every build. However I agree this is confusing in these situations. We plan some sort of “dev mode”.


(Gage Morgan) #15

Jason,The Link: part of the module.modulemap seems to look for libcairo.so but then outputs the errors shown previously. Instead of CCAiro not found, it becomes "couldn't link library" though the absolute path exists.
Cheers,--MGage--

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift'

···

Sent from Outlook Mobile
On Tue, Dec 8, 2015 at 8:08 PM -0800, "Jason Dusek" <jason.dusek@gmail.com> wrote:

On Tue, 8 Dec 2015 at 18:22 Gage Morgan via swift-users <swift-users@swift.org> wrote:

Okay, I've filed a report. To make it easier, the thing's code is SR-152.

https://bugs.swift.org/browse/SR-152

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com; gagemorgan@outlook.com

Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:11:03 -0500

What I'd like to know is, what is the "link: " part of the module.modulemap for? I pasted in the cairo.so part, but this only said that after doing so. I'd honestly just like to make sure this works.

________________________________
To: mawadah@apple.com; swift-users@swift.org
Date: Tue, 8 Dec 2015 18:07:25 -0500

Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
From: swift-users@swift.org

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:

Linking Executable: .build/debug/example
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]

What went amiss?

________________________________
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.

To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.

For instance, in the example from the sysmodules doc link I sent, you just

import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.

- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Chris Hanson) #16

Since Swift can natively call C APIs, I would recommend just using “Cairo” as the module name.

There’s no need to distinguish a module as being related to a C API.

  -- Chris

···

On Dec 7, 2015, at 7:40 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

1) Map the cairo.h file to a system module named CCairo


(Gage Morgan) #17

Awesome. So no technical binding here. Will test soon. Cheers,
--MGage--

···

Sent from Outlook Mobile

On Tue, Dec 8, 2015 at 9:01 AM -0800, "Jens Alfke" <jens@mooseyard.com> wrote:

On Dec 7, 2015, at 7:39 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

1) Map the cairo.h file to a system module named CCairo
2) Include that in a file
3) Convert any functions you've entered in C into a Swift format, no binding "workarounds" necessary like other languages,

Yes. Swift understands that this is a C function, and follows C calling conventions in the machine code that implements the call, so there’s no glue code necessary.

(That is, this is like C++’s `extern “C”`, not like SWIG.)

—Jens


(Gage Morgan) #18

I followed whatever procedure had been posted on the System Modules page on the Package Manager repo at GitHub. So yes, and I tried about 3 times :slight_smile:

Just covering all the bases here: did you commit and add Package.swift before making the tag?

···

Sent from Outlook Mobile
On Tue, Dec 8, 2015 at 9:24 PM -0800, "Harlan Haskins" <harlan@harlanhaskins.com> wrote:

Sent from my iPhone

On Dec 8, 2015, at 11:08 PM, Jason Dusek via swift-users <swift-users@swift.org> wrote:

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift'

On Tue, 8 Dec 2015 at 18:22 Gage Morgan via swift-users > <swift-users@swift.org> wrote:

Okay, I've filed a report. To make it easier, the thing's code is SR-152.

https://bugs.swift.org/browse/SR-152

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com; gagemorgan@outlook.com

Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:11:03 -0500

What I'd like to know is, what is the "link: " part of the module.modulemap for? I pasted in the cairo.so part, but this only said that after doing so. I'd honestly just like to make sure this works.

________________________________
To: mawadah@apple.com; swift-users@swift.org
Date: Tue, 8 Dec 2015 18:07:25 -0500

Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
From: swift-users@swift.org

________________________________
From: gagemorgan@outlook.com
To: mawadah@apple.com
Subject: RE: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Tue, 8 Dec 2015 18:03:42 -0500

So...including cairo in an app returns this:

Linking Executable: .build/debug/example
/usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libcairo.so
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/mgage/Downloads/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/mgage/Sources/example/.build/debug/example.o/llbuild.yaml"]

What went amiss?

________________________________
From: mawadah@apple.com
Subject: Re: [swift-users] Binding Regular C Functions (Gtk, Cairo, etc.)
Date: Mon, 7 Dec 2015 19:27:56 -0800
To: gagemorgan@outlook.com

Once you have followed the instructions to create a system package, your own package declares a dependency on that package. This is also covered in those steps.

To use the C APIs in your code, simply import the name of the module (as defined by the modulemap entry) in your swift file. It should just work.

For instance, in the example from the sysmodules doc link I sent, you just

import CJPEG

The C functions will have the same name in Swift as their C counterparts. The types might adjust to UnsafePointer etc.. For a list of those Swift bindings, run the REPL and import your module, then perform “:type lookup Module” to get a printout of available APIs.

- mish

On Dec 7, 2015, at 6:40 PM, Gage Morgan <gagemorgan@outlook.com> wrote:

I read that. What I want to know is how to bind the functions themselves after creating a system package. How to use the c functions that got imported, if you will.

Sent from Outlook

On Mon, Dec 7, 2015 at 6:39 PM -0800, "Mish Awadah" <mawadah@apple.com> wrote:

Please see the reference at https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

- mish

On Dec 7, 2015, at 6:33 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For any project: How is a C to Swift function binding created? I'd like to know how to import C functions to Swift. Once I can do this successfully, I'll be all set.

Sent from Outlook

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #19

So would things break if I ran swift build in the CCairo directory and then example? This so confusing...

···

Sent from Outlook Mobile

On Wed, Dec 9, 2015 at 11:19 AM -0800, "Max Howell" <max.howell@apple.com> wrote:

Gage,

For what it's worth, after running `git init` and `git tag -f 1.0.0`
in `CCairo` to get something importable, I run into this error:

:; swift build
Cloning Packages/CCairo
<unknown>:0: error: no such file or directory:
'/Users/solidsnack/Downloads/Sources/example/Packages/CCairo/Package.swift’

    rm -rf /Users/solidsnack/Downloads/Sources/example/Packages

The PM does not update the git repo, by default this is the right behavior since you should not change your dependency sources every build. However I agree this is confusing in these situations. We plan some sort of “dev mode”.


(Max Howell) #20

Chris, adding the C prefix is our recommendation so it is clear at the import statement that the sources in question are using the raw C interface rather than any “Swifty” wrapper.

We hope thus that the community will provide modules that add error handling, default arguments, closures, etc. while (importantly) still maintaining the general API of the C library in question and then name them without the C prefix.

Here is our documentation with further justification:

https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md

···

On Dec 8, 2015, at 12:55 PM, Chris Hanson via swift-users <swift-users@swift.org> wrote:

On Dec 7, 2015, at 7:40 PM, Gage Morgan via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

1) Map the cairo.h file to a system module named CCairo

Since Swift can natively call C APIs, I would recommend just using “Cairo” as the module name.

There’s no need to distinguish a module as being related to a C API.

  -- Chris

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users