Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link with Python3 Libs with cmake?

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.

like image 716
Fabian Avatar asked Mar 25 '15 00:03

Fabian


2 Answers

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

like image 126
Fellow Traveler Avatar answered Oct 20 '22 18:10

Fellow Traveler


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
like image 36
OlivierM Avatar answered Oct 20 '22 20:10

OlivierM