FAQ
Sorry.. I run the code but get the error

I don't understand two line in Makefile:
include $(GOROOT)/src/Make.inc
include $(GOROOT)/src/Make.pkg

and when I run "make" it occur No such file or directory
so I delete them.

then I run "make" and "go run main.go"
and get the follow error:

# command-line-arguments
./main.go:3 imported and not used: "cgotest"
./main.go:6 undefined: cgotest

PS. I'm a newer, sorry if i ask something stupid.
On Monday, January 31, 2011 6:48:40 AM UTC+8, Ostsol wrote:
On Jan 30, 2:03 pm, Gustavo Niemeyer wrote:
does cgo could not handle "c" forward declaration right.
I haven't noticed any issues with it. Do you have a self-contained
example to reproduce the problem?

--
Gustavo Niemeyerhttp://niemeyer.nethttp://
niemeyer.net/bloghttp://niemeyer.net/twitter

Here's one:

//testlib.h
#ifndef TESTLIB_H
#define TESTLIB_H

struct StructTest;

struct StructTest *NewStructTest(int num);
void DoStuff(struct StructTest *obj);

#endif //TESTLIB_H


//testlib.c
#include <stdio.h>
#include <stdlib.h>

struct StructTest {
int num;
};

struct StructTest *NewStructTest(int num) {
struct StructTest *s;
s = (struct StructTest*)malloc(sizeof(struct StructTest));
s->num = num;

return s;
}

void DoStuff(struct StructTest *obj) {
printf("StructTest obj -> num = %d\n", obj->num);
}


//cgotest.go
package cgotest

//#include "testlib.h"
import "C"

func DoStuff() {
var obj *C.StructTest
obj = C.NewStructTest(C.int(4))
C.DoStuff(obj)
}


//main.go
package main

import (
"cgotest"
)

func main() {
cgotest.DoStuff()
}


#Makefile
include $(GOROOT)/src/Make.inc

TARG=cgotest
CGOFILES=cgotest.go
CGO_OFILES=testlib.o

include $(GOROOT)/src/Make.pkg

CLEANFILES+=main

testlib: testlib.c testlib.h
gcc -c -fPIC -static testlib.c -o testlib.o

main: testlib install main.go
$(GC) main.go
$(LD) -o $@ main.$O


Oddly enough, it works without a problem if I don't declare the obj
variable in cgotest.go and simply use := to declare and initialize.
Any direct mention of C.StructTest in cgotest.go breaks the thing,
though.

-Daniel
--

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 6, '12 at 11:12p
activeDec 6, '12 at 11:12p
posts1
users1
websitegolang.org

1 user in discussion

xiaozhi Lin: 1 post

People

Translate

site design / logo © 2021 Grokbase