无法包含go程序所需的C头文件。

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

Unable to include required C header files for go program

问题

我正在尝试在我的Go程序中包含一个位于/usr/local/WordNet-3.0/include/目录下的头文件。

使用以下标志:

  1. // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
  2. // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib
  3. /*
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "wn.h"
  8. static void printlicense() {
  9. printf("WordNet License %s\n\n%s", dblicense, license);
  10. }
  11. */
  12. import "C"
  13. import "unsafe"
  14. import (
  15. "os"
  16. )

但是当我使用go run运行程序时,出现以下错误:

"fatal error: 'wn.h'文件未找到。"我使用的是go 1.5.1。

如果能告诉我我做错了什么,将不胜感激。

编辑:我已经通过将文件复制到我的工作目录中使其正常工作,但我仍然想知道我之前做错了什么。

英文:

I am trying to include a header file which exists in /usr/local/WordNet-3.0/include/ in my go program

using these flags

  1. // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
  2. // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib
  3. /*
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "wn.h"
  8. static void printlicense() {
  9. printf("WordNet License %s\n\n%s", dblicense, license);
  10. }
  11. */
  12. import "C"
  13. import "unsafe"
  14. import (
  15. "os"
  16. )

but when I run my program using go run, it gives me following error:

"fatal error: 'wn.h' file not found." I am on go 1.5.1.

Any help on what I am doing wrong would be appreciated.

EDIT : I have got this to work by copying the file over in my working directory, but I would still like to know, what I was doing wrong.

答案1

得分: 5

在本地进行了快速测试:你需要删除 cgo 标志和 C 代码之间的空行。

尝试这样做:

  1. // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
  2. // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib
  3. /*
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "wn.h"
  8. static void printlicense() {
  9. printf("WordNet License %s\n\n%s", dblicense, license);
  10. }
  11. */
  12. import "C"
  13. import "unsafe"
  14. import (
  15. "os"
  16. )
英文:

Did a quick test on my local : you need to remove the blank line between your cgo flags and your C code.

Try this :

  1. // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
  2. // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib
  3. /*
  4. #include &lt;stdio.h&gt;
  5. #include &lt;stdlib.h&gt;
  6. #include &lt;string.h&gt;
  7. #include &quot;wn.h&quot;
  8. static void printlicense() {
  9. printf(&quot;WordNet License %s\n\n%s&quot;, dblicense, license);
  10. }
  11. */
  12. import &quot;C&quot;
  13. import &quot;unsafe&quot;
  14. import (
  15. &quot;os&quot;
  16. )

huangapple
  • 本文由 发表于 2015年10月4日 19:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/32933060.html
匿名

发表评论

匿名网友

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

确定