Skip to content

cc-options not used when compiling capi wrapper files?

Summary

cc-options don't appear to be passed to C compiler when compiling capi wrapper functions.

Steps to reproduce

Main.hs

{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C.Types

foreign import capi "hello.h foo" foo :: CInt

main :: IO ()
main = print foo

hello.h

#ifdef HELLO
int foo();
#endif

hello.c

#ifdef HELLO
int foo() { return 1; }
#endif

hello.cabal

cabal-version: 2.2
name: hello
version: 0

executable hello
  build-depends: base
  main-is: Main.hs
  c-sources: hello.c
  cc-options: -DHELLO
  includes: hello.h

Output from cabal run:

[1 of 1] Compiling Main

/var/folders/0m/cpz59dyj3ts58crh9m1gbcyr0000gn/T/ghc58327_0/ghc_3.c:8:56: error:
     error: implicit declaration of function 'foo' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
  |
8 | HsInt32 ghczuwrapperZC0ZCmainZCMainZCfoo(void) {return foo();}
  |                                                        ^
HsInt32 ghczuwrapperZC0ZCmainZCMainZCfoo(void) {return foo();}
                                                       ^
1 error generated.
`gcc' failed in phase `C Compiler'. (Exit code: 1)

Expected behavior

I expect to have some int foo(); prototype in scope for the wrapper ghc_3.c file, so compilation should succeed and the program should print 1.

The program does work using ccall (maybe that's obvious), and it also works if I don't hide the function prototype behind a #ifdef HELLO guard.

Environment

  • GHC version used: 9.2.2
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information