Golang new version for CGO support for a pit

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

File Test.h as follows

#ifndef _test_h#define _test_h#ifdef __cplusplusextern "C" {#endifextern void MyTest (); #ifdef __cplusplus} #endif #endif

File test.cc as follows

#include "_cgo_export.h" extern "C" {void MyTest () {test ();}}

File Test.go as follows

Package test/* #include "test.h" */import "C" import () func test () {c.mytest ()}//export testfunc Test () {println ("Hello")}

If you do go build-x you will see the following results

Work=/tmp/go-build845372545mkdir-p $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/mkdir-p $ work/_/home/pirate/programs/goclipse/workspace/v8/src/cd/home/pirate/programs/goclipse/workspace/v8/src/test/ Home/pirate/programs/go/pkg/tool/linux_amd64/cgo-objdir $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/ test/_obj/---I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.go/home/pirate/programs/go /pkg/tool/linux_amd64/6c-f-v-w-I $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-i/home/ Pirate/programs/go/pkg/linux_amd64-o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_ defun.6-d goos_linux-d goarch_amd64 $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_ Defun.cgcc-i. -g-o2-fpic-m64-pthread-print-libgcc-file-namegcc-i. -g-o2-fpic-m64-pthread-i $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-o $WORK/_/home/pirate /programs/goclipse/workspace/v8/Src/test/_obj/_cgo_main.o-c $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_main.cgcc-i. -g-o2-fpic-m64-pthread-i $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-o $WORK/_/home/pirate /programs/goclipse/workspace/v8/src/test/_obj/_cgo_export.o-c $WORK/_/home/pirate/programs/goclipse/workspace/ V8/src/test/_obj/_cgo_export.cgcc-i. -g-o2-fpic-m64-pthread-i $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-o $WORK/_/home/pirate /programs/goclipse/workspace/v8/src/test/_obj/test.cgo2.o-c $WORK/_/home/pirate/programs/goclipse/workspace/v8/ Src/test/_obj/test.cgo2.cgcc-i. -g-o2-fpic-m64-pthread-i $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-o $WORK/_/home/pirate /programs/goclipse/workspace/v8/src/test/_obj/test.o-c./test.cg++-I. -g-o2-fpic-m64-pthread-i $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/-o $WORK/_/home/pirate /programs/goclipse/workspace/v8/src/test/_obj/test.cc.o-c./test.ccg++-I. -g-o2-fpic-m64-pthread-o $WORK/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/_cgo_.o $WORK/_/home/ PIRATE/PROGRAMS/GOCLIPSE/WORKSPACE/V8/SRC/TEST/_OBJ/_CGO_MAIN.O $WORK/_/home/pirate/programs/goclipse/workspace /V8/SRC/TEST/_OBJ/_CGO_EXPORT.O $WORK/_/HOME/PIRATE/PROGRAMS/GOCLIPSE/WORKSPACE/V8/SRC/TEST/_OBJ/TEST.CGO2.O $ WORK/_/HOME/PIRATE/PROGRAMS/GOCLIPSE/WORKSPACE/V8/SRC/TEST/_OBJ/TEST.O $WORK/_/home/pirate/programs/goclipse/ workspace/v8/src/test/_obj/test.cc.o# _/home/pirate/programs/goclipse/workspace/v8/src/test/tmp/ go-build845372545/_/home/pirate/programs/goclipse/workspace/v8/src/test/_obj/test.cc.o:in function ' myTest ':./ Test.cc:6: Undefined reference to ' test () ' Collect2:error:ld returned 1 exit status

Here you can see is test.cc is compiled with g++, but the declaration of the test function is in the _CGO_EXPORT.C, the declaration is in the _cgo_export.h, g++ compile will certainly not find this function, but only need a small change to solve the problem, The include "_cgo_export.h" is moved to the extern "C", the final following

extern "C" {#include "_cgo_export.h" void MyTest () {test ();}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.