chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] c++ example


From: Thomas Chust
Subject: Re: [Chicken-users] c++ example
Date: Wed, 1 Dec 2010 13:46:47 +0100

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]