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 6056199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:20:59+00:00 2026-05-23T08:20:59+00:00

I need to build some C code and then reference that C code via

  • 0

I need to build some C code and then reference that C code via the FFI. I would like to use my binding from inside ghci on osx. On of my constraints is that I cannot just hand the C sources to ghc in the .cabal file. This is due to a limitation with ghc/cabal that may be fixed in the next release of ghc (but I want my code to work now and on older releases). See this bug for details.

The gist of that bug is that the C code needs to be compiled with some Objective-C modules and ghc misinterprets those as linker scripts. I’ve tried many things and building the files myself with a makefile is the only thing that has worked. Really, this shouldn’t be an issue though because it should be the same as if I decided to use a external C library that I didn’t build myself. For the sake of this issue, let’s pretend it’s a separate C library that I can easily rebuild with different options.

If I build the C library as a .a, then ghci comlains that it cannot open the .dylib. My first question is: Why does ghci need a .dylib and does it really use it?

When I build a dylib I get a segfault when loading the code into ghci.

Keep in mind, this binding works already on other platforms, both linux and windows, and the binding works fine on osx when I’m compiling instead of using ghci. This problem specific to the osx/ghci combo.

In that trace above, I’m using gdb but it crashes regardless of whether I use gdb. I tracked it down to the lines that cause the crash:

void _glfwClearWindowHints( void )
{
    memset( &_glfwLibrary.hints, 0, sizeof( _glfwLibrary.hints ) );
}

The trouble maker is that memset line, well actually the problem is that when running inside ghci writing to the hints structure of _glfwLibrary is a memory access violation. The hints struct is simply a bunch of ints. It’s very flat and simple, and thus I think the problem is an issue either with how I’m linking things or with the way ghci is loading the code.

Here are the bits of my makefile that I use to build the dylib and the .a:

GCCFLAGS  := $(shell ghc --info | ghc -e "fmap read getContents >>=   \
             putStrLn . unwords . read . Data.Maybe.fromJust . lookup \
             \"Gcc Linker flags\"")
FRAMEWORK := -framework Cocoa -framework OpenGL
GLFW_FLAG := $(GCCFLAGS) -O2 -fno-common -Iglfw/include -Iglfw/lib    \
             -Iglfw/lib/cocoa $(CFLAGS)

all: $(BUILD_DIR)/static/libglfw.a $(BUILD_DIR)/dynamic/libglfw.dylib

$(BUILD_DIR)/dynamic/libglfw.dylib: $(OBJS)
  $(CC) -dynamiclib -Wl,-single_module -compatibility_version 1       \
        -current_version 1                                            \
        $(GLFW_FLAG) -o $@ $(OBJS) $(GLFW_SRC) $(FRAMEWORK)

$(BUILD_DIR)/static/libglfw.a: $(OBJS)
  ar -rcs $@ $(OBJS)

Most of the flags are taken straight from the GLFW Makefile so I think they should be correct for that library.

The first line looks a bit weird but it’s the solution I used for this problem.

Platform details:

  • OSX 10.6.6
  • x86_64
  • 4 cores
  • GHC version 7.0.3 installed via Haskell Platform installer
  • Source repo: https://github.com/dagit/GLFW-b

Edit: Here are my questions:

  • Should this work with ghci?
  • If so, what am I doing wrong or how can I fix the crash?
  • Can I just get by with the static .a version of the library with ghci?
  • 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-23T08:21:00+00:00Added an answer on May 23, 2026 at 8:21 am

    Initial Questions

    Should this work with ghci?
    If so, what am I doing wrong or how can I fix the crash?

    On OSX 10.6.7 (using the Haskell Platform /w GHC 7.0.2) I could load your built shared lib into ghci as follows:

    ➜  GLFW-b git:(master) ✗ ghci dist/build/Graphics/UI/GLFW.hs -Lbuild/dynam
    ic -lglfw                                                                 
    GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help             
    Loading package ghc-prim ... linking ... done.                            
    Loading package integer-gmp ... linking ... done.                         
    Loading package base ... linking ... done.                                
    Loading package ffi-1.0 ... linking ... done.                             
    Loading object (dynamic) glfw ... done                                    
    final link ... done                                                       
    [1 of 1] Compiling Graphics.UI.GLFW ( dist/build/Graphics/UI/GLFW.hs, inte
    rpreted )                                                                 
    Ok, modules loaded: Graphics.UI.GLFW.                                     
    *Graphics.UI.GLFW> initialize                                             
    True  
    

    Note: I built the glfw libs using your provided Makefile, and additionally used your .cabal file to process src/Graphics/UI/GLFW.hsc and build dist/build/Graphics/UI/GLFW.hs (i.e. I’d previously run cabal configure/build).

    Can I just get by with the static .a version of the library with ghci?

    Yes, support for loading static libs was included in GHC 7.0.2 (GHC Manual). compiler/ghci/Linker.lhs is a great read, and will give you a high-level sense of how ghci decides what to make of the command line arguments passed to it. Additionally, when navigating various platform support issues, I’ve found this documentation exceedingly useful.

    Linking static archives with ghci.

    As of writing, line 1113 of compiler/ghci/Linker.hs demonstrates that ghci currently requires that static archives be built by the package system (i.e. named HSlibname.a)

    locateOneObj :: [FilePath] -> String -> IO LibrarySpec                        
    locateOneObj dirs lib                                                         
      | not ("HS" `isPrefixOf` lib)                                               
        -- For non-Haskell libraries (e.g. gmp, iconv) we assume dynamic library  
      = assumeDll                                                                 
      | not isDynamicGhcLib                                                       
        -- When the GHC package was not compiled as dynamic library               
        -- (=DYNAMIC not set), we search for .o libraries or, if they             
        -- don't exist, .a libraries.                                             
      = findObject `orElse` findArchive `orElse` assumeDll           
    

    Further investigation of cmd line argument parsing indicates that libraries specified are collected at line 402 in the reallyInitDynLinker function:

    ; classified_ld_inputs <- mapM classifyLdInput cmdline_ld_inputs
    

    where classifyLdInput is defined over

    classifyLdInput :: FilePath -> IO (Maybe LibrarySpec)
    classifyLdInput f
      | isObjectFilename f = return (Just (Object f))
      | isDynLibFilename f = return (Just (DLLPath f))
      | otherwise          = do
        hPutStrLn stderr ("Warning: ignoring unrecognised input `" ++ f ++ "'")
        return Nothing
    

    This means that outside of a package specification, there currently is no direct way to link an archive file in ghci (or said differently, there currently is no cmd-line argument to do so).

    Fixing your cabal package

    In your .cabal package specification, you are attempting to build two conflicting libraries:

    • A: link in a pre-built library (built according to your specification in Setup.hs and Makefile, and linked as per the extra-libraries and extra-lib-dirs directives)
    • B: build a new library inline (the c-sources and frameworks directives).

    A simple fix for the error above is to simply remove all directives enabling B when building for Mac OSX, as follows:

       include-dirs:
         glfw/include
         glfw/lib
    -  c-sources:
    -    glfw/lib/enable.c
    -    glfw/lib/fullscreen.c
    -    glfw/lib/glext.c
    -    glfw/lib/image.c
    -    glfw/lib/init.c
    -    glfw/lib/input.c
    -    glfw/lib/joystick.c
    -    glfw/lib/stream.c
    -    glfw/lib/tga.c
    -    glfw/lib/thread.c
    -    glfw/lib/time.c
    -    glfw/lib/window.c
    +    
    +  if !os(darwin)
    +    c-sources:
    +      glfw/lib/enable.c
    +      glfw/lib/fullscreen.c
    +      glfw/lib/glext.c
    +      glfw/lib/image.c
    +      glfw/lib/init.c
    +      glfw/lib/input.c
    +      glfw/lib/joystick.c
    +      glfw/lib/stream.c
    +      glfw/lib/tga.c
    +      glfw/lib/thread.c
    +      glfw/lib/time.c
    +      glfw/lib/window.c
    

    and

         if os(darwin)
    -      include-dirs:
    -        glfw/lib/cocoa
    -      frameworks:
    -        AGL
    -        Cocoa
    -        OpenGL
           extra-libraries: glfw
    -      extra-lib-dirs: build/static build/dynamic
    +      extra-lib-dirs: build/dynamic
    

    I’ve not tested anything beyond verifying that the following now works properly:

    ➜  GLFW-b git:(master) ✗ ghci                                      
    GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help      
    Loading package ghc-prim ... linking ... done.                     
    Loading package integer-gmp ... linking ... done.                  
    Loading package base ... linking ... done.                         
    Loading package ffi-1.0 ... linking ... done.                      
    Prelude> :m + Graphics.UI.GLFW                                     
    Prelude Graphics.UI.GLFW> initialize                               
    Loading package GLFW-b-0.0.2.6 ... linking ... done.               
    True                                                               
    Prelude Graphics.UI.GLFW>      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a class that will represent a row in some table
In one of my projects I need to build an ASP.NET page and some
I need to run some precompile steps before I build my project using FlexBuilder.
I need to build a managed DLL, targeted for x64, and expose it via
I need to build something that starts serving a H.264 encoded video to a
I need to build an XML document from a Java object hierarchy. Both the
I'm writing some code that needs to frequently append to the end of a
I need some help with re-working the logic on this php code. What I'd
When I use the classic gnu Make I put in post build actions like
Is there some sort of built-in authentication in WCF? I need to expose a

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.