Home > Backend Development > Golang > How to Integrate Go Functions into Existing C Projects Using GCCGO?

How to Integrate Go Functions into Existing C Projects Using GCCGO?

Linda Hamilton
Release: 2024-10-26 22:42:02
Original
886 people have browsed it

How to Integrate Go Functions into Existing C Projects Using GCCGO?

Using Go Within Existing C Projects

Background:
You have a C program comprised of multiple object files stored in an archive file (.a). You intend to add a new Go file to the project, compile it into an object file, and incorporate it into the existing archive.

Objective:
Integrate Go functions into your C program.

Process:

1. Compile the Go File into an Object File:

  • Run the following command:

    gccgo -c printString.go -o printString.o -fgo-prefix=print -Wall -Werror -march=native
    Copy after login

2. Call Go Functions from C:

  • In your c_caller.c file, declare an extern function:

    extern int PrintString(char*) __asm__ ("print.main.PrintString");
    Copy after login
  • In the main function, call the Go function and handle the result:

    int result = PrintString(string_to_pass);
    if (result) { printf("Everything went as expected!\n"); }
    else       { printf("Uh oh, something went wrong!\n"); }
    Copy after login

3. Using GCCGO to Build the Entire Project:

  • Run the following command:

    gccgo -o main c_caller.c printString.o -Wall -Werror -march=native
    Copy after login

4. Addressing Errors:

  • If you encounter errors like "undefined reference to main.main", ensure that you have a main function in your C code.
  • If you encounter errors like "cannot find -lgo," verify that the LD_LIBRARY_PATH environment variable is set to point to the folder where libgo.so is located.

Alternative Solution in Go 1.5:

In Go 1.5 (coming in August), a new feature enables the creation of C-compatible libraries from Go code. With this feature, you can build a static or shared library directly from a Go file, eliminating the need for intermediate object files.

Example:

  • In main.c:

    #include <stdio.h>
    
    int main()
    {
      char *string_to_pass = NULL;
      if (asprintf(&amp;string_to_pass, "This is a test.") < 0) {
          printf("asprintf fail");
          return -1;
      }
    
      PrintString(string_to_pass);
      return 0;
    }
    Copy after login
  • In main.go (compiled with go build -buildmode c-archive ... for static or go build -buildmode c-shared ... for shared library):

    package main
    
    import "C"
    import "fmt"
    
    //export PrintString
    func PrintString(cs *C.char) {
      s := C.GoString(cs)
      fmt.Println(s)
    }
    
    func main() {}
    Copy after login

The above is the detailed content of How to Integrate Go Functions into Existing C Projects Using GCCGO?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template