chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] c++ example


From: David Dreisigmeyer
Subject: Re: [Chicken-users] c++ example
Date: Wed, 1 Dec 2010 08:23:40 -0500

This may be getting closer.  Here's my code again (the names are
slightly different from above):

**punCpp.cpp:

#include <iostream>
#include "punCpp.h"

using namespace std;

void punCpp (void)
{
  printf("Hello World!\n");
  return;
}

**pun_module.scm:

(module pun_module (test_pun)
        (import chicken scheme foreign)

        (foreign-declare "extern void punCpp (void);\n")
        
        (define pun_fun (foreign-lambda void "punCpp"))
        
        (define (test_pun)
          (pun_fun)))

**Then I ran:

new-host:chicken daviddreisigmeyer$ csc-4.6.0 -s -c++ pun_module.scm punCpp.cpp
Undefined symbols:
  "punCpp()", referenced from:
      f_34(long, long, long)in pun_module.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Error: shell command terminated with non-zero exit status 256: g++
pun_module.o punCpp.o -o pun_module.so -m64 -bundle
-L"/Users/daviddreisigmeyer/Programming/scheme/chicken-4.6.0//lib"
-lchicken -lm

**When I ran

csc-4.6.0 -c++ -t pun_module.scm

and looked at pun_module.cpp I found:

/* Generated from pun_module.scm by the CHICKEN compiler
   http://www.call-with-current-continuation.org
   2010-12-01 08:20
   Version 4.6.3
   macosx-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
   compiled 2010-11-29 on new-host.home (Darwin)
   command line: pun_module.scm -output-file pun_module.cpp -feature
chicken-scheme-to-c++
   used units: library eval
*/

#include "chicken.h"

extern void punCpp (void);


static C_PTABLE_ENTRY *create_ptable(void);
C_noret_decl(C_library_toplevel)
C_externimport void C_ccall C_library_toplevel(C_word c,C_word
d,C_word k) C_noret;
C_noret_decl(C_eval_toplevel)
C_externimport void C_ccall C_eval_toplevel(C_word c,C_word d,C_word k) C_noret;

static C_TLS C_word lf[2];
static double C_possibly_force_alignment;
static C_char C_TLS li0[]
C_aligned={C_lihdr(0,0,21),40,112,117,110,95,109,111,100,117,108,101,35,116,101,115,116,95,112,117,110,41,0,0,0};
static C_char C_TLS li1[]
C_aligned={C_lihdr(0,0,10),40,116,111,112,108,101,118,101,108,41,0,0,0,0,0,0};


/* from pun_module#pun_fun */
static C_word C_fcall stub6(C_word C_buf) C_regparm;
C_regparm static C_word C_fcall stub6(C_word C_buf){
C_word C_r=C_SCHEME_UNDEFINED,*C_a=(C_word*)C_buf;
punCpp();
return C_r;}
......





On Wed, Dec 1, 2010 at 7:46 AM, Thomas Chust <address@hidden> wrote:
> 2010/12/1 Alan Post <address@hidden>:
>> On Tue, Nov 30, 2010 at 11:18:49AM -0500, David Dreisigmeyer wrote:
>>> [...]
>>> When I try this:
>>>
>>> *pun_cpp.cpp:
>>>
>>> #include <iostream>
>>> using namespace std;
>>>
>>> void pun_cpp (void)
>>> {
>>>   printf("Hello World!");
>>>   return;
>>> }
>>>
>>> *pun_module.scm:
>>>
>>> (module pun_module (test_pun)
>>>       (import chicken scheme foreign)
>>>
>>>       (define pun_fun (foreign-lambda void "pun_cpp"))
>>>
>>>       (define (test_pun)
>>>         (pun_fun)))
>>>
>>> *compile:
>>>
>>> g++ -c pun_cpp.cpp && csc-4.6.0 -s -c++ pun_module.scm pun_cpp.o
>>>
>>> I get the following:
>>>
>>> new-host:chicken daviddreisigmeyer$ g++ -c pun_cpp.cpp && csc-4.6.0 -s
>>> -c++ pun_module.scm pun_cpp.o
>>> pun_module.cpp: In function ‘long int stub6(long int)’:
>>> pun_module.cpp:29: error: ‘pun_cpp’ was not declared in this scope
>>>
>>> Error: shell command terminated with non-zero exit status 256: g++
>>> pun_module.cpp -o pun_module.o -c -Wno-write-strings -no-cpp-precomp
>>> -fno-strict-aliasing -fwrapv -fno-common -DHAVE_CHICKEN_CONFIG_H -m64
>>> -DC_ENABLE_PTABLES -Os -fomit-frame-pointer -no-cpp-precomp -fPIC
>>> -DPIC -DC_SHARED
>>> -I"/Users/daviddreisigmeyer/Programming/scheme/chicken-4.6.0//include"
>>>
>>
>> I'm just guessing here, I don't know that much about FFI in chicken,
>> but I suspect you need to use:
>>
>> extern "C" void pun_cpp (void)
>>
>> when declaring a routine to be used from a C++ file by Chicken.
>> Otherwise the linker munges the name to support C++ function
>> overloading, and Chicken can't find it.
>> [...]
>
> Hello,
>
> for two reasons I would say that this guess is wrong: First of all, in
> the given example CHICKEN is invoked in C++ mode, which means the code
> generated by the CHICKEN compiler is passed through the system C++
> compiler to produce binaries — and if the C++ compiler wouldn't
> understand its own name mangling that would be quite strange. Second,
> the way C(++) compilation and linking works, it is extremely unlikely
> that a name mangling error should ever be reported by the compiler, as
> is the case for the error message in the given example, it would
> rather cause an undefined symbol error during linking.
>
> My own suspicion is that the C++ compiler does not allow implicit
> function prototypes (which is a Good Thing (TM) by the way ;-), so I
> would try to add a (foreign-declare "extern void pun_cpp (void);\n")
> before the definition of pun_fun in the CHICKEN module and try to
> compile and link again.
>
> Ciao,
> Thomas
>
>
> --
> When C++ is your hammer, every problem looks like your thumb.
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]