第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

導(dǎo)入 cython 生成的 c 共享庫以與 cgo 一起使用

導(dǎo)入 cython 生成的 c 共享庫以與 cgo 一起使用

Go
慕桂英546537 2023-07-10 14:28:21
我想導(dǎo)入一個(gè)由 Cython 在 python 3.7 中生成的 c-shared-library,嘗試通過 cgo 來完成。在這種情況下:go版本go1.12.7 linux/amd64Python 3.7.3Cython 版本 0.29.12操作系統(tǒng):Manjaro 18.0.4內(nèi)核:x86_64 Linux 5.1.19-1我將繼續(xù):制作一個(gè) python 文件vim pylib.pyx:#!pythoncdef public void hello():     print("hello world!")并運(yùn)行python -m cython pylib.pyx生成 c-shared-library,我有兩個(gè)文件,pylib.c以及pylib.h. 現(xiàn)在,嘗試將它們導(dǎo)入到 golang,因此創(chuàng)建一個(gè) go 文件vim test.go:package main/*#include </usr/include/python3.7m/Python.h>#include "pylib.h"*/import "C"import "fmt"func main() {   C.hello()   fmt.Println("done")}最后,我運(yùn)行g(shù)o run test.go:我有以下輸出:# command-line-arguments/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_51159acd5c8e_Cfunc_hello':/tmp/go-build/cgo-gcc-prolog:48: undefined reference to `hello'collect2: error: ld returned 1 exit status我也嘗試將其導(dǎo)入到 c 中,但遇到了類似的輸出,如下所示:undefined reference to `hello'ld returned 1 exit status我不知道該怎么辦,請(qǐng)幫助我。:(
查看完整描述

1 回答

?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

我運(yùn)行 go run test.go:我有以下輸出:


# command-line-arguments

/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_51159acd5c8e_Cfunc_hello':

/tmp/go-build/cgo-gcc-prolog:48: undefined reference to `hello'

collect2: error: ld returned 1 exit status

我們可以使用以下代碼生成等效的錯(cuò)誤消息。


package main


/*

#include <math.h>

*/

import "C"

import "fmt"


func main() {

    cube2 := C.pow(2.0, 3.0)

    fmt.Println(cube2)

}

輸出:


$ go run cube2.go

# command-line-arguments

/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_f6c6fa139eda_Cfunc_pow':

/tmp/go-build/cgo-gcc-prolog:53: undefined reference to `pow'

collect2: error: ld returned 1 exit status

在這兩種情況下,ld(鏈接器)在查看通常的位置后都找不到 C 函數(shù):undefined reference to 'pow'或undefined reference to 'hello'。


讓我們告訴您在 C庫中cgo哪里可以找到 C函數(shù):。powmathm


對(duì)于cgo,使用ld標(biāo)志,


#cgo LDFLAGS: -lm

GCC:3.14 鏈接選項(xiàng)


-llibrary

    Search the library named library when linking.

更新之前的代碼,


package main


/*

#cgo LDFLAGS: -lm

#include <math.h>

*/

import "C"

import "fmt"


func main() {

    cube2 := C.pow(2.0, 3.0)

    fmt.Println(cube2)

}

輸出:


$ go run cube2.go

8

$

這說明了一個(gè)基本cgo原則:包含 C 庫的 C 頭文件并指向 C 庫的位置。


查看完整回答
反對(duì) 回復(fù) 2023-07-10
  • 1 回答
  • 0 關(guān)注
  • 164 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)