doubo3384 2015-02-18 20:45
浏览 135
已采纳

cgo:体系结构x86_64的未定义符号

I would like to call the go func from the C function space, but the program throws the build error.

example.go

package main

/*
#include "test.c"
*/
import "C"
import "fmt"

func Example() {
    fmt.Println("this is go")
    fmt.Println(C.GoString(C.myprint(C.CString("go!!"))))
}

// export receiveC              (remove the extra space between // and export)
func receiveC(msg *C.char) {
    fmt.Println(C.GoString(msg))
}

func main() {
    Example()
}

test.c

#include <stdio.h>

extern void receiveC(char *msg);

char* myprint(char *msg) {
    receiveC(msg);  // calling the exported go function
    return msg;     
    }

When I execute the command to run/build (go build or go run example.go or go build example.go) the program, it throws the error:

# github.com/subh007/goodl/cgo
Undefined symbols for architecture x86_64:
  "_receiveC", referenced from:
      _myprint in example.cgo2.o
      __cgo_6037ec60b2ba_Cfunc_myprint in example.cgo2.o
      _myprint in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am following the cgo slides to writing the program. Please let me know for any mistakes here.

Edit1: I am using OS-X 10.9 OS.

Edit2: I have one extra space between // export, there should be no space between // and export. But now I am getting the following error while building :

# github.com/subh007/goodl/cgo
duplicate symbol _myprint in:
    $WORK/github.com/subh007/goodl/cgo/_obj/_cgo_export.o
    $WORK/github.com/subh007/goodl/cgo/_obj/example.cgo2.o
duplicate symbol _receiver_go in:
    $WORK/github.com/subh007/goodl/cgo/_obj/_cgo_export.o
    $WORK/github.com/subh007/goodl/cgo/_obj/example.cgo2.o
duplicate symbol _myprint in:
    $WORK/github.com/subh007/goodl/cgo/_obj/_cgo_export.o
    $WORK/github.com/subh007/goodl/cgo/_obj/test.o
duplicate symbol _receiver_go in:
    $WORK/github.com/subh007/goodl/cgo/_obj/_cgo_export.o
    $WORK/github.com/subh007/goodl/cgo/_obj/test.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • 写回答

1条回答 默认 最新

  • dqask02082 2015-02-18 22:21
    关注

    Duplicate symbols are generated because I have included the the test.c directly to the go file. So the symbols are included twice.

    I think, the correct way of writing this code is (please comment if I am wrong) to:

    1. Define the header file (test.h):

      #ifndef TEST_H_
      #define TEST_H_
      
      char* myprint(char *msg);
      
      #endif
      
    2. Define the implementation file (test.c):

       #include <stdio.h>
       #include "test.h"
      
       extern void receiveC(char *msg);
      
       char* myprint(char *msg) {
           receiveC(msg);
           return msg;
        }
      
    3. Include the .h file to the go file (example.go) : package main

       /*
       #include "test.h"
       */
       import "C"
       import "fmt"
      
       func Example() {
            fmt.Println("this is go")
            fmt.Println(C.GoString(C.myprint(C.CString("go!!"))))
        }
      
        // make sure that there should be no space between the `//` and `export`
        //export receiveC
        func receiveC(msg *C.char) {
              fmt.Println(C.GoString(msg))
        }
      
        func main() {
             Example()
        }
      
    4. Build the program :

       go build
      
    5. run the generated executable (the executable file generate with the cgo name, need some investigation to find the reason).

       $./cgo
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用