Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:45:53+00:00 2026-05-22T16:45:53+00:00

i need a example how to use hsc2hs, i thought that when i write

  • 0

i need a example how to use hsc2hs, i thought that when i write a header file like:

// foo.h
#define PI 3.14159

typedef struct {
    int i1;
    int i2;
} foo;

struct foo2 {
    int i1;
    int i2;
};

int fooFkt(foo f);

and then create a hsc file like:

import Foreign
import Foreign.C

#include "foo.h"

use hsc2hs:

{-# INCLUDE "foo.h" #-}
{-# LINE 1 "test.hsc" #-}
import Foreign
{-# LINE 2 "test.hsc" #-}
import Foreign.C


{-# LINE 5 "test.hsc" #-}

i dont understand it, i thought hat hsc2hs will import all needed things for me like the PI

who can give me a better example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:45:54+00:00Added an answer on May 22, 2026 at 4:45 pm

    Following RWH ch 17, you need to introduce something that binds the C PI value to a symbol in Haskell.

    For example:

    {-# LANGUAGE ForeignFunctionInterface #-}
    {-# LANGUAGE CPP                      #-}
    
    import Foreign
    import Foreign.C
    
    -- helpers
    import Control.Applicative
    import Control.Monad
    
    #include "foo.h"
    
    -- binding a symbol
    
    -- Note that hsc2hs can't marshal floating point CPP values, so this will be "3"
    -- c_pi :: CInt
    -- c_pi = #const PI
    
    -- Instead, bind to a function that returns a CFloat
    pi' :: Float
    pi' = realToFrac c_pi
    
    foreign import ccall unsafe "my_pi" c_pi :: CFloat
    

    And while we’re here, we can also marshal the struct to and from Haskell:

    -- Marshalling for the struct foo
    
    data Foo = Foo { i1, i2 :: Int }
        deriving Show
    
    -- Define how to marshal foo structs
    
    instance Storable Foo where
        sizeOf    _ = #{size foo}
    
        alignment _ = alignment (undefined :: CInt)
    
        poke p foo  = do
            #{poke foo, i1} p $ i1 foo
            #{poke foo, i2} p $ i2 foo
    
        peek p = return Foo
                  `ap` (#{peek foo, i1} p)
                  `ap` (#{peek foo, i2} p)
    

    And bind to fooFkt function:

    -- Import the function too
    
    foreign import ccall "foo.h fooFkt"
        c_fooFkt :: Ptr Foo -> IO CInt
    
    -- Marshal data to and from C
    
    fooFkt :: Foo -> IO Int
    fooFkt f = fromIntegral <$> with f c_fooFkt
    

    Done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a basic example of how to use the IComparable interface so that
I need to explain myself why I do not use static methods/propertis. For example,
I need an example algorithm that will draw pixels one at a time on
I need an example on how to use a comparable class on a HashSet
I want to build some libraries into executables that I need to use with
I noticed that I'm often in the need of a container class. For example
(ns db-example (:use [clojure.contrib.sql :only (with-connection with-query-results)] ) (:import (java.sql DriverManager))) ;; need this
I need simple example to use ISerializable,IEnumerable,IList with Generics efficiently. Also wish to know
I need a example of how to use CGPoint with a if statement to
How do I use the counter to trigger sprites? Need an example or idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.