Telling qmake to compile dependency lib - qt

I have a main pro file for my application and I would like to tell qmake to also compile a separate lib at the same time as the application. The lib also has a pro file in its directory. Is this possible?

Put the lib and the app in separate subdirectories and make your top-level .pro file to use the SUBDIRS template:
TEMPLATE = subdirs
SUBDIRS = lib app
app.depends = lib

Related

How to install first project after build in qmake subfolder template?

I have a qtcreator project with subfolder template. The folders looks like this:
mainProject/
mainProject.pro
staticLib/
(bunch of .cpp and .h files)
staticLib.pro
testApp/
main.cpp
testApp.pro
And the mainProject.pro file:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = staticLib \
testApp
testApp.depends = staticLib
I would like to install the static library so I added the install option in the staticLib.pro file:
headersDataFiles.path = $$LIBPATH/include
headersDataFiles.files = $$PWD/*.h
INSTALLS += headersDataFiles
libraryFiles.path = $$LIBPATH/lib
Debug:libraryFiles.files = $$OUT_PWD/debug/*.a
Release:libraryFiles.files = $$OUT_PWD/release/*.a
INSTALLS += libraryFiles
The problem is when I build the project in Qtcreator, it builds the static lib and then the application without running make install first, so the build for the app fails. And Qtcreator only lets me add build steps after qmake_all (which build both projects without installing the lib first).
So the question is, is there any way I could build staticLib project, install it and then build testApp?
EDIT: The testApp project can always be linked to the library from the output directory of the first project. But I would like to move the .a and headers files to other folder to keep the project organized. The testApp building failure it's not the problem. Sorry for the misunderstood.
I don't like the idea of adding steps in Qt creator (if it's no the only way, of course) as they are not persistent over different dev environments AFAIK. For example, you will need to add this step in Qt Creator each time you download/clone code from repository on another machine.
The way we've done it on Windows (however, it should work for other platforms also) is via the QMAKE_POST_LINK and $$QMAKE_COPY (pre-define qmake cross-platform copy command):
FILES_TO_COPY = $$DESTDIR/staticLib.a <path to your header1> <path to your header2>
or if you prefer:
FILES_TO_COPY = $$DESTDIR/staticLib.a
FILES_TO_COPY += <path to your header1>
FILES_TO_COPY += <path to your header2>
You can use vars like $$PWD, to not hardcode full path to headers, and then
DESTINATION_DIR = $$shell_quote($$shell_path("/put/it/here/"))
for (FILE, FILES_TO_COPY)
{
FILE_PATH = $$shell_quote($$shell_path($$FILE))
QMAKE_POST_LINK += $$QMAKE_COPY $$FILE_PATH $$DESTINATION_DIR $$escape_expand(\\n)
}
UPD: If you have all needed headers in one directory, you can use QMAKE_COPY_DIR instead of QMAKE_COPY.
Also, if you plan to build it on one specific platform, you could omit the $$shel_quote/$$shell_path and just write the correct paths.
Or if install is what you want, you can just add
QMAKE_POST_LINK=$(MAKE) install
to your .pro file

Pro file directive: copy target to folder at build step

I have a TEMPLATE = subdirs project with 6 subprojects which build in specified order.
I would like to copy the output file of project1 (it's target) to some folder.
This folder is passed with LIBS += -L to project2 and project2 may use this file as a static library.
I found some .pro file commands to copy target files wherever but they are performed at deploy step. I need this to be done at build step. Precisely after project1 got built and before project2 build starts. And would be better if that will be some code that could be kept in .pro file.
Create DestDir.pri in folder, where all of your projects located.
Insert next code:
isEmpty(DESTDIR) {
CONFIG(debug, debug|release) {
DESTDIR=$$PWD/Build/Debug
}
CONFIG(release, debug|release) {
DESTDIR=$$PWD/Build/Release
}
}
Include DestDir.pri to each pro file:
include(../DestDir.pri)
You can change DESTDIR variable to your path or set this variable via qmake command line utils - in both cases this code will locate your artefacts to common folder.
I have found one more way to achieve that:
copydata.commands = $(COPY_FILE) $$OUT_PWD/$(TARGET0) ~/my_libs/
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
It allows to control what library file (.so, .so.1, .so.1.0, .so.1.0.0) is copied and seems to be crossplatform, so I'll left it here too.
But I'll use Milovidov's solution as less complicated in my project.

Could not find .pro file for sub dir - when subdir contains relative path

I've got a top-level .pro file with subdirs template
TEMPLATE = subdirs
SUBDIRS += \
common/tests
common/tests contains .pro file, but qmake couldn't find it:
Could not find .pro file for sub dir "common/tests" in "/home/o.antonyan/Documents/mt3/main/src/common/tests"
But it's there:
ls /home/o.antonyan/Documents/mt3/main/src/common/tests
api interfaces Makefile tests_common.pro.user ui
dataformat.cpp local_enviroment.cpp tests_common.pro types
If I move this pro file to parent directory and modify topp-level qmake SUBDIRS += common then it works. The problem is only when subdirs contain relative path more than 1 level deep.
QMake version 3.0 Using Qt version 5.4.1 on GNU/Linux x86, Qt compiled from sources
From the docs:
It is recommended that the project file in each subdirectory has the same base name as the subdirectory itself, because that makes it possible to omit the file name. For example, if the subdirectory is called myapp, the project file in that directory should be called myapp.pro.
Try renaming tests_common.pro to tests.pro.
I had the same problem and found this solution for the top level .pro file:
TEMPLATE = subdirs
tests_common.file = tests/tests_common.pro
SUBDIRS += tests_common
But now I would prefere the answer of svlasov.

QtCreator tricky project

I have a project in Qt Creator which has several shared library projects and the main project which also contains the main function. What I want is to add a new project which shouldn't be a shared library but just a project with some header files where I keep definitions and error codes. What I wish is to be able to add the path of this project to other projects INCLUDEPATH in order to use those files in other projects.
To do so I created an empty project which .pro file looks like this:
HEADERS += \
myHeader.h
but when I build the whole project it complains that it doesn't find the main in this project with only one header.
Is it possible in QtCreator to achieve this?
Create a .pri file which has your INCLUDEPATH, etc; and then refer to it in your other projects' .pro files:
# Common.pri
INCLUDEPATH += ../myPath
INCLUDE += myHeader.h
# OtherProject.pro
!include( ./Common.pri ) {
error( Could not find the Common.pri file. )
}
INCLUDEPATH += ../myOtherPath
Remember to use the += operator in your .pro files otherwise they will overwrite the .pri file variables.

How do I specifiy an object directory in a Qt project file?

I am building a console Qt project.
Currently, my project file looks like this:
CONFIG += qt console debug
CONFIG -= app_bundle
HEADERS = HelperClass.h
SOURCES = HelperClass.cpp \
main.cpp
TARGET = doit
My QMAKESPEC environment variable is set to: macx-g++
I go to the directory with the project (and source) files and run qmake (which builds a Makefile) then make to build the project. The object files (*.o) from the compilation are placed in the same directory with my source and the executable.
How do I configure this build to place those object files in a sub directory so they don't pollute my source area?
set OBJECTS_DIR:
OBJECTS_DIR = ../myproject/tmp
See http://doc.qtsoftware.com/4.5/qmake-variable-reference.html#objects-dir

Resources