Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
I'm using the latest tarball of gambit, compiled with --enable-cplusplus.
I compile foo.scm to C++ via:
gsc -link -flat foo.scm ==> foo.c and foo_.c
Then I want to compile these into object files and I run into trouble. g++ -fPIC -c foo.c outputs the following:
oo.c: In function ‘int ___H__20_foo_23_0(___processor_state_struct*)’: foo.c:119: error: ‘cpp_bar’ was not declared in this scope foo.c: At global scope: foo.c:156: warning: deprecated conversion from string constant to ‘char*’
Whereas
gcc compiles it fine. It must be a name-mangling issue, but it seems peculiar to me, given that I compiled gsc using g++. Moreover, I can compile if I add extern "C" {int cpp_bar(char*);} to foo.c. I picked this up from reading about someone who had similar problems a few years ago:
http://osdir.com/ml/lisp.scheme.gambit/2005-02/msg00037.html
I then compile and link like:
g++ -fPIC -c -D___DYNAMIC foo_.c foo.c g++ -fPIC -c -D___DYNAMIC foo.cpp -o cppfoo.o ld -G -o foo.o1 /usr/lib/libstdc++.so.6.0.9 /usr/lib/gcc/i486-linux-gnu/4.2/crtbegin.o cppfoo.o foo_.o foo.o
Unfortunately, this leads me here:
*** WARNING -- Could not find C function: "____20_foo_2e_o1" *** ERROR IN ##main -- .../foo.o1: undefined symbol: cpp_bar (load "foo.o1")
Can anyone offer me any pointers on how to proceed?
-John
Afficher les réponses par date
On Tue, Feb 24, 2009 at 05:41:24AM -0800, John Thompson wrote:
Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
Prototype in foo.h should be defined like:
extern "C" int cpp_bar(char *text);
or
extern "C" { int cpp_bar(char* text); … }
for a group of functions.
I was just running into the same issue:
does "g++ -Wno-write-strings ... your other options ..." get rid of the 'string constant -> char* deprecated' warning?
On Tue, Feb 24, 2009 at 5:41 AM, John Thompson bargarply@yahoo.com wrote:
Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
I'm using the latest tarball of gambit, compiled with --enable-cplusplus.
I compile foo.scm to C++ via:
gsc -link -flat foo.scm ==> foo.c and foo_.c
Then I want to compile these into object files and I run into trouble. g++ -fPIC -c foo.c outputs the following:
oo.c: In function ‘int ___H__20_foo_23_0(___processor_state_struct*)’: foo.c:119: error: ‘cpp_bar’ was not declared in this scope foo.c: At global scope: foo.c:156: warning: deprecated conversion from string constant to ‘char*’
Whereas gcc compiles it fine. It must be a name-mangling issue, but it seems peculiar to me, given that I compiled gsc using g++. Moreover, I can compile if I add extern "C" {int cpp_bar(char*);} to foo.c. I picked this up from reading about someone who had similar problems a few years ago:
http://osdir.com/ml/lisp.scheme.gambit/2005-02/msg00037.html
I then compile and link like:
g++ -fPIC -c -D___DYNAMIC foo_.c foo.c g++ -fPIC -c -D___DYNAMIC foo.cpp -o cppfoo.o ld -G -o foo.o1 /usr/lib/libstdc++.so.6.0.9 /usr/lib/gcc/i486-linux-gnu/4.2/crtbegin.o cppfoo.o foo_.o foo.o
Unfortunately, this leads me here:
*** WARNING -- Could not find C function: "____20_foo_2e_o1" *** ERROR IN ##main -- .../foo.o1: undefined symbol: cpp_bar (load "foo.o1")
Can anyone offer me any pointers on how to proceed?
-John
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On 24-Feb-09, at 8:41 AM, John Thompson wrote:
Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
In C++, prototypes are not optional (and if it works in C it is by accident). In the C++ code generated for foo.scm the C++ compiler has no idea what is the prototype of cpp_bar that you want to call. You might think that the c-lambda types are there to supply that information, but in fact the Gambit compiler simply generates a call to cpp_bar whose *actual* parameters are those of the c-lambda. By not automatically generating a prototype for cpp_bar it allows for cpp_bar to be a macro or a qualified name such as "cpp::bar" or inline code such as "___result = ___arg1[0];". So to solve your problem foo.scm should be
(c-declare #<<end-of-c-declare #include "foo.h" end-of-c-declare )
(define bar (c-lambda (char-string) int "cpp_bar"))
Note that you'd have to include foo.h if this was a C++ module, for exactly the same reasons, so it shouldn't be too surprising.
Marc
Hi Marc,
Thanks for the response. The include did indeed solve my compilation problems.
Now I'm compiling like this:
gsc -link -flat foo.scm g++ -Wno-write-strings -fPIC -c -D___DYNAMIC foo_.c foo.c g++ -Wno-write-strings -fPIC -c -D___DYNAMIC foo.cpp -o cppfoo.o ld -G -o foo.o1 cppfoo.o foo.o foo_.o
This leads to a run-time linking error of:
*** WARNING -- Could not find C function: "____20_foo_2e_o1" *** ERROR IN ##main -- .../foo.o1: undefined symbol: ____20_foo_2e_o1 (load "foo.o1")
Any idea what the issue is here?
-John
--- On Tue, 2/24/09, Marc Feeley feeley@iro.umontreal.ca wrote: From: Marc Feeley feeley@iro.umontreal.ca Subject: Re: [gambit-list] Difficulties linking against a C++ library To: bargarply@yahoo.com Cc: gambit-list@iro.umontreal.ca Date: Tuesday, February 24, 2009, 2:22 PM
On 24-Feb-09, at 8:41 AM, John Thompson wrote:
Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is
as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
In C++, prototypes are not optional (and if it works in C it is by accident). In the C++ code generated for foo.scm the C++ compiler has no idea what is the prototype of cpp_bar that you want to call. You might think that the c-lambda types are there to supply that information, but in fact the Gambit compiler simply generates a call to cpp_bar whose *actual* parameters are those of the c-lambda. By not automatically generating a prototype for cpp_bar it allows for cpp_bar to be a macro or a qualified name such as "cpp::bar" or inline code such as "___result = ___arg1[0];". So to solve your problem foo.scm should be
(c-declare #<<end-of-c-declare #include "foo.h" end-of-c-declare )
(define bar (c-lambda (char-string) int "cpp_bar"))
Note that you'd have to include foo.h if this was a C++ module, for exactly the same reasons, so it shouldn't be too surprising.
Marc
Figured it out due to this old message:
https://webmail.iro.umontreal.ca/pipermail/gambit-list/2004-October/000014.h...
-John
--- On Tue, 2/24/09, John Thompson bargarply@yahoo.com wrote: From: John Thompson bargarply@yahoo.com Subject: Re: [gambit-list] Difficulties linking against a C++ library To: "Marc Feeley" feeley@iro.umontreal.ca Cc: gambit-list@iro.umontreal.ca Date: Tuesday, February 24, 2009, 11:55 PM
Hi Marc,
Thanks for the response. The include did indeed solve my compilation problems.
Now I'm compiling like this:
gsc -link -flat foo.scm g++ -Wno-write-strings -fPIC -c -D___DYNAMIC foo_.c foo.c g++ -Wno-write-strings -fPIC -c -D___DYNAMIC foo.cpp -o cppfoo.o ld -G -o foo.o1 cppfoo.o foo.o foo_.o
This leads to a run-time linking error of:
*** WARNING -- Could not find C function: "____20_foo_2e_o1" *** ERROR IN ##main -- .../foo.o1: undefined symbol: ____20_foo_2e_o1 (load "foo.o1")
Any idea what the issue is here?
-John
--- On Tue, 2/24/09, Marc Feeley feeley@iro.umontreal.ca wrote: From: Marc Feeley feeley@iro.umontreal.ca Subject: Re: [gambit-list] Difficulties linking against a C++ library To: bargarply@yahoo.com Cc: gambit-list@iro.umontreal.ca Date: Tuesday, February 24, 2009, 2:22 PM
On 24-Feb-09, at 8:41 AM, John Thompson wrote:
Hi,
I'm trying to get a toy, loadable C++ library compiled. My setup is
as follows:
foo.scm:
(define bar (c-lambda (char-string) int "cpp_bar"))
foo.cpp:
int cpp_bar(char* text) { return 0; }
foo.h:
int cpp_bar(char* text);
In C++, prototypes are not optional (and if it works in C it is by accident). In the C++ code generated for foo.scm the C++ compiler has no idea what is the prototype of cpp_bar that you want to call. You might think that the c-lambda types are there to supply that information, but in fact the Gambit compiler simply generates a call to cpp_bar whose *actual* parameters are those of the c-lambda. By not automatically generating a prototype for cpp_bar it allows for cpp_bar to be a macro or a qualified name such as "cpp::bar" or inline code such as "___result = ___arg1[0];". So to solve your problem foo.scm should be
(c-declare #<<end-of-c-declare #include "foo.h" end-of-c-declare )
(define bar (c-lambda (char-string) int "cpp_bar"))
Note that you'd have to include foo.h if this was a C++ module, for exactly the same reasons, so it shouldn't be too surprising.
Marc
_______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list