3

I'm hacking around with someone else's code. They have several macros defined in a .h file (no code, just macros), and these macros are used in several Haskell files. What I'm missing is the step which allows this compilation. When I try to compile the Haskell file, I just get errors on the missing/undefined macros. Is there a GHC option that will use macros defined in a header file? I'm trying to do the same with GHCi.

I did attempt to just rename the .h file to .hs and import it in the files that use the macros, but I am apparently not allowed to export macros (and they are not exported automatically).

How can I use these macros when compiling with GHC(i)?

4
  • See these two pages in the GHC 6.6 docs: haskell.org/ghc/docs/6.6/html/users_guide/… and haskell.org/ghc/docs/6.6/html/users_guide/…. However this seems to be deprecated in newer GHC versions: haskell.org/ghc/docs/7.0.4/html/users_guide/… Commented Jun 23, 2014 at 16:10
  • I've used the FFI before, so I've seen that option. However, I'm not really intending to use the full FFI, we're talking about a couple of macros here. Also, including header files didn't seem to help, as the deprectation message indicates. Commented Jun 23, 2014 at 16:36
  • 3
    Add the CPP pragma to the top of your haskell file and add a #include foo.h line to the haskell file. The C preprocessor will expand the macros for you. Commented Jun 23, 2014 at 20:48
  • @vivian That's an answer! Thank you! It'd be interesting to know if there's a way to just have GHC make the macros visible to all files instead of having to manually include it in each file, but it sure beats copying all of the macro code! For example, you can use -DMY_MACRO=3 to define a global macro, why can't I make the entire header file a global macro? Commented Jun 23, 2014 at 21:47

1 Answer 1

3

Add the CPP pragma to the top of your haskell file and add a #include foo.h line to the haskell file. The C preprocessor will expand the macros for you.

See [1] for options like -Dsymbol=value which will define a macro for all the files that are compiled with that invocation of ghc.

[1] https://www.haskell.org/ghc/docs/latest/html/users_guide/options-phases.html

Sign up to request clarification or add additional context in comments.

1 Comment

I know about the -D flag for single macros, but my comment above was about making all of the macros in a .h file global in the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.