作者Hsins (迅雷不及掩耳盗铃)
看板C_and_CPP
标题Re: [问题] 'numpy/arrayobject.h' file not found
时间Tue Aug 13 11:37:08 2019
※ 引述《Philethan (Ethan)》之铭言:
: 开发平台(Platform): (Ex: Win10, Linux, ...)
: MacOS Mojave 10.14.6
: 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
: Xcode Version 10.3
Xcode 不是编译器,他是集成开发环境(IDE, Integrated Development Environment)
https://github.com/lava/matplotlib-cpp
在这个函数库的页面上可以知道他用的编译器应该是 gcc
要查看版本可以在命令行下查看,比如我的:
---
$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
---
: 错误讯息都发生在执行 matplotlibcpp.h 的时候。
: 最初的错误是:'Python.h' file not found
: 这时的指令为 #include <Python.h>
: 但在我将 include 的内容改为 #include <Python/Python.h> 之後就ok了。
: (参考自https://reurl.cc/YLAno)
这个 'Python.h' 是 matplotlibcpp.h 里面引入的标头档
在 macOS 下 not found 的原因是如同你找到的那篇所说
他所放置的路径并不像其他 Linux/Unix 作业系统一样放在 /usr/include/ 里
而是在 /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
一般来说的状况下,可以采用那篇回答来做没什麽问题
: 可是就发生了第二个错误讯息:'numpy/arrayobject.h' file not found
: 在作者Github上也有网友提出相同问题,作者是说只要你设定好正确档案路径
: 就可以了(https://github.com/lava/matplotlib-cpp/issues/46)。
: 後来我试着在 terminal 中进入 Python,然後
: '/Users/Ethan/Library/Python/3.6/lib/python
: /site-packages/numpy/core/include'
: 於是我将上述路径新增到
: Xcode >> Project >> build settings >> target >> Search Header Path
: 不论是底下的 debug,还是 release,我将上述路径去掉前後单引号後直接贴上。
: 结果不但没解决问题,反而还使我找不到 Python.h,设定仍为
: #include<Python/Python.h>
: 'Python.h' file not found
: 很神奇的是,如果我将上述路径的 /include 去掉,那就找得到 Python.h
: 也就是说,Python.h 看起来好像是位於 .../numpy/core 资料夹中,但我
: 从 Finder 去看,却找不到 Python.h,所以我也搞不懂为什麽去掉 /include
: 之後就没有出现 'Python.h' file not found 错误讯息。
: 话说回来,就算去掉了/include,还是会出现错误讯息:
: 'numpy/arrayobject.h' file not found
: 但是我可以肯定/include之中确实有 /numpy/arrayobject.h 这个档案,
这个时候的 'Python.h' file not found
是因为 numpy/arrayobject.h 里面又有 #include <Python.h>
你不太可能一个档案一个档案都去改成 <Python/Python.h>
----
比较推荐的解决做法是,透过 ln -s 创建 soft link 到 include 路径下
首先处理 Python.h 的问题:
-
$ ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 /usr/local/include/python2.7
-
接着处理 numpy/ndarrayobject.h 的问题
这部分在昨天有踩到一个坑,就是如果使用 GitHub 上面最新的包
或者是用 brew install numpy 下的包,版本都太新了
後来我在这个包找到了历史古蹟
https://github.com/explosion/thinc
-
# 复制标头档到常用的函式库资料夹
$ cd ~
$ git clone https://github.com/explosion/thinc
$ cp -r ~/thinc/include/numpy/ ~/Library/Developer/numpy
$ rm -r ~/thinc
# 建立软连结
$ ln -s ~/Library/Developer/numpy /usr/local/include/numpy
-
最後应该透过下面的命令就能完成编译了:
---
$ g++ test.cpp -std=c++11 -I/usr/local/include/python2.7 -lpython2.7
$ ./a.out
---
当然也可以一股脑地全部都丢到 /usr/local/include
我是不太喜欢自己环境弄太脏...
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.241.112.68 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1565667433.A.D7E.html
1F:推 Philethan: 非常感谢大大协助!!!!相当有用! 08/13 16:31
2F:→ Philethan: 已可以在 terminal 正常绘图罗 不过Xcode还是怪怪的 08/13 16:31