[UPDATE 2]
The following two lines, when added to my CMake file, successfully found python 3 and its libraries. 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.
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. When running from CLion, I get the following error:
CMake Error: The following variables are used in this project, but
they are set to NOTFOUND. Please set them or make sure they are set
and tested correctly in the CMake files: 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. On ubuntu this is working fine however, on macOS, I can't seem to get cmake to recognize pythonlibs3.
On ubuntu the following line works:
FIND_PACKAGE(PythonLibs 3 REQUIRED)
However, on macOS, it can only fine pythonlibs 2.7.10 in /usr/libs/
I have tried the following:
Using a python3 virtual environment and then running cmake.
adding set(Python_ADDITIONAL_VERSIONS 3.6) to my cmake
Other info:
I installed python3 (3.6) with brew, and it is located in /usr/local/bin
I am using cmake version 3.6.3
When I write FIND_PACKAGE(PythonInterp 3) cmake is able to find my python3 installation.
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/. This seems to be because this file is located /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,
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"
(requested version 3) with any of the following names:
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. If "PythonLibs" provides a separate development package or
SDK, be sure it has been installed.
Additionally, if I try to set the python variables without using find_package, it is able to find the library:
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:
PYTHON_ADD_MODULE(${PYRITMO_LIB} src/pythonwrappers.cpp)
The error reads:
Unknown CMake command "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.
解决方案
As stated above in the update to the question, moving to CMake 3.7.2 and using the following two lines fixed my issues:
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. 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.
If these two lines are placed before the Google Test install code, then python3 will be found and used for google test.