How to link with Python3 Libs with cmake?
Asked Answered
C

3

15

I have Python3 installed via brew install python3. However, cmake cannot find PythonLibs 3. Here's the header of my CMakeLists.txt.

cmake_minimum_required(VERSION 3.0)
find_package(PythonLibs 3 REQUIRED)

When I ran cmake, I got this error message

Could NOT find PythonLibs: Found unsuitable version "2.7.6", but required is at least "3" (found /usr/lib/libpython2.7.dylib)

Not sure what I did wrong.

Coin answered 25/3, 2015 at 0:39 Comment(2)
Have a look at https://mcmap.net/q/682034/-does-cmake-support-python3 - it's a very similar problem. The simplest thing suggestion would be to manually set the paths to the Python headers and libraries, but it isn't a great solution. I know nothing about brew and how it affects things...Zantos
Thanks, the solution in that link works, although it no longer makes my code cross platform :(. And I asked another question on the same topic #29268218, if you don't mind answering. Thanks a lot @DavidW.Coin
M
7

Because you are using CMake >= 3.0, you can you find_package(Python COMPONENTS Interpreter Development) see: https://cmake.org/cmake/help/v3.12/module/FindPython.html

That would for instance give you for:

find_package(Python COMPONENTS Interpreter Development)

message("Python_FOUND:${Python_FOUND}")
message("Python_VERSION:${Python_VERSION}")
message("Python_Development_FOUND:${Python_Development_FOUND}")
message("Python_LIBRARIES:${Python_LIBRARIES}")

Results:

Python_FOUND:TRUE
Python_VERSION:3.8.0
Python_Development_FOUND:TRUE
Python_LIBRARIES:/usr/lib/x86_64-linux-gnu/libpython3.8.so
Maddalena answered 4/4, 2020 at 8:23 Comment(3)
me/qgb/github/binapi/examples/websockets/main.cpp /home/qgb/github/binapi/examples/websockets/main.cpp:20:10: fatal error: Python.h: No such file or directory 20 | #include <Python.h> | ^~~~~~~~~~ compilation terminated. [2/4] Building CXX object CMakeFiles/cashflow.dir/main.cpp.o ninja: build stopped: subLoeb
@CSQGB Installing the package libpython3-dev should solve your issue.Maddalena
I am using vcpkg and executed vcpkg install boost python3:x64-linuxLoeb
W
6

In my experience, this happened because I was using an older version of cmake (2.8 instead of 3+) that didn't know about Python 3.4 (it gave up after 3.3.)

The solution was to go into the CMakeLists.txt file and add an "additional versions" directive ABOVE the find_package:

set(Python_ADDITIONAL_VERSIONS 3.4) find_package(PythonLibs 3 REQUIRED)

You could probably also fix it by upgrading your version of cmake. But the above worked for me with cmake 2.8

Wommera answered 22/2, 2016 at 15:27 Comment(0)
J
0

Another reason for this is that CMake can't ever find Python 3 when it is installed from brew on OSX. It looks like the CMake devs know that FindPythonLibs sucks and have a ticket to revamp it but it doesn't look like it will happen any time soon.

I believe the Python interpreter itself knows where its libraries and headers are so I think the best thing to do would be to run it to find out. To get the path to the Python interpreter I would force the user to specify it manually. One of the big issues with Python is that lots of software includes its own copy so you'll end up with 5 copies of it on your system. The chance of picking up the wrong one is just too high. Get the user to specify the correct one.

Jamisonjammal answered 16/11, 2017 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.