0

In a Windows QT application, I've successfully managed to call SetUnhandledExceptionFilter() and write out a minidump once the program crashes unexpectedly. However, as I've learnt here, we need to instruct the compiler to create a "Program-Debug Data Base" (pdb file) with vital information for post-mortem code debugging, e.g. method and symbol names etc. Otherwise, the dump is kind of worthless when opened in WinDbg, as we don't get any meaningful stack traces and I need to see at which line my program crashed exactly. So my goal here is to generate.pdb files alongside the release build (to store them in a private archive for possible debugging scenarios when trying to debug a crash on the customer side).

I'm using a CMakeLists.txt file in QT Creator and don't get any .pdb files upon building. Based on this forum post answer and this answer, I came up with this:

if (${CMAKE_BUILD_TYPE} STREQUAL "MinSizeRel")
    message("Configure with PDB debug information")
    set(QMAKE_CLFAGS_RELEASE "${QMAKE_CFLAGS_RELEASE} /Zi")
    set(QMAKE_LFLAGS_RELEASE "${QMAKE_LFLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif()

Unfortunately, this does not work, i.e. in my out folder under the respective release build folder, I do not find any .pdb files. The if(...) clause should not be the problem, as I see the message "Configure with PDB debug information" in the "General Messages" section of QT Creator when the project is built.

I've also tried to add

set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/pdb")
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/pdb")

with no luck. I also had no luck with:

add_compile_options("/Zi")
add_link_options("/DEBUG")
add_link_options("/OPT:REF /OPT:ICF")

The values QMAKE_CLFAGS_RELEASE and QMAKE_LFLAGS_RELEASE are correctly populated, so I don't know what I'm missing to get the .pdb files.

Note that I've also seen this answer, however, I don't know how to get this to work with the classic CMakeLists.txt

Edit: Here is my current CMakeLists.txt file in Qt Creator.

cmake_minimum_required(VERSION 3.5)

project(ProjectName VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (${CMAKE_BUILD_TYPE} STREQUAL "MinSizeRel")
    message("Configure with PDB debug information")
    set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/pdb")
    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Zi /DEBUG")
    set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /Zi /DEBUG")
endif()

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(Qt6 REQUIRED COMPONENTS Network)

set(PROJECT_SOURCES
        ... (cpp and header files list of my project)
)

# https://doc.qt.io/qt-6/qmessagelogcontext.html
# By default, QMessageLogContext only captures in Debug mode,
# we change this so that this information is still available in other modes, e.g. Release mode
add_definitions("-DQT_MESSAGELOGCONTEXT")

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(ProjectName
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
else()
    add_executable(ProjectName
        ${PROJECT_SOURCES}
    )
endif()

target_link_libraries(ProjectName
    PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
    PRIVATE Qt6::Network
)

set_target_properties(ProjectName PROPERTIES
    WIN32_EXECUTABLE TRUE
)

install(TARGETS ProjectName
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(ProjectName)
endif()

While building the project, I get this error (multiples times):

g++.exe: warning: /Zi: linker input file unused because linking not done
g++.exe: error: /Zi: linker input file not found: No such file or directory
g++.exe: warning: /DEBUG: linker input file unused because linking not done
g++.exe: error: /DEBUG: linker input file not found: No such file or directory
ninja: build stopped: subcommand failed.
14:11:19: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 1.
Error while building/deploying project ProjectName (kit: Desktop Qt 6.3.1 MinGW 64-bit)
When executing step "Build"

The issue pane in Qt contains these messages multiple times:

/Zi: linker input file unused because linking not done
error: /Zi: linker input file not found: No such file or directory
/DEBUG: linker input file unused because linking not done

The build commands passed to g++ by Qt are as follows:

C:\Qt\Tools\mingw1120_64\bin\g++.exe -DMINGW_HAS_SECURE_API=1 -DQT_CORE_LIB -DQT_GUI_LIB -DQT_MESSAGELOGCONTEXT -DQT_NEEDS_QMAIN -DQT_NETWORK_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DUNICODE -DWIN32 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_UNICODE -D_WIN64 -IE:/project/path/out/minsizerelease -IE:/project/path/src -IE:/project/path/out/minsizerelease/ProjectName_autogen/include -isystem C:/Qt/6.3.1/mingw_64/include/QtCore -isystem C:/Qt/6.3.1/mingw_64/include -isystem C:/Qt/6.3.1/mingw_64/mkspecs/win32-g++ -isystem C:/Qt/6.3.1/mingw_64/include/QtWidgets -isystem C:/Qt/6.3.1/mingw_64/include/QtGui -isystem C:/Qt/6.3.1/mingw_64/include/QtNetwork -Os -DNDEBUG /Zi /DEBUG -std=gnu++17 -MD -MT CMakeFiles/ProjectName.dir/qtutil/qslotthrower.cpp.obj -MF CMakeFiles\ProjectName.dir\qtutil\qslotthrower.cpp.obj.d -o CMakeFiles/ProjectName.dir/qtutil/qslotthrower.cpp.obj -c E:/project/path/src/qtutil/qslotthrower.cpp
Splines
  • 550
  • 1
  • 6
  • 17
  • where did you place your `add_compile_options`? Did you place it _before_ the definition of the targets you want it to apply to? (because you need to) – starball Mar 28 '23 at 02:01
  • Also, if you're trying to define you own configuration type, see https://stackoverflow.com/q/11437692/11107541, https://stackoverflow.com/q/75823363/11107541, and https://stackoverflow.com/q/43746829/11107541 – starball Mar 28 '23 at 02:04
  • @user Yes, I placed it before the `qt_add_executable()`, `target_link_libraries()` and `install()` calls. – Splines Mar 28 '23 at 02:09

1 Answers1

1

The post and the answer you referred are all for qmake, not CMake. They would only work if you set them in a .pro file (not a CMakeLists.txt) and use qmake as your build system. If you're using CMake, variables like QMAKE_CLFAGS_RELEASE and QMAKE_LFLAGS_RELEASE won't affect CMake at all.

A bit off-topic: unless you have to deal with old projects that still use qmake, you should always stick to CMake since it's the de facto standard C++ build system and Qt/QtCreator now use CMake by default, too.

To generate separate debug information in the Release build, you could add the following snippet to your CMakeLists.txt:

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi /DEBUG")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Zi /DEBUG")

# If it's the MinSizeRel build, then uncomment the following instead:
# set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Zi /DEBUG")
# set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /Zi /DEBUG")

# Or apply the flags to all build types:
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zi /DEBUG")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Zi /DEBUG")

# Add your project's source files and build targets here...

That being said, the add_compile_options/add_link_options should also work (though the modern CMake way is using target_compile_options/target_link_options). I doubt you missed something in the CMakeLists.txt.

See:


Edit: I assumed you're using MSVC since you're developing Windows app, but this is not the case. /Zi and /Debug both are compiler flags for MSVC and minGW won't recognize them, this is where all the compiling errors stem from.

You should use -g instead to generate debug info with MinGW. See Can I make cmake always generate debugging symbols?.

Note MinGW stores debug info in its own format rather than the Microsoft PDB. If you really want to generate PDB, I do recommend you switch to using MSVC for convenience (and remember to use a MSVC compiled Qt, too). See the discussion in how to generate pdb files while building library using mingw?

tanjor
  • 71
  • 2
  • 4
  • Thank you, didn't know that `QMAKE_CLFAGS` don't have any effect when using CMake ;) While CMake picks up the argument correctly now, I cannot build the project anymore and get a lot of "error /Zi linker input file not found: No such file or directory" and likewise for the `/DEBUG` flag and it's hard to find any answers for this problem online. – Splines May 09 '23 at 14:53