<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>《Go与C语言的互操作》的评论</title>
	<atom:link href="http://tonybai.com/2012/09/26/interoperability-between-go-and-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/</link>
	<description>一个程序员的心路历程</description>
	<lastBuildDate>Fri, 17 Apr 2026 15:31:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>作者：fastfading</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-7523</link>
		<dc:creator>fastfading</dc:creator>
		<pubDate>Tue, 17 Nov 2020 06:38:23 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-7523</guid>
		<description>func CArrayToGoArray(cArray unsafe.Pointer, size int) []int { return (*[1&lt;&lt;30]int)(cArray)[0:size]}  
这个貌似也不是全平台方案
@tony bai    能否给出一个CArrayToGoArray 实现的cgo 例子, 
注意这里数组内存是在C 里面malloc，   最后传给了go， 需要go管理生存期。
//extern void MyCMallocArray(int *p, int n);
func CGoMallocArray() []int  {
xxxxxx
MyCMallocArray
 xxxxxx

}</description>
		<content:encoded><![CDATA[<p>func CArrayToGoArray(cArray unsafe.Pointer, size int) []int { return (*[1&lt;&lt;30]int)(cArray)[0:size]}<br />
这个貌似也不是全平台方案<br />
@tony bai    能否给出一个CArrayToGoArray 实现的cgo 例子,<br />
注意这里数组内存是在C 里面malloc，   最后传给了go， 需要go管理生存期。<br />
//extern void MyCMallocArray(int *p, int n);<br />
func CGoMallocArray() []int  {<br />
xxxxxx<br />
MyCMallocArray<br />
 xxxxxx</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：bigwhite</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-7322</link>
		<dc:creator>bigwhite</dc:creator>
		<pubDate>Wed, 23 Jan 2019 23:19:05 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-7322</guid>
		<description>我用&quot;go1.11.2 darwin/amd64&quot;版本go编译器在macos下重新试了一下该例子，编译和执行都是ok的(不过我没有在windows上试，我没有windows上的go编译环境)：

$go build -o bar bar1.go bar2.go
 $ls
bar*		bar1.go		bar2.go		go.mod
 $./bar
I am bar!
I am a GoExportedFunc!</description>
		<content:encoded><![CDATA[<p>我用”go1.11.2 darwin/amd64&#8243;版本go编译器在macos下重新试了一下该例子，编译和执行都是ok的(不过我没有在windows上试，我没有windows上的go编译环境)：</p>
<p>$go build -o bar bar1.go bar2.go<br />
 $ls<br />
bar*		bar1.go		bar2.go		go.mod<br />
 $./bar<br />
I am bar!<br />
I am a GoExportedFunc!</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：R</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-7321</link>
		<dc:creator>R</dc:creator>
		<pubDate>Wed, 23 Jan 2019 08:59:52 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-7321</guid>
		<description>bar1.go bar2.go的例子会报错，报错信息如下：
# command-line-arguments
C:\Users\ADMINI~1\AppData\Local\Temp\go-build390401246\b001\_x002.o: In function `bar&#039;:
D:/Go-Work/src/Demo/DemoCGO/main.go:23: undefined reference to `GoExportedFunc&#039;
D:/Go-Work/src/Demo/DemoCGO/main.go:23: undefined reference to `GoExportedFunc&#039;
collect2.exe: error: ld returned 1 exit status

Compilation finished with exit code 2

golang版本是go version go1.11.2 windows/amd64</description>
		<content:encoded><![CDATA[<p>bar1.go bar2.go的例子会报错，报错信息如下：<br />
# command-line-arguments<br />
C:\Users\ADMINI~1\AppData\Local\Temp\go-build390401246\b001\_x002.o: In function `bar&#8217;:<br />
D:/Go-Work/src/Demo/DemoCGO/main.go:23: undefined reference to `GoExportedFunc&#8217;<br />
D:/Go-Work/src/Demo/DemoCGO/main.go:23: undefined reference to `GoExportedFunc&#8217;<br />
collect2.exe: error: ld returned 1 exit status</p>
<p>Compilation finished with exit code 2</p>
<p>golang版本是go version go1.11.2 windows/amd64</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：phpor</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-972</link>
		<dc:creator>phpor</dc:creator>
		<pubDate>Mon, 21 Jul 2014 02:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-972</guid>
		<description>这里提到的C使用GO的例子，最终还是使用GO来编译的C代码，如果GO编译出来的库文件能直接给C使用就好了（似乎不太现实）</description>
		<content:encoded><![CDATA[<p>这里提到的C使用GO的例子，最终还是使用GO来编译的C代码，如果GO编译出来的库文件能直接给C使用就好了（似乎不太现实）</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：匿名</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-881</link>
		<dc:creator>匿名</dc:creator>
		<pubDate>Wed, 26 Jun 2013 04:47:26 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-881</guid>
		<description>这个不行啊，内存管理要分开</description>
		<content:encoded><![CDATA[<p>这个不行啊，内存管理要分开</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：j5ive</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-781</link>
		<dc:creator>j5ive</dc:creator>
		<pubDate>Sun, 28 Oct 2012 14:58:21 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-781</guid>
		<description>func CArrayToGoArray(cArray unsafe.Pointer, size int)  []int {    return (*[1&lt;&lt;30]int)(cArray)[0:size]}</description>
		<content:encoded><![CDATA[<p>func CArrayToGoArray(cArray unsafe.Pointer, size int)  []int {    return (*[1&lt;&lt;30]int)(cArray)[0:size]}</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：四月份平民</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-780</link>
		<dc:creator>四月份平民</dc:creator>
		<pubDate>Fri, 26 Oct 2012 06:59:24 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-780</guid>
		<description>func CArrayToGoArray(cArray unsafe.Pointer, size int) (goArray []int) {...}这个有更好的实现方案：func CArrayToGoSlice(begin uintptr, size int) []byte {	var theGoSlice []byte	sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&amp;theGoSlice)))	sliceHeader.Cap = size	sliceHeader.Len = size	sliceHeader.Data = begin	return theGoSlice}</description>
		<content:encoded><![CDATA[<p>func CArrayToGoArray(cArray unsafe.Pointer, size int) (goArray []int) {&#8230;}这个有更好的实现方案：func CArrayToGoSlice(begin uintptr, size int) []byte {	var theGoSlice []byte	sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&amp;theGoSlice)))	sliceHeader.Cap = size	sliceHeader.Len = size	sliceHeader.Data = begin	return theGoSlice}</p>
]]></content:encoded>
	</item>
	<item>
		<title>作者：t.k.</title>
		<link>https://tonybai.com/2012/09/26/interoperability-between-go-and-c/#comment-765</link>
		<dc:creator>t.k.</dc:creator>
		<pubDate>Thu, 27 Sep 2012 13:59:20 +0000</pubDate>
		<guid isPermaLink="false">http://tonybai.com/?p=1051#comment-765</guid>
		<description>都是很新鲜的概念。我觉得Go把python对c的利用也学到了。</description>
		<content:encoded><![CDATA[<p>都是很新鲜的概念。我觉得Go把python对c的利用也学到了。</p>
]]></content:encoded>
	</item>
</channel>
</rss>
