Search code examples
gocgo

warning: unused variable ‘_cgo_a’


What is the '_cgo_a' variable?

I'm trying to link a c++ static lib.

greeter.cpp

#include "greeter.h"
#include <iostream>

void
greet()
{
    std::cout << "Greetings\n";
}

greeter.h

#ifndef GREETER_H_
#define GREETER_H_

#ifdef __cplusplus
extern "C" {
#endif

void
greet();

#ifdef __cplusplus
}
#endif

#endif

I compiled the above into a static library like so:

$ g++ -c greeter.cpp
$ ar vfx greeter.o -o libgreeter.a

and here's my main.go

package main

// #cgo CFLAGS: -g -Wall
// #cgo LDFLAGS: -L. -lgreeter
// #include "greeter.h"
import "C"

func main() {
    C.greet()
}

Then when I do go build that's what I get:

# error
cgo-gcc-prolog: In function ‘_cgo_261f55e693f4_Cfunc_greet’:
cgo-gcc-prolog:46:49: warning: unused variable ‘_cgo_a’ [-Wunused-variable]

My go version: go version go1.12.5 linux/amd64

EDIT: If I remove the -Wall from the CFLAGS it compiles fine. Still what is the _cgo_a variable and why is it give me an error?


Solution

  • Do not use -Wall in cgo CFLAGS. This is a general issue in Go. Read more in the github thread: https://github.com/golang/go/issues/6883#issuecomment-383800123