Including C library in Go triggers syntax and type errors

Viewed 76

I am writing a Go program that needs to import a C library. The library is a 3rd party library so I only have the header files. While working with this library in C, there are no compilation or syntax errors, however, when building my Go/cgo program I am seeing pages of errors to this effect:

#11 0.473 ./vendor/path/to/lib/base.h:235:31: error: stray '##' in program
#11 0.473 ./vendor/path/to/lib/base.h:235:46: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
#11 0.473 ./vendor/path/to/lib/common.h:13:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before '#pragma'
#11 0.473 ./vendor/path/to/lib/init.h:227:52: error: unknown type name 'InitOptionsType'
#11 0.473 ./vendor/path/to/lib/init.h:271:59: error: unknown type name 'PlatformOptionsType'
...

At first, I was seeing these errors on Windows and blamed GCC issues. Now, however, I am building this in a Docker container running Alpine Linux and with GCC installed.

Dockerfile for reference:

FROM golang:alpine
ENV APP_NAME hello-world
# install gcc
RUN apk add build-base
WORKDIR /home/helloworld
# copy and build program across
ADD vendor/ ./vendor/
COPY main.go .
COPY go.mod .
RUN go build -o /go/app

GCC version

gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027
Copyright (C) 2020 Free Software Foundation, Inc.

And finally, my Go code:

package main

import "fmt"

// #cgo CFLAGS: -Ivendor/path/to/lib/ -g -Wall
// #include "vendor/path/to/lib/sdk.h"
import "C"

func main() {
    fmt.Println("Hello World!")
}

Without the #include, the program builds and runs perfectly.

For further illustration, here are the areas of code that were highlighted by the errors I gave :

#define MY_STRUCT(struct_name, struct_def)           \

    EXTERN_C typedef struct _tag ## struct_name {     \ // stray '##' in program
                                                        // & expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

        MY_PASTE struct_def                          \

    } struct_name

Any idea as to why this is happening?

0 Answers
Related