警告:未使用的变量‘_cgo_a’

warning: unused variable ‘_cgo_a’

什么是“_cgo_a”变量?

我正在尝试 link 一个 C++ 静态库。

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

我把上面的编译成一个静态库,像这样:

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

这是我的 main.go

package main

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

func main() {
    C.greet()
}

然后当我这样做时 go build 这就是我得到的:

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

我的go版本:go version go1.12.5 linux/amd64

编辑: 如果我从 CFLAGS 中删除 -Wall 它编译正常。什么是 _cgo_a 变量,为什么它给我一个错误?

不要在 cgo CFLAGS 中使用 -Wall。这是 Go 中的一个普遍问题。在 github 主题中阅读更多内容:https://github.com/golang/go/issues/6883#issuecomment-383800123