FAQ
(This is a repost of my question on Stack Overflow)

I'm trying to use swig to interface with c++, and I'm having problems using
a std::string& parameter. This is on Fedora 19 with golang 1.1-2, swig
2.0.10-1, and gcc 4.8.1-1.

I don't know C++. I do know C, and had hoped to wing it. At any rate,
_swig_wrap_pinput ends up with a null pointer.

What am I overlooking?

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 mainimport (
        "st")

func main() {
      myLit:="This is a test."
      st.Pinput(&myLit)}


*Makefile*

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
     /usr/lib64/golang/pkg/tool/linux_amd64/6c -I /usr/lib64/golang/pkg/linux_amd64 st_gc.c

st.6: st.go
     /usr/lib64/golang/pkg/tool/linux_amd64/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
     /usr/lib64/golang/pkg/tool/linux_amd64/6l stmain.6

stmain.6: stmain.go
     /usr/lib64/golang/pkg/tool/linux_amd64/6g stmain.go

**

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Jul 23, 2013 at 2:29 am

    On Mon, Jul 22, 2013 at 7:08 PM, David Mackay wrote:
    func main() {
    myLit:="This is a test."
    st.Pinput(&myLit)
    }
    I'm surprised this compiles at all. A C++ const std::string& argument
    should produce a Go function that expects a string argument, not a
    string*.

    And indeed when I try your test case I get this:

    stmain.go:8: cannot use &myLit (type *string) as type string in
    function argument

    I've attached the Makefile that I used, it's a version of yours that
    is more portable.

    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 23, '13 at 2:18a
activeJul 23, '13 at 2:29a
posts2
users2
websitegolang.org

2 users in discussion

David Mackay: 1 post Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2022 Grokbase