Call your own C library with Go using cgo

Use the C language library you wrote in Go with cgo.

C language library creation

The program to be libraryed is hello.c and hello.h is its header file. main.c is a program that uses that library.

hello.c


#include <stdio.h>
#include "hello.h"

void hello(void)
{
    printf("Hello, World!\n");
}

hello.h


extern void hello(void);

main.c


#include <stdlib.h>
#include "hello.h"

int main(int argc, char *argv[])
{
    hello();
    return EXIT_SUCCESS;
}
$ gcc -c hello.c
$ ar rusv libhello.a hello.o
$ gcc -o main main.c -L. -lhello
$ ./main
Hello, World!

Use your own library with Go

I prepared the following program called main.go. cgo is a function for using the C language program provided as standard in Go with Go.

main.go


package main

/*
#cgo LDFLAGS: -L. -lhello
#include <hello.h>
*/
import "C"

func main() {
	C.hello()
}

ʻImport Import a package that requires" C "`. The comment out written just above it is the code for using C language.

/*
#cgo LDFLAGS: -L. -lhello
#include <hello.h>
*/

The C language can be written here as it is, and the functions defined here can be used in the Go code. Gives information about the C language library to use at compile time with LDFLAGS, which has the same meaning as the two arguments specified with $ gcc -o main main.c -L. -lhello in the previous chapter. An option where -L specifies the location of the library. The library is libhello.a created in the previous chapter and is under the same directory, so specify only .. -l specifies the library to use, the way to specify is the library name excluding lib, that is, hello excluding lib from libhello. After that, include hello.h so that you can call thehello ()function.

The execution result is as follows.

$ go run main.go
Hello, World!

Recommended Posts

Call your own C library with Go using cgo
Let's call your own C ++ library with Python (Preferences)
Call your own C language shared library from Python using ctypes
Using cgo with the go command
[Reinforcement learning] DQN with your own library
Publish your own Python library with Homebrew
Linux C / C ++ Build your own library creation environment
Steps to install your own library with pip
Using X11 with ubuntu18.04 (C)
Until you can install your own Python library with pip
Run the intellisense of your own python library with VScode.
Learn algorithms with Go @ recursive call
Call C from Python with DragonFFI
Solve your own maze with Q-learning
Using Lambda with AWS Amplify with Go
Use curl / jq library with Go
Train UGATIT with your own dataset
Solve your own maze with DQN
When using the zap library in your own Logger with Golang, how to put the hierarchy above Logger in Caller
Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Your own Twitter client made with Django
Create your own DNS server with Twisted
Create your own Composite Value with SQLAlchemy
Tweet your own sentences using Markov chains
Try using Dropbox API v2 with Go
[Python] Register your own library on PyPI
Until you install your own Python library
To import your own module with jupyter
I tried Hello World with 64bit OS + C language without using the library
Try to make your own AWS-SDK with bash
[Personal memo] julia --Using Python library with julia using PyCall
[Python] Implement your own list-like class using collections.UserList
Argument implementation (with code) in your own language
Make your own module quickly with setuptools (python)
Precautions when using google-cloud library with GAE / py
Train Stanford NER Tagger with your own data
Make your own music player with Bottle0.13 + jPlayer2.5!