Calling a C shared library from GO

I am trying to call a C shared library from C, but i always get an error

could not determine kind of name for C.CFile_print

All my GOPATH and GOBIN are set, my .go file is as follows am creating package company, i have linked my C file also in go #cgo LDFLAGS: -L./ -lcfile

Company.go

package company

/*
 #cgo LDFLAGS: -L. / -lcfile
 #include "header.h"
*/
import (
    "C"
    "fmt"
)

func companyInfo(company_name *C.char, subsidiary_name *C.char, age int) {
    fmt.Println("In GO")
    fmt.Println("Company_name:", C.GoString(company_name))
    fmt.Println("subsidery_name:", C.GoString(subsidery_name))
    fmt.Println("Age:", age)
    C.CFile_print()
}

My C file is as follows.

CFile.c

#include <stdio.h>
#include "header.h"
//#include "_cgo_export.h"

void CFile_print() {
    printf("I am in C file\n");
}

My header files is as follows

header.h

void CFile_print(void);

My directory content is as below.

.
├── CFile.c
├── CFile.o
├── header.h
├── libcfile.so
└── company.go

Can any one please tell why i always get the error, and i want my go as package company i dont want to make it as main package

could not determine kind of name for C.CFile_print

replace this:

/*
 #cgo LDFLAGS: -L. / -lcfile
 #include "header.h"
*/
import (
    "C"
    "fmt"
)

with:

/*
 #cgo LDFLAGS: -L. -lcfile
 #include "header.h"
*/
import "C"

import (
    "fmt"
)

I think.

1 Like

Thank You it worked

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.