How to copy .pdb files of Qt dynamic libraries with CMake?

63 Views Asked by At

How to copy .pdb files of Qt dynamic libraries with CMake?

This is how I copy .dll files:

add_custom_command(TARGET myproject POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:Qt6::Core>
        $<TARGET_FILE_DIR:myproject>/$<TARGET_FILE_NAME:Qt6::Core>
)

I tried TARGET_PDB_FILE:

add_custom_command(TARGET myproject POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_PDB_FILE:Qt6::Core>
        $<TARGET_FILE_DIR:myproject>/>
)

Error:

CMake Error at CMakeLists.txt:44 (add_custom_command):
  Error evaluating generator expression:

    $<TARGET_PDB_FILE:Qt6::Core>

  TARGET_PDB_FILE not allowed for IMPORTED targets.

I tried TARGET_FILE_BASE_NAME (same for TARGET_LINKER_FILE_BASE_NAME):

add_custom_command(TARGET disasm-gui POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE_BASE_NAME:Qt6::Core>.pdb
        $<TARGET_FILE_DIR:disasm-gui>
)

Error:

Error copying file (if different) from "Qt6::Core.pdb" to "C:/myproject/build/Debug".

Why is it named like this?

How I use cmake:

set CMAKE_PREFIX_PATH=C:\Qt\6.6.0\msvc2019_64

mkdir build
cd build
cmake ..
cmake --build .

My files:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.26)

project(myproject VERSION 0.1 LANGUAGES CXX)

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

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets)

set(PROJECT_SOURCES
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
)

qt_add_executable(myproject MANUAL_FINALIZATION ${PROJECT_SOURCES})

target_link_libraries(myproject PRIVATE Qt6::Widgets)

set_target_properties(myproject PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS myproject
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

qt_finalize_executable(myproject)


if(MSVC)
    add_custom_command(TARGET myproject POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt6::Core> $<TARGET_FILE_DIR:myproject>/$<TARGET_FILE_NAME:Qt6::Core>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt6::Gui> $<TARGET_FILE_DIR:myproject>/$<TARGET_FILE_NAME:Qt6::Gui>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt6::Widgets> $<TARGET_FILE_DIR:myproject>/$<TARGET_FILE_NAME:Qt6::Widgets>
        COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt6::QWindowsIntegrationPlugin> $<TARGET_FILE_DIR:myproject>/plugins/platforms/$<TARGET_FILE_NAME:Qt6::QWindowsIntegrationPlugin>
    )

    # TODO: copy pdb for debug?
endif()

main.cpp:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    ui = std::make_unique<Ui::MainWindow>();
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <memory>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    std::unique_ptr<Ui::MainWindow> ui;
};

#endif // MAINWINDOW_H

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar"/>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
0

There are 0 best solutions below