Using cgo with go modules

Issue

I’m trying to create a go program and I need to use freetype, but module for freetype in go seems really different from the original freetype library for c. Overcome this problem I created a simple library in C, and I’m trying to call a function in that library from Go using cgo. But I couldn’t seem to figure out where should my .a file needs to be located in order go to be able to find it. I’m using go modules and my project is not in the GOPATH.

So my question is where I need to put my C library’s .h and .a files?

Solution

If i got you right. It’s a problem that you don’t know how to tell cgo tool chain where to find .h and .a files. There is many examples on web, provide mine here.

code_example

package democgo

/*
#cgo CFLAGS: -I ${directory_of_your_dot_H}
#cgo LDFLAGS: -L ${directory_of_your_dot_A} -l${your_dot_A_name} -lstdc++
#include "code_from_c.h"
#include <stdlib.h>
*/
import "C"

// Hello is.
func Hello() {
    fmt.Println("this is CGO at package democgo")
    fmt.Println(
        C.GoString(
            C.HelloWorld(),
        ),
    )
}

Answered By – Frank Wang

Answer Checked By – Pedro (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.