简体   繁体   English

Pythonlibs3 CMake和macOS

[英]Pythonlibs3 CMake and macOS

[UPDATE 2] [更新2]

The following two lines, when added to my CMake file, successfully found python 3 and its libraries. 以下两行,当添加到我的CMake文件中时,成功找到了python 3及其库。 The reason this was only working in the terminal was because CLion was using its bundled version of CMake (3.6.3) and the updated version my terminal was using (3.7.2) correctly finds python. 这只是在终端工作的原因是因为CLion正在使用其捆绑版本的CMake(3.6.3)和我终端使用的更新版本(3.7.2)正确找到python。

FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3)

[UPDATE] I got the cmake file to work, however, it only finds the python3 library when I run from the terminal. [UPDATE]我得到了cmake文件,但是,当我从终端运行时它只找到python3库。 When running from CLion, I get the following error: 从CLion运行时,我收到以下错误:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND. CMake错误:此项目中使用以下变量,但它们设置为NOTFOUND。 Please set them or make sure they are set and tested correctly in the CMake files: PYTHON_LIBRARY (ADVANCED) 请设置它们或确保它们在CMake文件中正确设置和测试:PYTHON_LIBRARY(ADVANCED)

[ORIGINAL POST] [原始帖子]

I am developing a cross platform C++ application and using PythonLibs 3 along with boost_python to be able to call c++ methods from python. 我正在开发一个跨平台的C ++应用程序,并使用PythonLibs 3和boost_python来从python调用c ++方法。 On ubuntu this is working fine however, on macOS, I can't seem to get cmake to recognize pythonlibs3. 在ubuntu这工作正常但是,在macOS上,我似乎无法让cmake识别pythonlibs3。

On ubuntu the following line works: 在ubuntu上,以下行有效:

FIND_PACKAGE(PythonLibs 3 REQUIRED)

However, on macOS, it can only fine pythonlibs 2.7.10 in /usr/libs/ 但是,在macOS上,它只能在/usr/libs/ pythonlibs 2.7.10。

I have tried the following: 我尝试过以下方法:

  1. Using a python3 virtual environment and then running cmake. 使用python3虚拟环境然后运行cmake。
  2. adding set(Python_ADDITIONAL_VERSIONS 3.6) to my cmake set(Python_ADDITIONAL_VERSIONS 3.6)添加到我的cmake

Other info: 其他信息:

  • I installed python3 (3.6) with brew, and it is located in /usr/local/bin 我用brew安装了python3(3.6),它位于/ usr / local / bin中
  • I am using cmake version 3.6.3 我使用的是cmake版本3.6.3
  • When I write FIND_PACKAGE(PythonInterp 3) cmake is able to find my python3 installation. 当我写FIND_PACKAGE(PythonInterp 3) cmake能够找到我的python3安装。
  • When checking my /usr/lib/ folder, I found libpython2.7.dylib but I do not have a libpython3.6/dylib in either /usr/lib/ or /usr/local/lib/ . 检查我的/usr/lib/文件夹时,我找到了libpython2.7.dylib但我在/usr/lib//usr/local/lib/没有libpython3.6/dylib This seems to be because this file is located /usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib . 这似乎是因为这个文件位于/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib

If I add the location of libpython3.6 to my find_package, 如果我将libpython3.6的位置添加到我的find_package中,

FIND_PACKAGE(PythonLibs 3 PATHS /usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/ REQUIRED)

it is able to find the library but then I get the error: 它能够找到库但我得到错误:

Could not find a package configuration file provided by "PythonLibs" 找不到“PythonLibs”提供的包配置文件
(requested version 3) with any of the following names: (请求的版本3)具有以下任何名称:

 PythonLibsConfig.cmake pythonlibs-config.cmake 

Add the installation prefix of "PythonLibs" to CMAKE_PREFIX_PATH or set "PythonLibs_DIR" to a directory containing one of the above files. 将“PythonLibs”的安装前缀添加到CMAKE_PREFIX_PATH或将“PythonLibs_DIR”设置为包含上述文件之一的目录。 If "PythonLibs" provides a separate development package or SDK, be sure it has been installed. 如果“PythonLibs”提供单独的开发包或SDK,请确保它已安装。

Additionally, if I try to set the python variables without using find_package, it is able to find the library: 另外,如果我尝试在不使用find_package的情况下设置python变量,它可以找到库:

SET(PYTHON_INCLUDE_PATH /usr/local/include/python3.6mu) SET(PYTHON_EXECUTABLE /usr/local/bin/python3.6mu) SET(PYTHON_INCLUDE_DIR /usr/local/include/python3.6mu) SET(PYTHON_LIBRARIES /usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/)

but later on in my cmake I get an error on the following line: 但是后来我的cmake中出现了以下错误:

PYTHON_ADD_MODULE(${PYRITMO_LIB} src/pythonwrappers.cpp)

The error reads: 错误如下:

Unknown CMake command "PYTHON_ADD_MODULE". 未知的CMake命令“PYTHON_ADD_MODULE”。

The reason for this appears to be because this function is provided by FindPythonLibs.cmake which is loaded in by find_package(Pythonlibs) and therefore, if this is not used to located PythonLibs, this function cannot be called. 原因似乎是因为此函数由FindPythonLibs.cmake提供,它由find_package(Pythonlibs)加载,因此,如果不使用它来定位PythonLibs,则无法调用此函数。

As stated above in the update to the question, moving to CMake 3.7.2 and using the following two lines fixed my issues: 如上所述,在问题更新中,转到CMake 3.7.2并使用以下两行修复了我的问题:

FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3)

[UPDATE] For anyone using Google Test and installing it via CMake, it is important to put the above lines before the Google Test code. [更新]对于使用Google Test并通过CMake安装的任何人,将上述行放在Google Test代码之前非常重要。 This is because Google test will look for python, and find python2, then when these two lines are run, they will not be able to find python 3. 这是因为Google测试会查找python,并找到python2,然后当这两行运行时,他们将无法找到python 3。

If these two lines are placed before the Google Test install code, then python3 will be found and used for google test. 如果这两行放在Google Test安装代码之前,则会找到python3并用于google测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM