使用cgo和makefile 使用cgo时,可以结合makefile来编译和构建项目。

huangapple go评论81阅读模式
英文:

Using makefile with cgo

问题

我正在使用gcc在Windows上编译一个启用CGO的Go程序。gcc已经预编译到了Webots中。

我可以成功地使用以下makefile编译C程序:

### 不要修改:这包括了Webots全局Makefile.include
null :=
space := $(null) $(null)
WEBOTS_HOME_PATH?=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
gobuild:
	go build -x -v

但是,如果我运行make gobuild,它会失败并显示错误:fatal error: webots/robot.h: No such file or directory

在使用make gobuild进行CGO构建时,无法找到头文件webots/robot.h,但是在使用make构建时可以找到。

我该如何解决这个问题?

英文:

I am compiling a CGO-enabled Go program using gcc in windows. The gcc comes precompiled into webots.

I can compile successfully the c program alone using the following makefile:

### Do not modify: this includes Webots global Makefile.include
null :=
space := $(null) $(null)
WEBOTS_HOME_PATH?=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
gobuild:
	go build -x -v

But if I run make gobuild it fails with the error: fatal error: webots/robot.h: No such file or directory

The header file webots/robot.h cannnot be located when building with/for cgo using make gobuild, but it is found when building with make.

How do I fix this?

答案1

得分: 1

将包含webots/robot.h文件的目录添加到使用make gobuild构建时的包含路径中。您可以通过在makefile中的go build命令后添加-I标志,然后跟随包含头文件的目录的路径来实现此目的。例如:

gobuild:
    go build -x -v -I/path/to/webots/include
英文:

Add the directory containing the webots/robot.h file to the include path when building with make gobuild. You can do this by adding the -I flag followed by the path to the directory containing the header file to the go build command in your makefile. For example:

gobuild:
    go build -x -v -I/path/to/webots/include

huangapple
  • 本文由 发表于 2023年4月19日 22:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定