Qt subproject can't find moc files - qt

I recently tried to write a unit test (with googletest) for a class including a Q_OBJECT macro and a self-defined signal. The test subproject won't compile (even after rebuilding/deleting everything) with the following linker errors:
"error: undefined reference to `vtable for Class'"
and
"error: undefined reference to `vtable for Class::signal()'"
My src subproject compiles just fine. After researching the issue I guess the problem is that the compiler doesn't generate moc files for the test subproject. Also I've not been successful to include the src subproject's moc files in test. How can I fix this?
Here are my .pro files:
Project .pro file
TEMPLATE = subdirs
CONFIG(debug, debug|release) {
SUBDIRS += \
src \
test
test.depends = src
} else {
SUBDIRS += \
src
QMAKE_CXXFLAGS += -O2
}
src.pro
QT += core gui charts widgets
TARGET = Project name
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
HEADERS += \
...
SOURCES += \
...
test.pro
include(gtest_dependency.pri)
QT += core
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG += thread
CONFIG += qt
INCLUDEPATH += $$PWD/../src
DEPENDPATH += $$PWD/../src
HEADERS += \
... (only test headers)
SOURCES += \
... (test and src source files)

I had the same problem. Making sure Qt mocs the src headers also for the test project seem to require to mention them in the test projects .pro file too. Thus adding something like:
HEADERS += ( dependent src headers )
If the tests need all the src headers they could be extracted into a common .pri file.

Related

How to fix 'LINK : fatal error LNK1181: cannot open input file 'Qt5Ftp.obj''?

I use Qt5.12.0, Qt Creator 4.8.0. So it doesn't contain QFtp but I need to use it. So I follow the way I found on the Internet, I put the .h, .lib, .prl, .dll files of Qt5Ftp to the corresponding directories in 'Qt\Qt5.12.0\5.12.0\msvc2017_64'. Then I built my project, the compiling process is successful, but there's an error in the linking process—
LINK : fatal error LNK1181: cannot open input file 'Qt5Ftp.obj'
I'd tried to rerun qmake, clean all, rebuild all, etc, everything I could try, but I still got this error.
Here's the content of the .pro file of my project—
QT += core gui network webenginewidgets
CONFIG += c++11
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
include(qtsingleapplication/src/qtsingleapplication.pri)
TARGET = YunQuBrowser
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp \
webview.cpp \
webpage.cpp \
ftpclient.cpp \
logfile.cpp \
cmdconfig.cpp
HEADERS += mainwindow.h \
webview.h \
webpage.h \
ftpclient.h \
logfile.h \
cmdconfig.h \
common.h
INCLUDEPATH += ./include
FORMS += mainwindow.ui
RESOURCES += \
YunQuBrowser.qrc \
RC_FILE = YunQuBrowserDesktopIcon.rc
CONFIG(debug, debug|release) {
LIBS += Qt5Ftpd
} else {
LIBS += Qt5Ftp
}
The last comment I post contains a mistake. The correct way is to change the whole CONFIG block into LIBS += -L"QFtp/qtftp_lib", which should be a directory, but not a path of a .lib file.

how to unit test a Qt application with QtTest using subdirs

I found a few other hits when searching for this particular problem, but i cant get my setup to work with my unit test project(s).
project info: ( optional reading)
I am in the beginning of my final undergraduate project, where I am to
create an IDE for the language Promula and the the
verification tool Spin. The professor I am doing this with expects to
use it in his classes and for future undergraduates to keep improving. It is also meant to be a open source project everyone can contribute to.
Spin tool
my folder struture:
QSpin
-QSpin.pro
-QSpinApplication
--QSpinapplication.pro
--QSpinApplication.pri
--include
---Workspace
----*.h
--src
---Workspace
----*.cpp
-QSpinApplicationTests
--QSpinApplicationTests.pro
--WorkspaceGroupTests
---WorkspaceGroupTests.pro
----some cpp testfile
My QSpinApplication compiles and runs without any problems. The import of what i have so can be imported.
#include <Workspace/(file_name).h>
However in my test project WorkspaceGroupTests i want to include headers in the same way, but i cant get it to work. Beside that i assume the test project need to know where the cpp files are. If i dont use any of the application headers the test project compiles and runs with the default test case.
at the moment i am using a default shadow build with its root folder in the QSpin folder.
Any idea of how i get my test project working correctly, to actual be able to test my application classes?
QSpin.pro:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
QSpinApplication\QSpinApplication.pro \
QSpinApplicationTests
QSpinApplication.pro:
QT += quick qml gui
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=src/main.cpp
include(QSpinApplication.pri)
RESOURCES += qml.qrc
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
QSpinApplication.pri:
#//DEPENDPATH += $$PWD
INCLUDEPATH += ./include
SOURCES +=src/workspace/workspaceview.cpp \
src/workspace/groupitem.cpp \
src/workspace/groupitemsplitter.cpp \
src/workspace/workspacegroup.cpp \
src/workspace/workspaceviewattached.cpp \
src/workspace/workspaceviewiteminfo.cpp \
src/workspace/workspacegrouplinknode.cpp
HEADERS += \
include/workspace/workspaceview.h \
include/workspace/groupitem.h \
include/workspace/groupitemsplitter.h \
include/workspace/workspacegroup.h \
include/workspace/workspaceviewattached.h \
include/workspace/workspace.h \
include/workspace/workspaceviewiteminfo.h \
include/workspace/workspacegrouplinknode.h
QSpinApplicationTests.pro:
TEMPLATE = subdirs
SUBDIRS += \
WorkspaceGroupTests
WorkspaceGroupTests.pro:
QT += testlib
QT -= gui
DEPENDPATH+=./../../QSpinApplication
include(./../../QSpinApplication/QSpinApplication.pri)
CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += tst_workspacegrouptests.cpp
Not sure if I understood well but in general, what I would advise is to create a separate structure for "lib" where you put logic code that can be tested, so default structure would look like that:
Structure:
├───app.pro
├───lib.pro
└───tests.pro
Then you put in your lib.pro:
TARGET = NameOfTarget
TEMPLATE = lib
CONFIG += staticlib
and add in your tests.pro, and in app.pro:
LIBS += -L../lib -lNameOfTarget
At end it should be possible without problems to include in app directory and in test:
#include "anyHeaderFromLib.h" .
As an extra addition for Michal Kalinowski answer:
Looks like this will work only for Linux/Mac, in case of Windows you should handle it on that way:
win32:CONFIG(release, debug|release): LIBS += -L../lib/debug -lNameOfTarget
else:win32:CONFIG(debug, debug|release): LIBS += --L../lib/release -lNameOfTarget
else:unix: LIBS += -L../lib -lNameOfTarget
Some more context can be get here: https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

qtcreator project file depndancies and build order

I am using qtcreator with subdir project structure. The case is that:
I have 2 projects. Project A and B. After A compiles, then B, but in B, I must use the headers of A (classes, functions, etc.). I`ve found in wiki the depends and subdir project setup, but when I try to include class A from project A into class B into project B ( these names are for convininece ) it gives me undefined referencies. Here is my .pro file from main project (and subprojects respectively ):
#base pro file
TEMPLATE = subdirs
SUBDIRS += \
message \
daemon \
receiver
daemon.subdir = daemon
message.subdir = message
daemon.depends = message
subproject A:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp \
daemon.cpp \
logwriter.cpp
HEADERS += \
daemon.h \
logwriter.h \
defs.h
LIBS += -lpthread
subproject B:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
message.cpp \
main.cpp
HEADERS += \
message.h
So I need the project B classes into project A and further on when I extend the project.
Regards.
EDIT: A .pri example would be appreciated, if I'm going to set project A to be a library (-lclssA )
"undefined references" are generally link problems, not header include problems. You may have to link your project A with B if you need to use it.
e.g in A :
LIBS += ../path/to/libB.so
The simplest way is to include it as a lib, the easiest way was from qtcreator, rightclick on the dependee and "Add library" -> "Existing library from build tree". This will generate the following in the .pro:
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../message/release/ -lmessage
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../message/debug/ -lmessage
else:unix: LIBS += -L$$OUT_PWD/../message/ -lmessage
INCLUDEPATH += $$PWD/../message
DEPENDPATH += $$PWD/../message
However I am losing the ability to make tests for my message class in it's main class. So if there is a better way, I'd accept it.

qmake not generating binary resource files

Specs: Qt Creator 2.5.1, Qt 4.7.4(32bit)
It is my understanding from the Qt documentation that including a resource (.qrc) in the .pro file of Qt is all that is required to get qmake to generate the correct qrc_*.cpp files. That doesn't seem to be the case for my project. When running qmake the makes files are created and there are references to the qrc_ file I'm looking for under compiler_rcc_clean, just not in the build chain.
[Update]
At a suggestion from comments below the makefile generated by qmake was tested directly w/ NMAKE. The correct qrc_filter_ao.cpp file was created. However the IDE still fails to properly generate this file when I build from Qt Creator.
.pro:
include (../../shared.pri)
TARGET = filter_ao
QT += opengl
HEADERS += ./filter_ao.h
SOURCES += ./filter_ao.cpp
RESOURCES += \
filter_ao.qrc
shared.pri
include (./general.pri)
VCGDIR = ../$$VCGDIR
TEMPLATE = lib
CONFIG += plugin
CONFIG += resources
QT += opengl
QT += xml
QT += xmlpatterns
QT += script
win32-msvc2008: LIBS += ../../distrib/common.lib
win32-msvc2008:DEFINES += GLEW_STATIC _USE_MATH_DEFINES
INCLUDEPATH *= ../.. $$VCGDIR ../$$GLEWDIR/include
DEPENDPATH += ../.. $$VCGDIR
win32-msvc2008:DEFINES += _CRT_SECURE_NO_DEPRECATE
CONFIG(release,debug | release){
# Uncomment the following line to disable assert in mingw
#DEFINES += NDEBUG
}
DESTDIR = ../../distrib/plugins
contains(TEMPLATE,lib) {
CONFIG(debug, debug|release) {
unix:TARGET = $$member(TARGET, 0)_debug
else:TARGET = $$member(TARGET, 0)d
}
}
win32-msvc2008: RCC_DIR = $(ConfigurationName)
general.pri
VCGDIR = ../../../vcglib
GLEWDIR = ../external/glew-1.7.0
win32:DEFINES += NOMINMAX
I think #Pie_Jesu is correct. I have set up a directory structure with the files from the OP, and imported the .pro file into QtCreator. I have removed the "win32-msvc2008: " prefix from the line which sets RCC_DIR since I am using VS Express 2010.
Result was that I get a message like "Unable to open \qrc_filter_ao.cpp for writing: Access denied" and no qrc_file is being created.
I then set the line to /TEMP:
RCC_DIR = /TEMP
Result is that I find the qrc_ file below \TEMP.
Did you try with a simpler .pro file? For instance, the following works for me (On Linux, no MSVC env available currently):
TEMPLATE = lib
CONFIG += plugin
CONFIG += resources
QT += opengl
QT += xml
QT += xmlpatterns
QT += script
TARGET = filter_ao
QT += opengl
RESOURCES += \
filter_ao.qrc
Make sure that the output of from the build contain a call to $QTDIR/rcc, like
/usr/bin/rcc -name filter_ao filter_ao.qrc -o qrc_filter_ao.cpp

Subdirectory in a Qt Project

I am looking for a way to organize my project's structure using QMake. I've found the subdirs template but I have quite a hard time understanding it. I have tried to do something like this. Can anyone please just tell me that I am correct or not.
edit: I have read the following thread How to use QMake's subdirs template? but I am still stuck
My project structure is as follows:
MultiFuncTester.pro
- DMM
(DMM.cpp, DMM.h and Multifunctester.pri)
-Safety
(Safety.cpp, Safety.h and Multifunctester.pri)
-Solar
(Solar.cpp, Solar.h and Multifunctester.pri)
Main
(Main.pro, Main.cpp and Multifunctester.pri)
Here Multifunctester.pri file has common stuff for all the sub directories.
I am pasting the MultiFuncTester.pro and .pri file and also the main.pro file
I have made project's pro file as MultiFuncTester.pro :
# build all components recursive
TEMPLATE = subdirs
######## normal build process ########
#
# Make sure your Main.pro is in the last line to ensure correct linking!
#
SUBDIRS = ../../MultiFuncTester/Components/Solar/Build/Solar.pro \
../../MultiFuncTester/Components/DMM/Build/DMM.pro \
../../MultiFuncTester/Components/Safety/Build/Safety.pro \
../../MultiFuncTester/Components/Setup/Build/Setup.pro \
../../MultiFuncTester/Components/Start/Build/Start.pro \
../../MultiFuncTester/Components/Main/Build/Main.pro \
CONFIG += ordered
MultiFunctester.pri file:
######################
# common stuff for all components
######################
TEMPLATE = lib
CONFIG += static \
warn_on \
qt \
thread \
rtti
QT += core \
gui
INCLUDEPATH +=/..\
../../MultiFuncTester/Components \
DEPENDPATH +=/..\
../../MultiFuncTester/Components \
CONFIG += debug_and_release
CONFIG += build_all
QMAKE_CXXFLAGS += -Wall
CONFIG(debug, debug|release) {
CONFIG_SUFFIX = dbg
} else {
CONFIG_SUFFIX = rel
DEFINES += QT_NO_DEBUG \
QT_NO_DEBUG_OUTPUT \
DBT_TRACE_DISCARD \
NDEBUG
CONFIG(gcov) {
QMAKE_CXXFLAGS_RELEASE += -fprofile-arcs -ftest-coverage
QMAKE_LFLAGS_RELEASE += -fprofile-arcs
QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE += -O0
}
}
CONFIG(crosstgt) {
### To be able to build Target run qmake as follows:
#qmake CONFIG+=crosstgt
CONFIG_SUFFIX = $${CONFIG_SUFFIX}_tgt
DEFINES += TARGET_BUILD
}
OBJECTS_DIR = obj_$${CONFIG_SUFFIX}
MOC_DIR = moc_$${CONFIG_SUFFIX}
DESTDIR = lib_$${CONFIG_SUFFIX}
Main.pro file:
################# include pri file #################
!include("Main.pri") {
error("Main.pri not found")
}
####################################################
################# override some pri settings #################
TEMPLATE = app
TARGET = MultiFuncTester
CONFIG -= static
QT += core \
gui
##############################################################
################# list used MultiFuncTester libraries #################
MultiFuncTester_COMPONENTS_DIR =../../MultiFuncTester/Components
################################################################
################# list MultiFunTester libraries #################
MultiFunTester_COMPONENTS_DIR =../../MultiFuncTester/Components
MultiFunTester_COMPONENTS = DMM \
SOLAR\
Safety\
Setup
################# own sources #################
INCLUDEPATH += ../../MultiFuncTester/Components \
SOURCES +=../Source/Main.cpp
################# set destination path
DESTDIR = bin_$$CONFIG_SUFFIX
################# edit include path
INCLUDEPATH += $$MultiFunctester_COMPONENTS_DIR \
################# start group
LIBS += -Wl,--start-group \
################# include MultiFunctester libraries and set dependencies
for(TheComponent, MultiFunctester_COMPONENTS) {
THELIBPATH = $$MultiFunctester_DIR/$${TheComponent}/Build/lib_$$CONFIG_SUFFIX
PRE_TARGETDEPS += $$THELIBPATH/lib$${TheComponent}.a
LIBS += $$THELIBPATH/lib$${TheComponent}.a
}
################# end group
LIBS += -Wl,--end-group
Each subdirectory has a .pro file which have headers and sources defined and also the common multifunctester.pri file
Please let me know that putting a common static library (MultiFunctester.pri file) is a right approach and what shall it do in the code.....and if not please help me in correcting me wherever I am wrong.
Thanks
There are some mistake :
TEMPLATE = lib in MultiFunctester.pri. .pri files are like header files, this will be added in every pro files which include it. It can be confusing, so avoid using the TEMPLATE variable in .pri
if I understand well your first point you have copies of Multifunctester.pri in each directory. This is also really confusing, you should only have one (or different pri files).
Overriding previous setting is not as a good idea as it seems. When you will have to debug to see which configuration is used in each pro file you will feel the pain. Rather declare all common settings in one place, and for variable settings declare them when they
apply.
I don't see Multifunctester.pri included anywhere...
You use the subdirs template when you want each sub-directory to be built separately (they can be interdependent). It is useful for instance when you have a framework with many executable. look at qwt source directory
In this case, you use
TEMPLATE = subdirs
CONFIG += ordered
### They MUST be subdirectories, no ../, and no .pro
SUBDIRS += dir1\
dir2\
...
dirn
for each level aside of the last one (where you have TEMPLATE = lib or app).
Now if you have directory structure but you want to build all together you can create a
pri file for each subdirectory where you include the source files. For example :
in the main src.pro file you have
include(MultiFunctester.pri) ## for the configurations
include($PATHTOSOLAR/Solar.pri)
include($PATHTODMM/dmm.pri)
include($PATHTOSAFETY/safety.pri)
include($PATHTOSETUP/setup.pri)
include($PATHTOSTART/start.pri)
include($PATHTOMAIN/main.pri)
where $PATHTO... is the relative path from thesrc.pro directory.
Now in each subdirectory you include the source files.
onesubdir.pri :
headers += /PATHTOTHISSUBDIR/file1.h ## again same relative path as above
....
source +=....

Resources