'invalid flag in #cgo LDFLAGS: -w' 问题解决
当我们在go项目中使用C库,或者引用的第三方库有使用C库,有时候会遇到 invalid flag in #cgo LDFLAGS: -w 这种错误。
这是因为在项目代码中,使用了#cgo指令符(directive),对C/C++编译器添加了 CFLAGS、 CPPFLAGS、CXXFLAGS 、LDFLAGS 等选项设置。
例如github.com/spacemonkeygo/openssl库的build.go中,则对各个特定平台的C/C++编译器,设置了不同的编译约束参数:
package openssl
// #cgo pkg-config: libssl libcrypto
// #cgo linux CFLAGS: -Wno-deprecated-declarations
// #cgo darwin CFLAGS: -I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/openssl/include -Wno-deprecated-declarations
// #cgo darwin LDFLAGS: -w -L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/openssl/lib
// #cgo windows CFLAGS: -DWIN32_LEAN_AND_MEAN
import "C"
当我们使用go版本在1.10及其以上版本进行项目编译的时候,则会提示如下错误:
go build xxxx/vendor/github.com/spacemonkeygo/openssl: invalid flag in #cgo LDFLAGS: -w
原因是golang为了安全,在使用go get,go build和friends期间,禁止编译器/链接器使用LDFLAGS等连接参数,目的是防止编译器被攻击。
如果要使用LDFLAGS等连接参数,我们需要手动指定CGO_LDFLAGS_ALLOW等参数,例如:CGO_LDFLAGS_ALLOW='-w'。
因此在命令行编译的时候,使用:
CGO_LDFLAGS_ALLOW='-w' go rum main.go
或者在IDE工具中,添加如上环境变量即可。
相关文章:https://github.com/golang/go/wiki/InvalidFlag
相关内容
InvalidFlag
Eddie Webb edited this page on 27 Feb 2019 · 2 revisions
invalid flag in #cgo CFLAGS
This page describes the background for build errors like invalid flag in #cgo CFLAGS and what you can do about them.
CVE-2018-6574 described a potential security violation in the go tool: running go get downloads and builds Go code from the Internet, Go code that uses cgo can specify options to pass to the compiler, so careful use of -fplugin can cause go get to execute arbitrary code. While it is difficult to block every possible way that the compiler might be attacked, we have chosen to block the obvious ones.
As described at issue 23672, this is done by using a safelist of compiler/linker options that are permitted during go get, go build, and friends. When cgo code tries to use to pass an option that is not on the safelist, the go tool will report an error invalid flag in #cgo CFLAGS (or #cgo LDFLAGS, pkg-config --cflags, pkg-config --ldflags, and so forth).
This safelist is new in releases 1.8.7, 1.9.4, and 1.10, and all subsequent releases.
What can I do?
If this happens to you, and the option is benign, you should do two things:
Set the environment variable CGO_CFLAGS_ALLOW (or CGO_LDFLAGS_ALLOW, CGO_CXXFLAGS_ALLOW, and so forth) to a regexp that matches the option.
File a bug requesting that the option be added to the safelist. Be sure to include the complete error message and, if possible, a description of the code you are building.
Why not use an unsafe list?
Because if some new unsafe option is added to a compiler, all existing Go releases will become immediately vulnerable.
Why not get a complete list of compiler options and safelist all of them?
Because there are hundreds of options, and there is no clear way to get a complete list. Many compiler and linker options are target dependent, and thus only reported on specific platforms or in specific configurations. The documentation is known to be incomplete.
'invalid flag in #cgo LDFLAGS: -w' 问题解决的更多相关文章
- go编译go-gtk,出现invalid flag in pkg-config --libs: -Wl,-luuid提示
go编译go-gtk 使用go编译go-gtk,直接编译会出现错误: pkg-config --cflags -- gdk-2.0 gthread-2.0 pkg-config --l ...
- 关于Maven整合SSM项目中报错Invalid bound statement (not found):的问题解决
如图:控制不报错 页面就是报500的错误 查阅了好多资料 都说是Mapper文件写的不对 我仔细找了好几遍也解决不了问题.. 解决: 坑爹的问题害我找了一上午原因,原来是需要在pom.xml文件中 ...
- 10 Go 1.10 Release Notes
Go 1.10 Release Notes Introduction to Go 1.10 Changes to the language Ports Tools Default GOROOT &am ...
- 在k8s中搭建可解析hostname的DNS服务
2016-01-25更新 上篇文章总结k8s中搭建hbase时,遇到Pod中hostname的DNS解析问题,本篇将通过修改kube2sky源码来解决这个问题. 1 前言 kube2sky在Githu ...
- go mac 交叉编译 linux
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -o server ./server.go
- fdisk添加分区引起的Linux Error: 22: Invalid argument
在Linux服务器(虚拟机)上使用fdisk添加分区.格式化分区后,遇到了Linux Error: 22: Invalid argument错误,操作步骤如下所示 [root@oracle-serve ...
- WARNING: Re-reading the partition table failed with error 22: Invalid argument
在划分磁盘分区时,遇到错误"WARNING: Re-reading the partition table failed with error 22: Invalid argument&qu ...
- CGO入门和OCR文字识别(非第三方API,有源码,效果好)实战
这是我参与8月更文挑战的第5天,活动详情查看:8月更文挑战 系列文章见: [第四天] GDB调试指南:C++中如何调试生产环境的程序? [第三天] IM敏感词算法原理和实现 [第二天] 现代IM架构研 ...
- golang的cgo支持调用C++的方法
1)swift,貌似官网的推荐 2)extern "C" 我使用后者,用起来比较爽,上代码 c.h #pragma once #ifdef __cplusplus extern & ...
- Go调用C代码,Cgo札记
http://www.myexception.cn/program/679738.html Go调用C代码,Cgo笔记 参考: 官方文档: http://golang.org/cmd/cgo/ htt ...
随机推荐
- el-radio-group初始化默认值后点击无法切换问题/vue中设置表单对象属性页面不同步问题
<el-radio-group v-model="ruleForm.type"> <el-radio :label="1">方案一< ...
- 傻妞教程——对接mongoDB使用返佣系统
使用docker安装mongo 1.安装 1.1 拉取mongo镜像 docker pull mongo:4.4 1.2 创建mongo数据持久化目录 mkdir -p /docker_volume/ ...
- C++17 Filesystem 实用教程
点击查看代码 C++17 标准带来了 std::filesystem库, 提供了强大的工具来处理文件路径, 目录以及其他与文件系统相关的操作. 这篇文章适合 C++ 初学者以及希望掌握 C++17 新 ...
- [Qt 基础内容-05] QDialogButtonBox
QDialogButtonBox 本文主要根据QT官方帮助文档以及日常使用,简单的介绍一下QDialogButtonBox的功能以及使用 文章目录 QDialogButtonBox 简介 信号和槽 基 ...
- libvirt和qga的区别?
本文分享自天翼云开发者社区<libvirt和qga的区别?>,作者:乐道 Libvirt是一个开源项目,提供了一组API.工具.库,用于管理和控制虚拟化平台. 在Openstack环境中, ...
- Flask快速入门4-蓝图
十六,Flask 用蓝图实现模块化的应用 1,Flask 为什么使用蓝图? Flask 中的蓝图为这些情况设计: 把一个应用分解为一个蓝图的集合.这对大型应用是理想的.一个项目可以实例化 一个应用对象 ...
- MinIO Linux 数据迁移
目录 安装 下载 安装Minio客户端 备份数据 数据还原 数据迁移 设置别名 迁移 从A服务器,迁移致B服务器,前提,需要两边的 Bucket 相同,如果B服务器没有A服务器里的 Bucket 需要 ...
- StarRocks 升级注意事项
前段时间升级了生产环境的 StarRocks,从 3.3.3 升级到了 3.3.9,期间还是踩了不少坑所以在这里记录下. 因为我们的集群使用的是存算分离的版本,也是使用官方提供的 operator 部 ...
- Spring定时任务的秘密
Spring定时任务的秘密 在 Spring 框架中,定时任务主要通过 @Scheduled 注解或 TaskScheduler 接口实现. 1.基本使用 在 Spring Boot 项目中,通过 @ ...
- 数字先锋 | 打造专属AI大脑,天翼云助力重庆理工大学开启智慧校园新篇!
从在线课程的蓬勃兴起,到远程教育的逐渐普及,再到智能教学工具的广泛应用--人工智能.大数据.云计算等前沿技术迅速发展的当下,科技正以前所未有的深度和广度重塑教育领域,促使教育各个层面发生深刻变革.师生 ...