question about swift and c callbacks

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?

Thanks,

Yang

Can you create a quick tarball of the swiftpm project. Would expedite the answering of the question.

Michael

···

On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users <swift-users@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?

Thanks,

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

Try making `test` static inline instead of just static.

-Joe

···

On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users <swift-users@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?

Hi Michael,

The attachment is the packages I created for testing purpose.
You can check it.

Thanks for the help,

Yang

Test1.tar.gz (91.1 KB)

···

2016-12-30 18:15 GMT-06:00 Michael Gottesman <mgottesman@apple.com>:

Can you create a quick tarball of the swiftpm project. Would expedite the
answering of the question.

Michael

> On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users < > swift-users@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?
>
> Thanks,
>
> Yang
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

Thanks.
It compiled successfully now under linux.

···

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

> On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users < > swift-users@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

sorry, i made mistake on compiling. It still report the same link error.

Yang

···

On Wed, Jan 4, 2017 at 11:20 PM Yang Yang <cometyang@gmail.com> wrote:

Thanks.
It compiled successfully now under linux.

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

> On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users < > swift-users@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

I simplified your example package and posted a bug report here:

···

On Thu, Jan 5, 2017 at 9:59 PM, Yang Yang via swift-users < swift-users@swift.org> wrote:

sorry, i made mistake on compiling. It still report the same link error.

Yang

On Wed, Jan 4, 2017 at 11:20 PM Yang Yang <cometyang@gmail.com> wrote:

Thanks.
It compiled successfully now under linux.

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

> On Dec 30, 2016, at 11:17 AM, Yang Yang via swift-users < >> swift-users@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

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

--
Ankit