如何在子目录项目中调用共享的类成员函数?

时间:2019-07-17 17:30:28

标签: c++ qt

我有一个名为Project的Qt子目录项目。

# Project.pro
TEMPLATE = subdirs

SUBDIRS += \
    Project-core \
    Project-gui


Project-gui.depends = Project-core

Project-gui必须从Project-core调用一个函数,因此我按照website所示链接了项目:

  

您可以在Qt Creator中右键单击您的项目,然后选择“添加库...”,选择“外部库”并浏览您的库文件:

我这样做了,但是我选择了“内部库”而不是“外部库”。现在,我可以在Project-core中包含Project-gui的头文件,并且将其中一个类标记为Q_DECL_EXPORT。我试图调用此类的(静态)成员函数。但是现在,当我要编译项目时,编译器的输出是一个漫长而复杂的错误消息:

21:14:15: Running steps for project Project...
21:14:15: Configuration unchanged, skipping qmake step.
21:14:15: Starting: "C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\mingw32-make.exe" -j8
cd Project-core\ && ( if not exist Makefile C:\Qt\Qt5.12.1\5.12.1\mingw73_64\bin\qmake.exe -o Makefile C:\Users\Usr\Desktop\Project\Alpha\Project\Project-core\Project-core.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug" ) && C:/Qt/Qt5.12.1/Tools/mingw730_64/bin/mingw32-make -f Makefile 
mingw32-make[1]: Entering directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-core'
C:/Qt/Qt5.12.1/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[2]: Entering directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-core'
mingw32-make[2]: Nothing to be done for 'first'.
mingw32-make[2]: Leaving directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-core'
mingw32-make[1]: Leaving directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-core'
cd Project-gui\ && ( if not exist Makefile C:\Qt\Qt5.12.1\5.12.1\mingw73_64\bin\qmake.exe -o Makefile C:\Users\Usr\Desktop\Project\Alpha\Project\Project-gui\Project-gui.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug" ) && C:/Qt/Qt5.12.1/Tools/mingw730_64/bin/mingw32-make -f Makefile 
mingw32-make[1]: Entering directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-gui'
C:/Qt/Qt5.12.1/Tools/mingw730_64/bin/mingw32-make -f Makefile.Debug
mingw32-make[2]: Entering directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-gui'
mingw32-make[2]: Nothing to be done for 'first'.
mingw32-make[2]: Leaving directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-gui'
mingw32-make[1]: Leaving directory 'C:/Users/Usr/Desktop/Project/Alpha/Project/Project-gui'
21:14:20: The process "C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\mingw32-make.exe" exited normally.
21:14:20: Elapsed time: 00:05.

如果我不调用该函数,该错误就会消失。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

Q_DECL_EXPORT(或PROJECTCORESHARED_EXPORT)添加到该方法使用的所有类中,即可解决此问题。