Skip to content

Commit e86e41e

Browse files
authored
Update README to show how to call Rust code via GHCi (#12)
1 parent d78e6bd commit e86e41e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,57 @@ Linking /Users/yvan/demo/dist-newstyle/build/aarch64-osx/ghc-9.0.2/test-0.1.0.0/
245245
Hello, Rust 🦀!
246246
```
247247

248+
Now let's see if we can use the GHCi repl to call the functions defined in Rust.
249+
250+
```text
251+
λ> withCString "aaa" hello
252+
ghc-9.4.8: ^^ Could not load '__c_hello', dependency unresolved. See top entry above.
253+
254+
255+
GHC.ByteCode.Linker: can't find label
256+
During interactive linking, GHCi couldn't find the following symbol:
257+
__c_hello
258+
This may be due to you not asking GHCi to load extra object files,
259+
archives or DLLs needed by your current session. Restart GHCi, specifying
260+
the missing library using the -L/path/to/object/dir and -lmissinglibname
261+
flags, or simply by naming the relevant files on the GHCi command line.
262+
Alternatively, this link failure might indicate a bug in GHCi.
263+
If you suspect the latter, please report this as a GHC bug:
264+
https://www.haskell.org/ghc/reportabug
265+
```
266+
267+
It seems like GHCi doesn't know how to link the library containing external
268+
functions. To fix this we first need to change the type of the Rust crate to
269+
`cdylib` in `Cargo.toml`. The reason why we need to do this is that GHCi can
270+
only load dynamic libraries, not static ones.
271+
272+
```toml
273+
[lib]
274+
crate-type = ["cdylib"]
275+
```
276+
Now we need to tell GHCi explicitly where to look for those libraries. We can
277+
do that by specifying the path in a `.ghci` file at the root of the project.
278+
279+
```ghci
280+
:set -Ltarget/debug -lgreetings
281+
```
282+
283+
After rebuilding the project with the necessary changes we can try once again:
284+
285+
```text
286+
$ cabal repl
287+
Build profile: -w ghc-9.4.8 -O1
288+
In order, the following will be built (use -v for more details):
289+
- greetings-0.1.0 (ephemeral targets)
290+
Preprocessing library for greetings-0.1.0..
291+
GHCi, version 9.4.8: https://www.haskell.org/ghc/ :? for help
292+
Loaded GHCi configuration from /path/to/project/greetings/.ghci
293+
[1 of 1] Compiling Greetings ( src/Greetings.hs, interpreted )
294+
Ok, one module loaded.
295+
λ> withCString "Rust" hello
296+
Hello, Rust!
297+
```
298+
248299
That's all folks! Happy hacking 🙂
249300

250301
## Nix support

src/main.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,57 @@
243243
//! Hello, Rust 🦀!
244244
//! ```
245245
//!
246+
//! Now let's see if we can use the GHCi repl to call the functions defined in Rust.
247+
//!
248+
//! ```text
249+
//! λ> withCString "aaa" hello
250+
//! ghc-9.4.8: ^^ Could not load '__c_hello', dependency unresolved. See top entry above.
251+
//!
252+
//!
253+
//! GHC.ByteCode.Linker: can't find label
254+
//! During interactive linking, GHCi couldn't find the following symbol:
255+
//! __c_hello
256+
//! This may be due to you not asking GHCi to load extra object files,
257+
//! archives or DLLs needed by your current session. Restart GHCi, specifying
258+
//! the missing library using the -L/path/to/object/dir and -lmissinglibname
259+
//! flags, or simply by naming the relevant files on the GHCi command line.
260+
//! Alternatively, this link failure might indicate a bug in GHCi.
261+
//! If you suspect the latter, please report this as a GHC bug:
262+
//! https://www.haskell.org/ghc/reportabug
263+
//! ```
264+
//!
265+
//! It seems like GHCi doesn't know how to link the library containing external
266+
//! functions. To fix this we first need to change the type of the Rust crate to
267+
//! `cdylib` in `Cargo.toml`. The reason why we need to do this is that GHCi can
268+
//! only load dynamic libraries, not static ones.
269+
//!
270+
//! ```toml
271+
//! [lib]
272+
//! crate-type = ["cdylib"]
273+
//! ```
274+
//! Now we need to tell GHCi explicitly where to look for those libraries. We can
275+
//! do that by specifying the path in a `.ghci` file at the root of the project.
276+
//!
277+
//! ```ghci
278+
//! :set -Ltarget/debug -lgreetings
279+
//! ```
280+
//!
281+
//! After rebuilding the project with the necessary changes we can try once again:
282+
//!
283+
//! ```text
284+
//! $ cabal repl
285+
//! Build profile: -w ghc-9.4.8 -O1
286+
//! In order, the following will be built (use -v for more details):
287+
//! - greetings-0.1.0 (ephemeral targets)
288+
//! Preprocessing library for greetings-0.1.0..
289+
//! GHCi, version 9.4.8: https://www.haskell.org/ghc/ :? for help
290+
//! Loaded GHCi configuration from /path/to/project/greetings/.ghci
291+
//! [1 of 1] Compiling Greetings ( src/Greetings.hs, interpreted )
292+
//! Ok, one module loaded.
293+
//! λ> withCString "Rust" hello
294+
//! Hello, Rust!
295+
//! ```
296+
//!
246297
//! That's all folks! Happy hacking 🙂
247298
//!
248299
//! ## Nix support

0 commit comments

Comments
 (0)