pythonc++qtpyside6

shiboken6: constexpr variable must be initialized by a constant expression


I am trying to build some python wrappers using shiboken6 and Pyside6. I have managed to get the code compiling on a single machine, but now that I migrated the code to a build server, I am getting a compile error from within the shiboken compiler:

QtCore/qplugin.h(146,33): error G3F63BFAE: constexpr variable 'HeaderOffset' must be initialized by a constant expression

This code seems to work perfectly fine on the reference machine, but I do not know in what way the setup differs. I installed a conda environment from the .yml file generated on the working machine to the new machine:

channels:
  - defaults
dependencies:
   ... // omitted most packages for brevity
  - pip=24.0=py312haa95532_0
  - pybind11-abi=5=hd3eb1b0_0
  - python=3.12.4=h14ffc60_1
  ... // omitted most packages for brevity
  - pip:
      - pyside6==6.7.2
      - shiboken6==6.7.2
      - shiboken6-generator==6.7.2

I am passing the following options to shiboken:

C:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/shiboken6_generator/shiboken6.exe 
--generator-set=shiboken 
--compiler=msvc
--enable-parent-ctor-heuristic
--enable-return-value-heuristic
--use-isnull-as-nb_nonzero
--enable-pyside-extensions
--avoid-protected-hack
--debug-level=full
--output-directory=build/x64/Release/
-TD:/myrepo/myproject/src/core/python_core/
-TC:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/PySide6/typesystems
-ID:/myrepo/myproject/src/core/python_core/
-IC:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/shiboken6_generator/include
-IC:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/PySide6/include
-IC:/ProgramData/Miniconda3/envs/myenv/include
-IC:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/PySide6/include/QtCore 
-IC:/ProgramData/Miniconda3/envs/myenv/Lib/site-packages/numpy/core/include 
-IC:/Qt/6.7.2/msvc2019_64/include/QtXml 
-IC:/Qt/6.7.2/msvc2019_64/include/QtGui 
-IC:/Qt/6.7.2/msvc2019_64/include/QtCore
-IC:/Qt/6.7.2/msvc2019_64/include/QtQml
-IC:/Qt/6.7.2/msvc2019_64/include 
bindings.h 
bindings.xml

Which should be fairly standard. I am not sure what causes the problem, as the definition of offsetof is identical between the two machines:

#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
    #ifdef __cplusplus
        #define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
    #else
        #define offsetof(s,m) ((size_t)&(((s*)0)->m))
    #endif
#else
    #define offsetof(s,m) __builtin_offsetof(s,m)
#endif

It confuses me that I find questions related to reinterpret_cast not being part of a valid constexpr (see this other stackoverflow post). Which would mean that no matter what this should not compile. One machine does, however and the other does not.

Note that the --compiler option was only added as an attempt to fix the issue and changes the error. When nothing is provided or when the option msvc is passed, I get the error above. Passing the value --compiler=clang results in the even more cryptic error

C1083 Cannot open source file 'c++'.

I am using Visual Studio 2019 (version 16.11.35) to compile the resulting python wrapper code and Qt 6.7.2 on both setups.

The only related issue I could find is this one from an ITK mailing list from 2017: https://itk.org/pipermail/community/2017-May/013075.html quote: "it is a known bug in VS2017"

Does anyone have an idea what the problem could be? Is this a bug in qplugin.h? Or am I missing a configuration that happens to be present on one machine, but is not on the other? Is this a problem with MSVC?


Solution

  • I did not find the cause of the problem, but found a workaround. Passing --clang-options=-D _CRT_USE_BUILTIN_OFFSETOF fixes the problem. Weirdly enough, I was under the impression that shiboken6 uses clang internally, so I don't know why this lets it use the clang implementation and before it doesn't.