Search code examples
cmakeboost-python

CMake target_link_libraries for boost-python does not find the .lib but the variable ${PYTHON_LIBRARIES} points to it


I am using a simple CMake (3.17.3) project to check my boost-python (1.73) installation via vcpkg (2020.02.04-nohash) on a windows 10 machine using VSCode (1.46.0). The CMake file is

cmake_minimum_required(VERSION 3.5)

find_package(PythonLibs 3.8 REQUIRED)
find_package(Boost COMPONENTS python38 REQUIRED)
set(CMAKE_SHARED_MODULE_PREFIX "")
add_library(hello_ext MODULE hello_ext.cpp)

target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(hello_ext PRIVATE ${Boost_LIBRARIES} ${PYTHON_INCLUDE_DIRS})

The error I get when I build this is LINK : fatal error LNK1104: cannot open file 'python38.lib' [E:\Code\python_starter\build\hello_ext.vcxproj]

The ${PYTHON_LIBRARIES} variable is optimizedE:/SD/Programming/C++/vcpkg/installed/x64-windows/lib/python38.libdebugE:/SD/Programming/C++/vcpkg/installed/x64-windows/debug/lib/python38_d.lib and a manual check confirms python38.lib is where this variable assumes it is.

I'm stumped. target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) does not seem to find python38.lib but it is there in the variable ${PYTHON_LIBRARIES}

I feel I'm missing something super obvious. I'm pretty new to cmake so go easy on me please!

*** EDIT: If I 'hard code' the link to python.lib, the project builds i.e.

#target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_link_libraries(hello_ext E:/SD/Programming/C++/vcpkg/installed/x64-windows/lib/python38.lib ${Boost_LIBRARIES})

builds fine.


Solution

  • I think I have the solution. According to FindPythonLibs the find_package(PythonLibs 3.8 REQUIRED) method to find the relevant python environment variables is deprecated and needs to be replaced with find_package(Python COMPONENTS Interpreter Development) With this, the capitalisation in target_include_directories macro changes from ${PYTHON_LIBRARIES} to ${Python_LIBRARIES}