程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Write c++ code on OSX to call Python script and compile with error

編輯:Python

environmental information

Use Macbook Pro(M1 Pro chip ),OS Version is Monterey 12.2.1.
Python Virtual environment through conda install , Version is 3.9.7.C++ call Python Used in Python Header files and Python Dynamic library is from Python Copy from the virtual environment .
The compiler version information is as follows :

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

cmake Version is 3.22.2.

Problem phenomenon

When compiling code , Report the following error :

ld: warning: ignoring file /Users/yangfan/machine_learning/pytorch_cpp/third_party/python/lib/libpython3.9.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

Code

  • python Code
def print_hello(): print("hello")
  • C++ Code
#include <iostream>#include <Python.h>using namespace std;bool call_python(){ Py_Initialize(); PyObject* module = PyImport_ImportModule("test"); if (module == nullptr) { cout << "module is NULL." << endl; return false; } PyObject* func = PyObject_GetAttrString(module, "print_hello"); if (func == nullptr) { cout << "function is NULL." << endl; return false; } PyEval_CallObject(func, nullptr); Py_Finalize(); return true;}int main(int argc, const char* argv[]){ call_python(); return 0;}
  • cmake Script
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)project(example)include_directories(../third_party/python/include/python3.9)link_directories(../third_party/python/lib)add_executable(example example.cpp)target_link_libraries(example python3.9)set_property(TARGET example PROPERTY CXX_STANDARD 14)

Cause analysis

The cause of the problem is speculated to be the environmental python Dynamic library files (libpython3.9.dylib) yes macOS-x86_64 Architecturally , and M1 Pro The chip is arm64 framework .
however , How to install macOS-arm64 Architecturally python Dynamic library , The answer has not yet been found , Please see if there are any solutions .




Take the answer :

I have found a solution , Let me ask and answer myself .
Since we passed conda Installed python Dynamic library is not arm64 framework , I wonder if I can get it through other installation methods arm64 Architecturally python Dynamic library . therefore , And I use homebrew Installed python, The result was that I was really blinded .

xxxx@macbook-pro Current % file Python
Python: Mach-O 64-bit dynamically linked shared library arm64



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved