dongliao6491 2019-06-17 14:52
浏览 183
已采纳

警告:未使用的变量“ _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
";
}

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?

  • 写回答

1条回答 默认 最新

  • dtbonklcs575884485 2019-06-17 21:15
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?