Skip to content

Commit 05ebcf9

Browse files
committed
Improve Haskell and C code advice
1 parent 84ef9c9 commit 05ebcf9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

doc/haskell_and_c_code.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ dependencies: base
119119
library:
120120
source-dirs: src
121121
# The Lib_stub.h header must be put by GHC somewhere where Cabal can find it.
122-
# This tells GHC to put it in the stubs directory of the project directory.
122+
# This tells GHC to put it in the autogen-stubs directory of the project
123+
# directory.
123124
ghc-options:
124125
- -stubdir autogen-stubs
125126

@@ -136,12 +137,13 @@ executables:
136137
main: main.c
137138
source-dirs: c-app
138139
ghc-options: -no-hs-main
139-
# This specifies that directory stubs should be searched for header files.
140+
# This specifies that directory autogen-stubs should be searched for header
141+
# files.
140142
include-dirs: autogen-stubs
141143
dependencies: c-example
142144
~~~
143145

144-
A `Lib.hs` module in directory `src`:
146+
A Haskell module souce file named `Lib.hs` in directory `src`:
145147
~~~haskell
146148
module Lib
147149
( myMax -- Exported only for the use of the 'Haskell' executable
@@ -153,7 +155,7 @@ myMax x1 x2 = if x1 > x2 then x1 else x2
153155
foreign export ccall myMax :: Int -> Int -> Int
154156
~~~
155157

156-
A Haskell module `Main.hs` in directory `app`:
158+
A Haskell module source file named `Main.hs` in directory `app`:
157159
~~~haskell
158160
module Main ( main ) where
159161

@@ -163,7 +165,7 @@ main :: IO ()
163165
main = print $ myMax 10 100
164166
~~~
165167

166-
A C source file `main.c` in directory `c-app`:
168+
A C source file named `main.c` in directory `c-app`:
167169
~~~c
168170
// Based in part on
169171
// https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/ffi.html#using-your-own-main
@@ -187,15 +189,22 @@ int main(int argc, char *argv[]) {
187189
// Use our foreign export from module Lib.hs ...
188190
printf("%lld\n", myMax(10,100));
189191

190-
// Deinitialise the Haskell system
192+
// De-initialise the Haskell system
191193
hs_exit();
192194
return 0;
193195
}
194196
~~~
195197
196-
The `foreign export` declaration in Haskell module `Lib.hs` will cause GHC to
198+
The `foreign export` declaration in Haskell module `Lib` will cause GHC to
197199
generate a 'stub' C header file named `Lib_stub.h`. The GHC option `-stubdir`
198-
will cause GHC to put that file in the specified directory (`autogen-stubs`).
200+
will cause GHC to put that file in the specified directory (`autogen-stubs`, in
201+
this example).
202+
203+
!!! info
204+
205+
If GHC's `-stubdir` option is omitted, GHC will put the generated C header
206+
file together with the other build artefacts for the module. However, that
207+
location cannot be specified reliably using the `install-dirs` key.
199208
200209
That generated C header file will have content like:
201210
~~~c
@@ -209,8 +218,8 @@ extern HsInt myMax(HsInt a1, HsInt a2);
209218
#endif
210219
~~~
211220

212-
The `include-dirs` key will cause the specified directory (`autogen-stubs`) to
213-
be searched for C header files.
221+
The `include-dirs` key will cause the specified directory (again,
222+
`autogen-stubs` in this example) to be searched for C header files.
214223

215224
The project's `stack.yaml` file only needs to identify a snapshot:
216225
~~~yaml

0 commit comments

Comments
 (0)