Skip to content

Can't use function pointers in C++11 code #64

Closed
@stephenpascoe

Description

@stephenpascoe

I can't get inline-c-cpp to compile if I use the funCtx to try and call Haskell from C++. The problem seems to be I can't specify exactly when to use the "-std=c++11" switch in Cabal.

Below is a minimal file which exposes the problem. My system is:

  • OSX El Capitan
  • stack v1.6.3, resolver: nightly-2018-01-02 (ghc 8.2.2)

src/Lib.hs

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Lib
    ( someFunc
    , someFunc2
    ) where

import qualified Language.C.Inline.Cpp as C
import Data.Monoid
import Foreign.C.Types (CDouble)

C.context $ C.cppCtx <> C.funCtx

C.include "<iostream>"


-- This snippet will only compile WITH "ghc-options: -optc-std=c++11"
-- Simulate including a header file with a C++ enum
C.verbatim "enum class MyEnum : char { OK = 0, FAIL = 1 };"

-- This function will only compile WITHOUT "ghc-options: -optc-std=c++11"
someFunc :: CDouble -> CDouble
someFunc x = let f x = x + (fromRational 42.0) in
  [C.pure| double { $fun:(double (*f)(double))($(double x)) } |]


-- This function compiles regardless of whether "ghc-options: -optc-std=c++11" is set
someFunc2 :: IO ()
someFunc2 = [C.block| void { std::cout << "Hello World!" << std::endl; } |]

package.yaml

library:
  source-dirs: src
  extra-libraries: stdc++
  ghc-options:   -optc-std=c++11

If I compile without -std=c++11 I get error: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
If I compile with -std=c++11 I get

Building library for inline-cpp-eg-0.1.0.0..
[2 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.0.1.1/build/Lib.o )
error: invalid argument '-std=c++11' not allowed with 'C/ObjC'
`gcc' failed in phase `C Compiler'. (Exit code: 1)

Strangely someFunc2 will compile in either case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions