[SwiftPM] [swift-build] How to use C module in Swift REPL


(bhargav gurlanka) #1

Hi,

I have a swift package with following layout:
.
├── Package.swift
├── Sources
│ ├── BarCModule
│ │ ├── Bar.c
│ │ └── include
│ │ └── Bar.h
│ └── MyApp
│ └── main.swift
└── Tests

After running swift build, I can see libBarCModule.so under .build/debug.
How can I use this library in REPL?

I've tried:

$ swift -I.build/debug -L.build/debug -lBarCModule
$ LD_LIBRARY_PATH=.build/debug swift -I.build/debug -L.build/debug
-lBarCModule

But in both cases, I couldn't call a function defined inside my C Module.

1> import BarCModule
2> say_hi
error: Couldn't lookup symbols:
  _say_hi

Regards,
Bhargav Gurlanka


(Daniel Dunbar) #2

Unfortunately, the error reporting isn't very good when LLVM tries to load a library (inside the JIT) and fails.

This is probably worth reporting as a bug. I'm not sure offhand and actually investigating this currently requires digging in to what is actually happening under the covers between the Swift importer and the LLVM JIT.

- Daniel

···

On May 25, 2016, at 12:38 PM, bhargav gurlanka via swift-build-dev <swift-build-dev@swift.org> wrote:

Hi,

I have a swift package with following layout:
.
├── Package.swift
├── Sources
│ ├── BarCModule
│ │ ├── Bar.c
│ │ └── include
│ │ └── Bar.h
│ └── MyApp
│ └── main.swift
└── Tests

After running swift build, I can see libBarCModule.so under .build/debug. How can I use this library in REPL?

I've tried:

$ swift -I.build/debug -L.build/debug -lBarCModule
$ LD_LIBRARY_PATH=.build/debug swift -I.build/debug -L.build/debug -lBarCModule

But in both cases, I couldn't call a function defined inside my C Module.

1> import BarCModule
2> say_hi
error: Couldn't lookup symbols:
  _say_hi

Regards,
Bhargav Gurlanka
_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev


(Ankit Agarwal) #3

I got this working by generating dylib for the C source
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html

Steps:

$ cd Fixtures/ClangModules/CLibrarySources/Sources
$ swift build
$ cp ../.build/debug/CLibrarySources.build/module.modulemap . #copy the
module map
$ clang -dynamiclib -std=gnu99 Foo.c -current_version 1.0
-compatibility_version 1.0 -fvisibility=hidden -o libFoo.dylib
$ swift -I include -L. -lFoo
1> import CLibrarySources
2> foo()
$R0: Int32 = 5

···

On Thu, May 26, 2016 at 1:13 AM, Daniel Dunbar via swift-build-dev < swift-build-dev@swift.org> wrote:

Unfortunately, the error reporting isn't very good when LLVM tries to load
a library (inside the JIT) and fails.

This is probably worth reporting as a bug. I'm not sure offhand and
actually investigating this currently requires digging in to what is
actually happening under the covers between the Swift importer and the LLVM
JIT.

- Daniel

On May 25, 2016, at 12:38 PM, bhargav gurlanka via swift-build-dev < > swift-build-dev@swift.org> wrote:

Hi,

I have a swift package with following layout:
.
├── Package.swift
├── Sources
│ ├── BarCModule
│ │ ├── Bar.c
│ │ └── include
│ │ └── Bar.h
│ └── MyApp
│ └── main.swift
└── Tests

After running swift build, I can see libBarCModule.so under .build/debug.
How can I use this library in REPL?

I've tried:

$ swift -I.build/debug -L.build/debug -lBarCModule
$ LD_LIBRARY_PATH=.build/debug swift -I.build/debug -L.build/debug
-lBarCModule

But in both cases, I couldn't call a function defined inside my C Module.

1> import BarCModule
2> say_hi
error: Couldn't lookup symbols:
  _say_hi

Regards,
Bhargav Gurlanka
_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev

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

--
Ankit


(Ankit Agarwal) #4

This seems to be the problem why REPL is not linking the C shared libs ->
https://bugs.swift.org/browse/SR-1624

···

On Thu, May 26, 2016 at 2:13 AM, Ankit Agarwal <ankit@ankit.im> wrote:

I got this working by generating dylib for the C source
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html

Steps:

$ cd Fixtures/ClangModules/CLibrarySources/Sources
$ swift build
$ cp ../.build/debug/CLibrarySources.build/module.modulemap . #copy the
module map
$ clang -dynamiclib -std=gnu99 Foo.c -current_version 1.0
-compatibility_version 1.0 -fvisibility=hidden -o libFoo.dylib
$ swift -I include -L. -lFoo
1> import CLibrarySources
2> foo()
$R0: Int32 = 5

On Thu, May 26, 2016 at 1:13 AM, Daniel Dunbar via swift-build-dev < > swift-build-dev@swift.org> wrote:

Unfortunately, the error reporting isn't very good when LLVM tries to
load a library (inside the JIT) and fails.

This is probably worth reporting as a bug. I'm not sure offhand and
actually investigating this currently requires digging in to what is
actually happening under the covers between the Swift importer and the LLVM
JIT.

- Daniel

On May 25, 2016, at 12:38 PM, bhargav gurlanka via swift-build-dev < >> swift-build-dev@swift.org> wrote:

Hi,

I have a swift package with following layout:
.
├── Package.swift
├── Sources
│ ├── BarCModule
│ │ ├── Bar.c
│ │ └── include
│ │ └── Bar.h
│ └── MyApp
│ └── main.swift
└── Tests

After running swift build, I can see libBarCModule.so under .build/debug.
How can I use this library in REPL?

I've tried:

$ swift -I.build/debug -L.build/debug -lBarCModule
$ LD_LIBRARY_PATH=.build/debug swift -I.build/debug -L.build/debug
-lBarCModule

But in both cases, I couldn't call a function defined inside my C Module.

1> import BarCModule
2> say_hi
error: Couldn't lookup symbols:
  _say_hi

Regards,
Bhargav Gurlanka
_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev

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

--
Ankit

--
Ankit