Go language calls C-language function pits

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

Recently in the people's post and telecommunications publishing house Xu Xiwei Lu Guihua "Go language Programming", see "1.2.9 language Interactivity" subsection of the Cprint.go. Instead of writing in the book's source code format, the import "C" and the import "unsafe" are merged into parentheses and are separated by a line with the comment Terminator * /. This is also most go language learning and developers often use code style, I modified the code as follows:

PackageMain

/*
#include <stdio.h>
#include <stdlib.h>
*/

Import(
"C"
"unsafe"
)

funcMain() {
CStr: = C.CString("Hello, World")
C.puts(CStr)
C. Free(unsafe. Pointer(CStr))
}

As a result, go build cprint.go or go run cprint.go in my Windows 7 64-bit and CentOS 6.6 x86_64 environments have been given the following error:

# command-line-arguments
Error: ' Puts ' undeclared (first use of this function)
Error: ' Free ' undeclared (first with this function)

A number of documents were looked up for 3 days, and the reason was not found, including the old version of the Go Language SDK, which was not resolved.

Occasionally see an English document, address: Http://stackoverflow.com/questions/18237738/linking-golang-with-xlib, prompted to import the virtual package C format must be a separate line, And immediately after the end of the comment line, and then re-follow the prompts to modify the compilation and run all passed, strictly according to the "Go Language Programming" book format is also normal.

The correct code is as follows:

package  main

/*
#include <stdio.h>
#include <stdlib.h>
*/
import  

import   (
    
)

Span style= "color: #ffff00;" >func  main () {
    cstr: = C.cstring ()
    c.puts (CStr)
    c.free (unsafe. Pointer (CStr)
}
   

The Go Language code format is very strict, the majority of go language enthusiasts sometimes accidentally modified the format or the use of other language style format, compile and run there may be a variety of inexplicable problems. remind you again: import "C" must immediately follow the end of the C code comment The last line, must not empty a row, and other packages can not be merged with the import parentheses.


This article is from the "Pine" blog, be sure to keep this source http://dongsong.blog.51cto.com/916653/1587363

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.