how to define a config in pro file ?
by default, we have two config, debug and release. I want to add 2 other config but not in pro.user ! in pro file.
Your question is a bit unclear. It sounds like maybe you're currently building with "debug" and "release", from the command line, and you want to add your own build variants similar to that.
If that's the case... the mechanism for this is addExclusiveBuilds. Here is an example. I wouldn't recommend to mess around with it if you aren't comfortable reading qmake code.
TEMPLATE = app
SOURCES = main.cpp
# Adds two build variants.
# One of them builds the app with optimal compiler flags,
# the other one builds the app with support for collecting coverage data.
# For the first one, CONFIG will contain `optimized' and a Makefile.Optimized will be generated.
# For the second, CONFIG will contain `coverage' and a Makefile.Coverage will be generated.
# There will also be a top-level Makefile which invokes both the sub-makefiles.
addExclusiveBuilds(optimized, Optimized, coverage, Coverage)
CONFIG(optimized, coverage|optimized) {
message(I am in the optimized build variant)
QMAKE_CXXFLAGS += -O3
TARGET = myapp-optimized
}
else:CONFIG(coverage, coverage|optimized) {
message(I am in the coverage build variant)
QMAKE_CXXFLAGS += --coverage
QMAKE_LFLAGS += --coverage
TARGET = myapp-coverage
}
else {
message(I am in the glue project which contains the build variants)
# This will cause a `make' to build both optimized and coverage
# variants by default.
CONFIG += build_all
}
If I understand what you are saying, you add what you want to the CONFIG variable:
CONFIG += user_setting
...
user_setting: message( "compiling with user_setting" )
See the qmake manual where it talks about the CONFIG variable, especially near the end of the section.
Related
I have a .pro file which looks like:
SOURCES += myfolder/source1.cpp \
myfolder/source2.cpp
HEADERS += myfolder/header1.h\
myfolder/header2.h
FORMS += myfolder/form1.ui\
myfolder/form2.ui
And everything works great. However, if I try to use an asterisk to include all the files, i.e.:
SOURCES += myfolder/*.cpp
HEADERS += myfolder/*.h
FORMS += myfolder/*.ui
qmake throws a file-not-found-error:
WARNING: Failure to find: myfolder\*.cpp
[...]
:-1: error: No rule to make target `myfolder/*.cpp', needed by `release/source1.o'. Stop.
In both cases, Qt-Creator can find the files.
Is there a way to use the asterisk? It's annoying to type the files manually.
Thank you!
[EDIT: Qt 4.8.4, Windows 7, Qt-Creator 2.6.1. Sry for forgetting this thought it isnt needed.]
[EDIT: Found solution: http://qt-project.org/forums/viewthread/1127 . Thank you anyway!]
In qmake 3.0, at least, it's possible to use something like:
SOURCES = $$files(*.cpp, true)
HEADERS = $$files(*.h, true)
The true argument will cause the files function to recursively find all files matching the pattern given by the first argument.
At first, using asterisk is bad practice - despite that qmake allows it, QtCreator cannot edit such *.pro correctly on adding new, renaming or deleting file. So try to add new files with "New file" or "Add existing files" dialogs.
QMake has for loop and function $$files(directory_path: String). Also append files to SOURCES or HEADERS variable respectively.
Brief example, which adds all files, but not directories, to variable FILES (not affect build or project tree):
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):FILES += $$file
If you want to check if file is *.cpp, try to use contains($$file, ".cpp").
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):contains($$file, ".cpp"):SOURCES += $$file
I am using QtCreator 3.1.1 to build a cross-platform project, and so I arranged to have different compilation kits for targeting my desktop PC and my BeagleBoneBlack (BBB).
Now I would like to define some macro in qmake project file (.pro) which are specific only for a given kit.
In other words I would like do in my .pro file something like:
if(kit == BBB)
DEFINES += MY_BBB_MACRO
elseif(kit == Desktop)
DEFINES += MY_DESKTOP_MACRO
else
DEFINES += OTHER_MACRO
Is is possible? How can I do that?
I obtained some help on Qt forum (take a look here) about this problem...
Anyway the solution consists in using qmake built-in test functions.
Basically I've added some CONFIG directive in QtCreator's project management: in the following screenshot you can see for example you can see that I've added CONFIG+=BBB in the project configuration for BBB kit; in the same way I've added CONFIG+=AM335x and CONFIG+=Desktop to AM335x and Desktop kits, respectively...
Then, in my .pro file I've added something like:
and now in my source code I can use something like #ifdef PLATFORM_BBB, #ifdef PLATFORM_AM335X and #ifdef PLATFORM_DESKTOP for differentiating the program behavior depending on compilation kit.
I found another solution.
First, add additional arguments in Projects using CONFIG+=Variable name for kit.
And in .pro file, write some code like below.
Desktop {
message("Desktop is selected")
}
RPI {
message("rpi is selected")
target.path = /home/pi
INSTALLS += target
}
If you look at the general message tab, you can see that the setting works well.
I have a .pro file which looks like:
SOURCES += myfolder/source1.cpp \
myfolder/source2.cpp
HEADERS += myfolder/header1.h\
myfolder/header2.h
FORMS += myfolder/form1.ui\
myfolder/form2.ui
And everything works great. However, if I try to use an asterisk to include all the files, i.e.:
SOURCES += myfolder/*.cpp
HEADERS += myfolder/*.h
FORMS += myfolder/*.ui
qmake throws a file-not-found-error:
WARNING: Failure to find: myfolder\*.cpp
[...]
:-1: error: No rule to make target `myfolder/*.cpp', needed by `release/source1.o'. Stop.
In both cases, Qt-Creator can find the files.
Is there a way to use the asterisk? It's annoying to type the files manually.
Thank you!
[EDIT: Qt 4.8.4, Windows 7, Qt-Creator 2.6.1. Sry for forgetting this thought it isnt needed.]
[EDIT: Found solution: http://qt-project.org/forums/viewthread/1127 . Thank you anyway!]
In qmake 3.0, at least, it's possible to use something like:
SOURCES = $$files(*.cpp, true)
HEADERS = $$files(*.h, true)
The true argument will cause the files function to recursively find all files matching the pattern given by the first argument.
At first, using asterisk is bad practice - despite that qmake allows it, QtCreator cannot edit such *.pro correctly on adding new, renaming or deleting file. So try to add new files with "New file" or "Add existing files" dialogs.
QMake has for loop and function $$files(directory_path: String). Also append files to SOURCES or HEADERS variable respectively.
Brief example, which adds all files, but not directories, to variable FILES (not affect build or project tree):
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):FILES += $$file
If you want to check if file is *.cpp, try to use contains($$file, ".cpp").
files = $$files($$PWD/src)
win32:files ~= s|\\\\|/|g
for(file, files):!exists($$file/*):contains($$file, ".cpp"):SOURCES += $$file
My project has top-level directory proj with subdirectories runtime and test. Basically, test depends on runtime, but it's a little more complicated.
Expected behavior: If you modify a file in runtime, then make runtime, then make test, this should rebuild test.
Actual behavior: For test, you get "make: Nothing to be done for `first'."
Here are the relevant excerpts from the project files.
proj.pro:
test.depends = runtime
runtime.pro:
TEMPLATE = lib
CONFIG = no_link target_predeps staticlib
TARGET =
# Avoid building libruntime.a
QMAKE_AR_CMD = #true
QMAKE_RANLIB = #true
include(../proj.pri)
RUNTIME_SOURCES += \
foo.c
bar.c
proj.pri:
CLANG_RUNTIME_FLAGS = -emit-llvm
runtime.input = RUNTIME_SOURCES
runtime.output = lib${QMAKE_FILE_IN_BASE}.bc
runtime.commands = $$CLANG $$CLANG_RUNTIME_FLAGS -c ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
QMAKE_EXTRA_COMPILERS += runtime
In runtime's Makefile, there is a rule for target compiler_runtime_make_all that seems to correspond to the QMAKE_EXTRA_COMPILERS. The files built by this rule (foo.bc and bar.bc) are in the OBJECTS list, so they get built when you make this Makefile.
In test's Makefile, there is also a rule for target compiler_runtime_make_all, but it has no recipe and it's not referred to anywhere.
So how do I tell test that it should depend on the QMAKE_EXTRA_COMPILERS for runtime?
In the top-level proj.pro (which I assume is TEMPLATE = subdirs), specifying that test depends on runtime only affects the order in which the top-level make is run on the specified SUBDIRS. It does not introduce any additional dependencies in any of the subdirectories — those are all totally independent invocations of qmake and make.
So, to solve this, you'll need to have test.pro indicate the specific runtime files it depends on. See the POST_TARGETDEPS variable.
Or if you're using QMAKE_EXTRA_COMPILERS to build the sources in test.pro, you could add something like this:
test.depends = $${ROOT}/runtime/lib*.bc
In our project, we added some source and header files if a MACRO is defined. We do this like that, in the .pro file:
contains(DEFINES, MY_DEF) {
message("Support MY_DEF")
INCLUDEPATH += \
my_include_dir
SOURCES += \
source1.cpp \
source2.cpp
HEADERS += \
my_include_dir/header1.h \
my_include_dir/header2.h
FORMS += \
myform.ui
}
This works fine during the build. The files are not compiled if MY_DEF is not defined. MY_DEF is defined like that:
DEFINES += MY_DEF
Curiously, Qt Creator always display the files in the project tree, whereas MY_DEF is defined or not. If not defined, they are not used for the build, but they still are displayed and editable, searches can scan them, etc... Is it a bug of Qt Creator?
This is not a big issue, just a little annoying, because we don't know clearly if a file is part of the project or not.
It's intentional even. There's a special "accumulating" parsing mode to collect all files that are mentioned in .pro files (essentially the same that's used to collect "translatable strings") for display in the project tree. Otherwise things like "Replace in all files in a project" would yield different results depending on the platform or the context it is run in. [And it's not half of qmake that's included, but close to all of it...]
This seems to be an issue with QtCreator and how it reads the .pro files - it doesn't seem to actually fully parse the files, opting instead to just pick out certain bits. I've got the same issue with files that are only included on one platform or another - in QtCreator, they always show up.
I expect that the reason is either that they don't want to re-implement half of qmake just to get the file lists, OR that there are situations where trying to parse it 'correctly' would get the wrong answer, and they've chosen to be predictably wrong instead of randomly wrong.
In addition to the conditional includes in QMake, I add #ifdef around such conditional source code. That way I also see it visually drop out of compilation when the conditions are not met. It's not as good as having the files drop out entirely from the project tree, but it's better than allowing them to still appear like they are part of the build when editing them if they are not applicable.
Just for the sake of completeness and answer correctness. Probably someone else needs this example of root .pro file with conditional source tree:
TEMPLATE = subdirs
SUBDIRS = device
CONFIG -= debug_and_release
_SANDBOX_DIR = $$dirname(PWD)
_PLAYER_PRO = $${_SANDBOX_DIR}/player/player.pro
SUBDIRS = device
device.subdir = $${_SANDBOX_DIR}/proxy/libproxy
contains(QMAKE_PLATFORM, android) {
unset(_PLAYER_PRO)
} else {
SUBDIRS += player
player.file = $${_PLAYER_PRO}
player.depends = device
}
SUBDIRS += app
app.subdir = $${_SANDBOX_DIR}/display/display
app.depends = device
contains(SUBDIRS, player) {
app.depends += player
}