Gmail Calendar Documents Web Reader more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Problem creating a ForeignPtr
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Federico Zenith  
View profile  
 More options Jan 27, 8:28 pm
Newsgroups: comp.lang.haskell
Followup-To: comp.lang.haskell
From: Federico Zenith <non.mi...@mma.re>
Date: Thu, 28 Jan 2010 02:28:06 +0100
Local: Wed, Jan 27 2010 8:28 pm
Subject: Problem creating a ForeignPtr
Hi all,
I have this problem with the following snippet:

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

import Foreign.Ptr

foreign import ccall "stdlib.h &free"
   p_free :: FunPtr (Ptr a -> IO ())
---------------------------------
This is a standard example reported verbatim from the GHC documentation of FunPtr:
http://www.haskell.org/ghc/docs/6.10.4/html/libraries/base/Foreign-Pt...

Calling "ghci test.hs" returns an error:

---------------------------------
test.hs:5:0:
    Illegal foreign declaration: requires via-C or native code generation (-fvia-C)
    When checking declaration:
        foreign import ccall safe "static stdlib.h &free" p_free
          :: FunPtr (Ptr a -> IO ())
Failed, modules loaded: none.
---------------------------------

Starting GHCI with the suggested -fvia-C does not help at all. GHC is version 6.10.4.

Any idea how to fix this?

Cheers,
-Federico


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Workaround (was: Problem creating a ForeignPtr)" by Federico Zenith
Federico Zenith  
View profile  
 More options Jan 28, 4:35 pm
Newsgroups: comp.lang.haskell
Followup-To: comp.lang.haskell
From: Federico Zenith <non.mi...@mma.re>
Date: Thu, 28 Jan 2010 22:35:35 +0100
Local: Thurs, Jan 28 2010 4:35 pm
Subject: Workaround (was: Problem creating a ForeignPtr)

Federico Zenith wrote:
> I have this problem with the following snippet:

> ---------------------------------
> {-# LANGUAGE ForeignFunctionInterface #-}

  module Test where

> import Foreign.Ptr

> foreign import ccall "stdlib.h &free"
>    p_free :: FunPtr (Ptr a -> IO ())
> ---------------------------------

I found that if I compile it with GHC and _then_ launch GHCI it works,
though using GHCI directly will not work if something is changed in the file
and GHCI decides it must be recompiled.

$ ghc -c test.hs
$ ghci test.hs

Weird. I hope someone with my same problem finds this solution and is saved
some stress.

Cheers
-Federico


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Workaround" by Mark T. B. Carroll
Mark T. B. Carroll  
View profile  
 More options Jan 28, 4:42 pm
Newsgroups: comp.lang.haskell
From: "Mark T. B. Carroll" <Mark.Carr...@Aetion.com>
Date: Thu, 28 Jan 2010 16:42:45 -0500
Local: Thurs, Jan 28 2010 4:42 pm
Subject: Re: Workaround

Federico Zenith <non.mi...@mma.re> writes:
> Weird. I hope someone with my same problem finds this solution and is saved
> some stress.

Thank you very much for posting it.

Mark


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk Thierbach  
View profile  
 More options Jan 28, 5:04 pm
Newsgroups: comp.lang.haskell
From: Dirk Thierbach <dthierb...@usenet.arcornews.de>
Date: Thu, 28 Jan 2010 23:04:51 +0100
Local: Thurs, Jan 28 2010 5:04 pm
Subject: Re: Workaround

Ok, exercise for the reader: Have you figured out *why* it behaves this
way? (Hint: GHCI *interprets*, GHC *compiles*. Now combine this information
with "via-C".)

- Dirk


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Federico Zenith  
View profile  
 More options Feb 1, 3:56 pm
Newsgroups: comp.lang.haskell
Followup-To: comp.lang.haskell
From: Federico Zenith <non.mi...@mma.re>
Date: Mon, 01 Feb 2010 21:56:09 +0100
Local: Mon, Feb 1 2010 3:56 pm
Subject: Re: Workaround

Dirk Thierbach wrote:
> Ok, exercise for the reader: Have you figured out *why* it behaves this
> way? (Hint: GHCI *interprets*, GHC *compiles*. Now combine this
> information with "via-C".)

Well it stays weird since GHCI makes use of the compiled code it finds (if
it finds it) instead of interpreting the file.
The error message could have been a bit more helpful, anyway.
Also, the "via-C" part was not required when compiling with GHC.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk Thierbach  
View profile  
 More options Feb 1, 4:51 pm
Newsgroups: comp.lang.haskell
From: Dirk Thierbach <dthierb...@usenet.arcornews.de>
Date: Mon, 1 Feb 2010 22:51:59 +0100
Local: Mon, Feb 1 2010 4:51 pm
Subject: Re: Workaround

Federico Zenith <non.mi...@mma.re> wrote:
> Dirk Thierbach wrote:
>> Ok, exercise for the reader: Have you figured out *why* it behaves this
>> way? (Hint: GHCI *interprets*, GHC *compiles*. Now combine this
>> information with "via-C".)
> Well it stays weird since GHCI makes use of the compiled code it finds (if
> it finds it) instead of interpreting the file.

Correct. But that's not weird, that's the intended behaviour: If a
current object file is around, why not use it instead of doing the
compilation all over?

> The error message could have been a bit more helpful, anyway.
> Also, the "via-C" part was not required when compiling with GHC.

Yes. If you look up -fvia-C in the GHC manual, section flag reference,
you're redirected to section 4.10.6 (Options affecting code generation).
There you'll find, near -fvia-C:

: -fobject-code
:
: Generate object code. This is the default outside of GHCi, and can
: be used with GHCi to cause object code to be generated in preference
: to bytecode.
:
: -fbyte-code  
:
: Generate byte-code instead of object-code. This is the default in
: GHCi. Byte-code can currently only be used in the interactive
: interpreter, not saved to disk. This option is only useful for
: reversing the effect of -fobject-code.

That explains why it works with GHC, but not with GHCI. And:

  Prelude> :l yyy.hs
  [1 of 1] Compiling Test             ( yyy.hs, interpreted )

  yyy.hs:7:0:
      Illegal foreign declaration: requires via-C or native code generation
      (-fvia-C)
      When checking declaration:
          foreign import ccall safe "static stdlib.h &free" p_free
            :: FunPtr (Ptr a -> IO ())
  Prelude> :set -fobject-code
  Prelude> :l yyy.hs
  [1 of 1] Compiling Test             ( yyy.hs, yyy.o )
  Ok, modules loaded: Test.

It works even in GHCI :-)

(Yes, you have to know what "native code generation" means, and
-fvia-C is a bit misleading, because it is a second requirement, which
is probably automatically set by the FFI option (I didn't check). But
at least it points you in the right direction.  RTFM helps, of
course. And it's not really surprising that FFI code does need C code
instead of bytecode).

- Dirk


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google