0

I'm testing my cmake python-extension setup utility and encountered an odd behavior that I cannot seem to resolve on my own.

In essence, my CMakeLists.txt boils down to 2 lines:

find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
...
Python3_add_library (${TARGET} SHARED ${SRCS})

When I try to build in Debug mode, MSVC 2019 spew a linker error:

LINK : fatal error LNK1104: cannot open file 'python37_d.lib'

I'm using conda, and I'm aware that I don't have the debug library on my PC. So, I started digging around to see if I could circumvent that: mainly just to link to the release lib, python37.lib...

With the help of this stackoverflow post, I printed all the target properties and found the relevant entry:

mymath LINK_LIBRARIES = Python3::Python

which indicate the use of the IMPORTED target FindPython3 configures. So, I printed its properties as well and found:

Python3::Python IMPORTED_IMPLIB = C:/Users/tikum/miniconda3/libs/python37.lib

Being puzzled, I looked in the corresponding .vcxproj in the build directory and found

<ImportLibrary>
C:/Users/tikum/Documents/Python/python-cmaketools-cpython-example/build/src/cpython_example/mymath/Debug/mymath.lib
</ImportLibrary>

So, I completely and utterly failed to find any trace of linking to python37_d.lib... Could someone enlighten me on what's going on here?

P.S., the example C code I'm trying to compile is a line-by-line copy of a sample by Martino Pilia that I found online.

kesh
  • 4,515
  • 2
  • 12
  • 20
  • https://stackoverflow.com/questions/56798472/force-linking-against-python-release-library-in-debug-mode-in-windows-visual-stu – CristiFati Jun 06 '20 at 17:28
  • @CristiFati - I saw that post earlier but it's a no-go. I get a configure-time error: ```The keyword signature for target_link_libraries has already been used with the target "mymath". All uses of target_link_libraries with a target must be either all-keyword or all-plain.``` – kesh Jun 06 '20 at 18:01
  • You could try: https://stackoverflow.com/questions/59126760/building-a-python-c-extension-on-windows-with-a-debug-python-installation – CristiFati Jun 06 '20 at 18:10
  • @CristiFati - ya, that points to the solution in the same vein of the answer below. – kesh Jun 06 '20 at 18:16

1 Answers1

0

You have to undefine DEBUG or _DEBUG, can't remember, around the python.h include in your code, since Python is linked with pragma directives. So it has nothing to do with cmake.

Something like this

#ifdef DEBUG
#undef DEBUG
#include <Python.h>
#define DEBUG
#else
#include <Python.h>
#endif

Should do the trick

  • Actually, I've already tried that already (although only with `_DEBUG` then but `DEBUG also doesn't work). – kesh Jun 06 '20 at 17:55
  • Strange that's what we use in our code. The python dll is linked using #pragma comment(lib, "python....lib"). Try to locate that code in the python headers and you'll find the correct variable to undefine. That's the way to do it on Windows. – Damien LEFEVRE Jun 06 '20 at 18:06
  • OK, I must have tried it wrong earlier, undef `_DEBUG` indeed did the trick! Thanks – kesh Jun 06 '20 at 18:08
  • Yeah, I saw the #pragma line when I looked deeper (I had no idea you can link like this 'til today, pretty nifty) – kesh Jun 06 '20 at 18:10