<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 在單獨的文件用 Go 調用 C 代碼 現在讓我們繼續討論當 C 代碼位于單獨的文件中時如何從 Go 程序中調用 C 代碼。 首先,我將解釋用程序解決的假想的問題。然后我們需要使用已經實現好的兩個 C 函數,這些函數我們不希望或無法在 Go 中重寫。 #### C 代碼部分 本小節將為你提供示例的 C 代碼。它有兩個文件:`callC.h`和`callC.c`。文件`(callC.h)`包含以下代碼: ```c #ifndef CALLC_H #define CALLC_H void cHello(); void printMessage(char* message); #endif ``` 文件`(callC.c)`包含以下代碼: ```c #include <stdio.h> #include "callC.h" void cHello() { printf("Hello from C!\n"); } void printMessage(char* message) { printf("Go send me %s\n", message); } ``` `callC.c`和`callC.h`文件都存儲在單獨的目錄中,我們把該目錄命名為`callClib`。但是,你可以使用你想使用的任何目錄名稱。 > Tip: 只要你使用正確的類型和數量的參數調用正確的 C 函數,那么的 C 代碼的具體實現并不重要。C 代碼中沒有任何內容告訴我們它將在 Go 程序中使用。You should look at the Go code for the juicy part. #### Go 代碼部分 本小節將為你提供示例的 Go 源代碼,該源代碼名為`callC.go`,并將分三部分向你介紹。 第一部分的代碼: ```Go package main // #cgo CFLAGS: -I${SRCDIR}/callClib // #cgo LDFLAGS: ${SRCDIR}/callC.a // #include <stdlib.h> // #include <callC.h> import "C" ``` 整個 Go 源文件中最重要的 Go 語句是使用單獨的 `import`語句包含 C 包。但是,`C`是一個虛擬的 Go 包,它只是告訴`go build`在 go 編譯器處理文件之前使用 `cgo` 工具對其輸入文件進行預處理。你仍然可以看到你需要使用注釋來告知 Go 程序有關 C 代碼的信息。在這種情況下,你告訴`callC.go`在哪里可以找到`callC.h`文件以及在哪里可以找到我們將在一段時間內創建的`callC.a`庫文件,就像以`#cgo`開頭的一段代碼。 第一部分的代碼: ```Go import ( "fmt" "unsafe" ) func main() { fmt.Println("Going to call a C function!") C.cHello() ``` 最后一部分代碼: ```Go fmt.Println("Going to call another C function!") myMessage := C.CString("This is Mihalis!") defer C.free(unsafe.Pointer(myMessage)) C.printMessage(myMessage) fmt.Println("All perfectly done!") } ``` 為了從 Go 將字符串傳遞給 C 函數,你將需要使用`C.CString()`創建一個 C 字符串。此外,你將需要一個`defer`語句,以便在不再需要 C 字符串時釋放其存儲空間。 defer 語句包括對`C.free()`和`unsafe.Pointer()`的調用。 在下一部分中,你將看到如何編譯和執行`callC.go`。 #### 混合 Go 和 C 代碼 現在你已經有了 C 代碼和 Go 代碼,現在該學習下一步以執行調用 C 代碼的 Go 文件了。 由于所有的關鍵信息都包含在 Go 文件中了,所以你唯一需要做的就是編譯 C 代碼以生成一個庫: ```shell $ ls -l callClib/ total 16 -rw-r--r--@ 1 mtsouk staff 162 Jan 10 09:17 callC.c -rw-r--r--@ 1 mtsouk staff 89 Jan 10 09:17 callC.h $ gcc -c callClib/*.c $ ls -l callC.o -rw-r--r-- 1 mtsouk staff 952 Jan 22 22:03 callC.o $ file callC.o callC.o: Mach-O 64-bit object x86_64 $ /usr/bin/ar rs callC.a *.o ar: creating archive callC.a $ ls -l callC.a -rw-r--r-- 1 mtsouk staff 4024 Jan 22 22:03 callC.a $ file callC.a callC.a: current ar archive $ rm callC.o ``` 之后,你將在與 callC.go 文件相同的目錄中擁有一個名為`callC.a`的文件。 gcc 可執行命令是**C 編譯器**的名稱。 現在,你可以使用 Go 代碼編譯文件創建一個新的可執行文件: ```shell $ go build callC.go $ ls -l callC -rwxr-xr-x 1 mtsouk staff 2403184 Jan 22 22:10 callC $ file callC callC: Mach-O 64-bit executable x86_64 ``` 執行`callC`可執行文件,你將得到輸出: ```shell $ ./callC Going to call a C function! Hello from C! Going to call another C function! Go send me This is Mihalis! All perfectly done! ``` > Tip: 如果要調用少量的 C 代碼,強烈建議 C 和 Go 代碼在單個 Go 文件中。但是,如果你要進行更復雜和更高級的操作,則首選靜態 C 庫。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看