|
243 | 243 | //! Hello, Rust 🦀!
|
244 | 244 | //! ```
|
245 | 245 | //!
|
| 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 | +//! |
246 | 297 | //! That's all folks! Happy hacking 🙂
|
247 | 298 | //!
|
248 | 299 | //! ## Nix support
|
|
0 commit comments