Let's try to create an example on how to call C function from Go by using static go libraries.
This sample has been tested on Ubuntu 16.04 LTS.
create folder call_c_doubler and create file as below
filename:Makefile
.PHONY: clean all
all:c_static_lib go_executable
c_static_lib:
gcc -c doubler/*.c
ar rs doubler.a *.o
rm -rf *.o
go_executable:
go build -o call_c_doubler
clean:
rm -rf *.o *.a call_c_doubler
filename: main.go
package main
import "fmt"
// #cgo CFLAGS: -I${SRCDIR}/doubler
// #cgo LDFLAGS: ${SRCDIR}/doubler.an
// #include <stdlib.h>
// #inclue <libdoubler.h>
import "C"
func main() {
fmt.Printf("Enter Go...\n")
fmt.Printf("Double : %d\n", C.double_it(2))
fmt.Printf("Exit Go...\n")
}
In folder call_c_doubler/doubler and create file as below
filename:libdoubler.c
#include "libdoubler.h"
#include <stdio.h>
int double_it(int x) {
printf("Calling C function");
return 2 * x;
}
filename:libdoubler.h
#ifndef DOUBLER_H
#define DOUBLER_H
int double_it(int x);
#endif
Then just run command make in your terminal to compile the program
Other topics:
How to build NGINX RTMP module, Setup Live Streaming with NGINX RTMP module, Publishing Stream with Open Broadcaster Software (OBS), Create Adaptive Streaming with NGINX RTMP, Implementing Filtergraph in Streaming with NGINX RTMP, How to Implement Running Text in Streaming with NGINX RTMP, How to build OpenSceneGraph with Code::Blocks, How to build OpenSceneGraph with Visual Studio 2010, Building Geometry Model, How to run OpenSceneGraph with Netbean IDE 8.2 C++, Rendering Basic Shapes, Using OSG Node to Load 3D Object Model, Rendering 3D Simulator with OpenSceneGraph, How to compile wxWidgets with Visual Studio 2010, How to Setup Debugging in Golang with Visual Code, How to Load 3D Object Model with OpenSceneGraph on wxWidgets