看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《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://www.ptt.cc/bbs/C_and_CPP/M.1565667433.A.D7E.html
Philethan: 非常感謝大大協助!!!!相當有用! 08/13 16:31
Philethan: 已可以在 terminal 正常繪圖囉 不過Xcode還是怪怪的 08/13 16:31