FAQ
I'm trying to use swig to interface between go and a c++ package. I'm
having a problem using strings. Swig doesn't seem to be generating proper
interface code. Here's my test case:

*st.i

*%module st
%{
#include <string>
extern void pinput(const std::string& pstring);
%}
%include <std_string.i>
#include <string>
void pinput(const std::string& pstring);
*
st.cxx

*#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

void pinput(const std::string& pstring) {
   std::cout<<pstring;
}
*
stmain.go

*package main
import (
        "st"
)

func main() {
      st.Pinput("This is a test.")
}
*
Makefile

*SWIG=/usr/bin/swig

SWIGOBJS = st_gc.c st_wrap.cxx st.go

all: st.so st.a

$(SWIGOBJS): st.i
     $(SWIG) -go -c++ -intgosize 64 st.i

st.o: st.cxx
     g++ -c -fpic -g -o st.o st.cxx

st_wrap.o: st_wrap.cxx
     g++ -c -fpic -g -o st_wrap.o st_wrap.cxx

st.so: st_wrap.o st.o
     g++ -shared st_wrap.o st.o -o st.so

st_gc.6: st_gc.c
     go tool 6c -I `go env GOROOT`/pkg/`go env GOOS`_`go env GOARCH` st_gc.c

st.6: st.go
     go tool 6g st.go

st.a: st.6 st_gc.6
     go tool pack grc st.a st.6 st_gc.6

test: stmain

stmain: stmain.6
     go tool 6l -L . -r . -o stmain stmain.6

stmain.6: stmain.go
     go tool 6g -I . stmain.go
*
gdb output

*[[email protected] swigtest]$ gdb stmain
GNU gdb (GDB) Fedora (7.6-30.fc19)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/dave/go/src/swigtest/stmain...done.
warning: File "/usr/share/golang/src/pkg/runtime/runtime-gdb.py"
auto-loading ha
s been declined by your `auto-load safe-path' set to
"$debugdir:$datadir/auto-lo
ad:/usr/bin/mono-gdb.py".
To enable execution of this file add
     add-auto-load-safe-path /usr/share/golang/src/pkg/runtime/runtime-gdb.py
line to your configuration file "/home/dave/.gdbinit".
To completely disable this security protection add
     set auto-load safe-path /
line to your configuration file "/home/dave/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the
shell:
     info "(gdb)Auto-loading safe path"
(gdb) break st.Pinput
Breakpoint 1 at 0x419b20: file /home/dave/go/src/swigtest/st.go, line 36.
(gdb) run
Starting program: /home/dave/go/src/swigtest/stmain
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7fffe7d5d700 (LWP 7772)]

Breakpoint 1, st.Pinput (arg1=...) at /home/dave/go/src/swigtest/st.go:36
36 func Pinput(arg1 string) {
(gdb) p arg1
$1 = 0x42e8b0 "This is a test."
(gdb) step
37 _swig_wrap_pinput(arg1)
(gdb) p arg1
$2 = 0x42e8b0 "This is a test."
(gdb) step
st._swig_wrap_pinput (p=void) at /home/dave/go/src/swigtest/st_gc.c:28
28 ·_swig_wrap_pinput(struct {
(gdb) p p
$3 = void
(gdb)
*
*

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Jul 24, 2013 at 4:03 pm

    On Wed, Jul 24, 2013 at 7:50 AM, David Mackay wrote:
    I'm trying to use swig to interface between go and a c++ package. I'm
    having a problem using strings. Swig doesn't seem to be generating proper
    interface code. Here's my test case:
    Your test case works fine if I change pinput to
         std::cout<<pstring << std::endl;
    Writing the newline flushes std::cout. What you've identified is that
    the flush normally done at the end of a C++ program is not occurring.
    Probably C++ destructors are not being run when the Go program exits.
    That is a bug though I'm not sure how to fix it. Please open an issue
    for that with a small test case. Thanks.
    st._swig_wrap_pinput (p=void) at /home/dave/go/src/swigtest/st_gc.c:28
    28 ·_swig_wrap_pinput(struct {
    (gdb) p p
    $3 = void
    There is no debug info for local variables in C code compiled by 6c.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 24, '13 at 2:50p
activeJul 24, '13 at 4:03p
posts2
users2
websitegolang.org

2 users in discussion

David Mackay: 1 post Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2023 Grokbase