[swift-users] question about swift and c callbacks

Yang Yang cometyang at gmail.com
Wed Jan 4 23:20:20 CST 2017


Thanks.
It compiled successfully now under linux.


2017-01-04 12:29 GMT-06:00 Joe Groff <jgroff at apple.com>:

>
> > On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users <
> swift-users at swift.org> wrote:
> >
> > I try to wrap a c library in swift package named Test.
> > The code looks like this:
> >
> > Test1.h
> >
> > struct MyParams {
> >       int (*func)();
> > };
> >
> > typedef struct MyParams MyParams;
> >
> > Test2.h
> > #include "Test1.h"
> >
> > static int test(){
> >   return 0;
> > }
> >
> > void myTest()
> > {
> >       MyParams params;
> >       params.func = test;
> > }
> >
> >
> > Then I try to import the package into swift code and use
> > in Test.swift.
> > ************************
> > import Test
> >
> > class TestSwift {
> >   func runtest(){
> >     myTest()
> >   }
> > }
> >
> > However, the compiler report link error:
> > function myTest: error: undefined reference to 'test'
> >
> > If I comment out
> > "params.func = test;" in myTest.
> >
> > It successfully build. How should I handle this situation?
>
> Try making `test` static inline instead of just static.
>
> -Joe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170104/d2db53e6/attachment.html>


More information about the swift-users mailing list