对QT :: RemoteObjects自动基因的const静态类成员字段(仅Windows)的未定义引用

发布于 2025-02-02 20:46:41 字数 3330 浏览 1 评论 0原文

由于Microsoft DELLSPEC“ feature”,我在Windows上重复使用共享库的问题有问题。在共享库中,我使用QT :: RemoteObjects自动化源或副本类的类,我无法在库代码之外的任何地方使用信号插槽功能。考虑以下小示例:

cmakelists.txt

cmake_minimum_required(VERSION 3.11.0)
project(QtSymbolsProblem CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

set (CMAKE_CXX_STANDARD 17)
find_package(Qt5 COMPONENTS RemoteObjects Core REQUIRED)

qt5_generate_repc(GENERATED PanelInterface.rep SOURCE)
set_source_files_properties(${GENERATED} PROPERTIES GENERATED TRUE SKIP_AUTOMOC TRUE)
message(STATUS "generated sources: ${GENERATED}")
add_library(SampleProcedure SHARED
    Procedure.cpp
    ${GENERATED}
)
set_target_properties(SampleProcedure PROPERTIES
    WINDOWS_EXPORT_ALL_SYMBOLS ON
)
target_link_libraries(SampleProcedure
  Qt5::RemoteObjects
  Qt5::Core
)
add_executable(main main.cpp)
target_link_libraries(main PUBLIC SampleProcedure)

replica定义panelinterface.rep

#include <QtCore>
#include <QString>

class PanelInterface
{
    PROP(QString foo);
};

库代码:

//// Procedure.hpp
#pragma once

#include <SampleProcedure_export.h>
#include <QObject>
#include <memory>

class PanelInterfaceSimpleSource;

class Procedure {
public:
  Procedure();
  ~Procedure();
  PanelInterfaceSimpleSource *panelInterface();

private:
  std::unique_ptr<PanelInterfaceSimpleSource> _panel;
};

//// Procedure.cpp
#include "Procedure.hpp"
#include "rep_PanelInterface_source.h"
#include <QObject>

Procedure::Procedure()
    : _panel(std::make_unique<PanelInterfaceSimpleSource>()) {}

Procedure::~Procedure() = default;

PanelInterfaceSimpleSource *Procedure::panelInterface() { return _panel.get(); }

main ostable:

#include "Procedure.hpp"
#include "rep_PanelInterface_source.h"
#include <QObject>

#include <iostream>

class Foo : public QObject {
  Q_OBJECT
public:
  Foo(Procedure &proc) {
    QObject::connect(  // this is impossible because of missing symbol
        proc.panelInterface(), &PanelInterfaceSimpleSource::fooChanged, this,
        [](QString str) { std::cout << str.toStdString() << std::endl; });
  }
};

int main() {
  Procedure proc;
  Foo foo(proc);
  return 0;
}

#include "main.moc"

在此示例code> panelInterfaceSimpleseimource是由由由自动基因生成的类QT。当我尝试连接到它的一个信号时,链接器给我错误:

main.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const PanelInterfaceSimpleSource::staticMetaObject" (?staticMetaObject@PanelInterfaceSimpleSource@@2UQMetaObject@@B)

cmake docs 提到

对于全局数据符号,__declSpec(dllimport)仍必须使用.dll中的代码进行编译。

我什至找到了其他一些主题提到CMAKE自动化导出标头的使用

include(GenerateExportHeader)
generate_export_header(SampleProcedure)

,但该解决方案只能与我可以修改的代码一起使用。

在QT自动化的代码中,我应该如何解决类静态变量的符号的问题?

I have the problem with reusing my shared libraries on windows, because of microsoft declspec "feature". In the shared library I have the class using QT::RemoteObjects autogenerated SOURCE or REPLICA classes, and I am not able to use signal-slot features anywhere outside my library code. Consider this small example:

CMakeLists.txt

cmake_minimum_required(VERSION 3.11.0)
project(QtSymbolsProblem CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

set (CMAKE_CXX_STANDARD 17)
find_package(Qt5 COMPONENTS RemoteObjects Core REQUIRED)

qt5_generate_repc(GENERATED PanelInterface.rep SOURCE)
set_source_files_properties(${GENERATED} PROPERTIES GENERATED TRUE SKIP_AUTOMOC TRUE)
message(STATUS "generated sources: ${GENERATED}")
add_library(SampleProcedure SHARED
    Procedure.cpp
    ${GENERATED}
)
set_target_properties(SampleProcedure PROPERTIES
    WINDOWS_EXPORT_ALL_SYMBOLS ON
)
target_link_libraries(SampleProcedure
  Qt5::RemoteObjects
  Qt5::Core
)
add_executable(main main.cpp)
target_link_libraries(main PUBLIC SampleProcedure)

Replica definition PanelInterface.rep:

#include <QtCore>
#include <QString>

class PanelInterface
{
    PROP(QString foo);
};

Library code:

//// Procedure.hpp
#pragma once

#include <SampleProcedure_export.h>
#include <QObject>
#include <memory>

class PanelInterfaceSimpleSource;

class Procedure {
public:
  Procedure();
  ~Procedure();
  PanelInterfaceSimpleSource *panelInterface();

private:
  std::unique_ptr<PanelInterfaceSimpleSource> _panel;
};

//// Procedure.cpp
#include "Procedure.hpp"
#include "rep_PanelInterface_source.h"
#include <QObject>

Procedure::Procedure()
    : _panel(std::make_unique<PanelInterfaceSimpleSource>()) {}

Procedure::~Procedure() = default;

PanelInterfaceSimpleSource *Procedure::panelInterface() { return _panel.get(); }

Main executable:

#include "Procedure.hpp"
#include "rep_PanelInterface_source.h"
#include <QObject>

#include <iostream>

class Foo : public QObject {
  Q_OBJECT
public:
  Foo(Procedure &proc) {
    QObject::connect(  // this is impossible because of missing symbol
        proc.panelInterface(), &PanelInterfaceSimpleSource::fooChanged, this,
        [](QString str) { std::cout << str.toStdString() << std::endl; });
  }
};

int main() {
  Procedure proc;
  Foo foo(proc);
  return 0;
}

#include "main.moc"

In this example PanelInterfaceSimpleSource is a class autogenerated by QT. When I try to connect to one of its signals, linker gives me the error:

main.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const PanelInterfaceSimpleSource::staticMetaObject" (?staticMetaObject@PanelInterfaceSimpleSource@@2UQMetaObject@@B)

In the CMake docs it is mentioned that

For global data symbols, __declspec(dllimport) must still be used when compiling against the code in the .dll.

and I even found some other topics that mention the use of CMake autogenerated export header

include(GenerateExportHeader)
generate_export_header(SampleProcedure)

but this solution will only work with the code that I am allowed to modify.

How should I solve the problem with missing symbol to the class static variable, in the code that was autogenerated by QT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文