Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does CMake support Python3?

I am not able to build a Python wrapper module for my C library via CMake and Swig for Python3. Everything works fine for Python2.x but it looks like CMake cannot find Python3. I already looked around and tried a couple of things.

For example, my python executable links to Python3, as I read CMake will find this version first.

Please see here the SWIG part of the CMakeLists.txt:

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonInterp 3)
FIND_PACKAGE(PythonLibs 3)

FIND_PATH(PYTHON_INCLUDE_PATH Python.h
  /usr/include
  /usr/local/include)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(kissCT3.i PROPERTIES CPLUSPLUS ON)
#SET_SOURCE_FILES_PROPERTIES(kissCT3.i PROPERTIES SWIG_FLAGS "-includeall -py3")

SWIG_ADD_MODULE(kissCT3 python kissCT3.i)
SWIG_LINK_LIBRARIES(kissCT3 libct2d matio kissfft ${PYTHON_LIBRARIES})

Unfortunately, the output after calling cmake shows that only python2.7 is found:

-- Found SWIG: /usr/bin/swig2.0 (found version "2.0.4")
-- Found PythonInterp: /usr/bin/python2.7 (Required is at least version "3")
-- Found PythonLibs: /usr/lib/libpython2.7.so (Required is at least version "3")
-- Configuring done
-- Generating done
like image 451
mijc Avatar asked Jul 17 '13 08:07

mijc


People also ask

Can you use CMake with Python?

CMake, the cross-platform build system generator, is now easily installable in Python distributions!

Why is CMake used in Python?

CMake Python Distributions CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

What is include in CMake?

include(<file|module> [OPTIONAL] [RESULT_VARIABLE <var>] [NO_POLICY_SCOPE]) Loads and runs CMake code from the file given. Variable reads and writes access the scope of the caller (dynamic scoping). If OPTIONAL is present, then no error is raised if the file does not exist.

What are CMake files?

CMake is an open-source, cross-platform tool that uses compiler and platform independent configuration files to generate native build tool files specific to your compiler and platform. The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.


2 Answers

Can you make sure your build directory is clean? I had the exact same issue and after cleaning the dir it worked.

like image 181
shadeslayer Avatar answered Sep 23 '22 04:09

shadeslayer


CMake 3.12 should help you with your issue. Citing the release notes:

New “FindPython3” and “FindPython2” modules, as well as a new “FindPython” module, have been added to provide a new way to locate python environments.

like image 38
usr1234567 Avatar answered Sep 19 '22 04:09

usr1234567