CMake Duplicating Debug Library and not including normal library

Hi! I’m experiencing some strange behavior with CMake’s Visual Studio makefile generator.

I have a project that includes Python3’s libraries through the normal methods, and it successfully finds both the debug and normal version of the libraries (optimized;C:/Program Files/Python38/libs/python38.lib;debug;C:/Program Files/Python38/libs/python38_d.lib)

However, when I run cmake with the VS (16 2019) generator, in the additional dependencies section, C:/Program Files/Python38/libs/python38_d.lib is included twice and C:/Program Files/Python38/libs/python38.lib is never included.

This results in this LNK1104 linker error. If I manually change one of the _d's to the optimized version, it compiles fine, so it’s not a back-breaking issue but it is rather inconvenient.

@marc.chevrier Any idea what might be happening here?

From the stackoverflow description, I think the problem comes from the mix of Boost.Python3 and Python3 libraries…

I don’t have boost installed, so I cannot validate my hypothesis but if I specify twice python libraries, in the vcxproj, there is a double dependency which is not quite optimal even if debug and release modes are correctly handled (double debug library for debug mode and double release library for release modes).

I don’t think the issue has anything to do with Boost. I commented out all the lines with Boost and I still have the debug library included twice and the normal library not included at all.

Definitively, I don’t understand what is exactly your problem.

Let me explain the expected behavior regarding keywords debug and optimized for libraries:

  • For debug builds (CMAKE_BUILD_TYPE=Debug for mono config generators or debug type for multi configs generators (Visual Studio in your case)), the library specified after debug keyword is selected and only this library.
  • For release builds (CMAKE_BUILD_TYPE=Release or RelWithDebInfo, etc… for mono config generators or release type for multi configs generators (Visual Studio in your case)), the library specified after optimized keyword is selected and only this library.

So, in the .vcxproj file, under Debug section, it is absolutely normal that optimized build is not included. Now for the duplication of python38_d.lib value, it is due to the fact that the library is specified multiple times in your CMake files.

Sure, I get that the debug library should be the one included for the debug build. However, when I try to build with MSBuild.exe, it fails to link because it’s trying to link with the non-debug version. “Correcting” it in the .vcxproj just gives it the non-debug library to link against, but in reality it should never even be trying to link against the non-debug library.

This means you have, in your project, a dependency on some code compiled in release mode against Python API. The Python API use #pragma comment(lib,...) declarations to force link of debug or release library…

Python 3.9 will have a mechanism to disable this behavior. https://bugs.python.org/issue38728