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

  • SEARCH
  • Home
  • 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 7042021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:07:03+00:00 2026-05-28T02:07:03+00:00

Let’s take a common example how a haskell function can be called from a

  • 0

Let’s take a common example how a haskell function can be called from a C function:

Haskell module:

{-# LANGUAGE ForeignFunctionInterface #-}

module Safe where

import Foreign.C.Types

fibonacci :: Int -> Int
fibonacci n = fibs !! n
    where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral

foreign export ccall fibonacci_hs :: CInt -> CInt

And C module:

#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Safe_stub.h"
extern void __stginit_Safe(void);
#endif
#include <stdio.h>

int main(int argc, char *argv[]) {
   int i;
   hs_init(&argc, &argv);
#ifdef __GLASGOW_HASKELL__
   hs_add_root(__stginit_Safe);
#endif

   i = fibonacci_hs(42);
   printf("Fibonacci: %d\n", i);

   hs_exit();
   return 0;
}

I compile and link it:

$ ghc -c -O Safe.hs
$ ghc test.c Safe.o Safe_stub.o -o test

That’s ok. But what if I need to import some library in haskell module? E.g., if I need to use bytestrings I should add “import Data.Bytestring.Char8” (this module is taken for an example and is not used in code):

{-# LANGUAGE ForeignFunctionInterface #-}

module Safe where

import Foreign.C.Types
import Data.Bytestring.Char8

fibonacci :: Int -> Int
fibonacci n = fibs !! n
    where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral

foreign export ccall fibonacci_hs :: CInt -> CInt

And that’s not ok, for now I get an error:

$ ...undefined reference to `__stginit_bytestringzm0zi9zi2zi0_DataziByteStringziChar8_'

All I’ve currently found on the problem is this:
a bug in GHC and as follows
changeset (more formal description of the bug)

As I use ghc-6.12.3, I already have this feature implemented. So I have no idea how to fix the problem.

Perhaps, it would be easier to make a shared library and dynamically link it with my C module?

  • 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-28T02:07:04+00:00Added an answer on May 28, 2026 at 2:07 am

    I don’t think that bug is related. Have you tried using --make?

    $ ghc -c -O Safe.hs
    $ ghc --make test.c Safe.o Safe_stub.o -o test
    

    Those errors are the kinds of errors you used to get1 when linking pure-Haskell code with package dependencies without using --make; GHC links in base by default, but if you want anything from another package that doesn’t work.

    You could also try specifying the packages explicitly if you want a more “manual” method:

    $ ghc -c -O Safe.hs
    $ ghc -package bytestring test.c Safe.o Safe_stub.o -o test
    

    1 Since GHC 7, --make has been the default.

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

Sidebar

Related Questions

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say, I have a .NET 2 installed. Can I programmatically install version 4
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say, I've got a XmlNode: <A>1</A> How to remove a text from it,
Let's say R is a mxn random matrix. How can generate R with each
Let's say I have an image called hello.png with dimensions 200x100 . I create

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.