PDA

View Full Version : QMAKE custom DEFINES & CONFIG



sheeeng
23rd April 2009, 08:20
Hi,

I'm new to Qt and its qmake functions. I have read through the Qt Assistant but I do not comprehend everything yet.

May I know how can we add custom DEFINES & CONFIG in the *.pro and *.pri files?

E.g.


win32 {
}
macx {
}

If I need a class in Windows only, can I do a *pro file as below? However, it is not working as I expected.


win32:DEFINES += WIN32_MYCLASS //(but not working also in qmake)
win32:CONFIG += win32_myclass //(but not working in *.cpp during #ifdef #endif)

Please advice. Thanks in advance.

Lykurg
23rd April 2009, 08:33
If you want different versions of a file be included use:

win32 {
INCLUDE += Windows/myfile.h
}
macx {
INCLUDE += Mac/myfile.h
}

or for different code in one file use



MyClass::whatEver()
{
#ifdef Q_OS_UNIX
// do fancy unix stuff
#endif
#ifdef Q_OS_WIN32
// do windows stuff here
#endif
}


The right code is chosen by the compiler automatically.

sheeeng
23rd April 2009, 08:43
Thanks, Lykurg for your fast reply! I did understand the part on macx and win32 platform specific codes.

But, what I mean is that how can I separate the C++ classes under single win32 platform?

e.g.
SimpleBuild (contains ClassA only)
AdvancedBuild (contains ClassA, ClassB, and ClassC)

I need to do different *pro file that uses different custom configuration to add files.


win32:DEFINES += WIN32_ADVANCED
win32:CONFIG += win32_Advanced

SUBDIRS += simple_build.pro

Under SUBDIRS there are WIN32_ADVANCED flags to check whether the class is needed or not.
Then, under a controller class, there is a #ifdef win32_Advanced flag to include its header.

Hope this clarify my query.

Lykurg
23rd April 2009, 09:03
Ah, ok. Misread it the first time. Then I would suggest in your pro file:

contains(DEFINES, WIN32_ADVANCED) {
message(Building the advanced version...)
}

with

$qmake mypro.pro
build normal mode, and

$qmake "DEFINES += WIN32_ADVANCED" mypro.pro
building the advanced.

Hope you mean that:)

sheeeng
23rd April 2009, 09:28
Ah, ok. Misread it the first time. Then I would suggest in your pro file:

contains(DEFINES, WIN32_ADVANCED) {
message(Building the advanced version...)
}

with

$qmake mypro.pro
build normal mode, and

$qmake "DEFINES += WIN32_ADVANCED" mypro.pro
building the advanced.

Hope you mean that:)

Before the contains, how do we add the WIN32_ADVANCED flag?

How do we use that '$qmake" in *.pro file? I'm building using MSVC and opens the *.pro file using Qt-VSIntegration.

Thanks!

Lykurg
23rd April 2009, 09:33
Before the contains, how do we add the WIN32_ADVANCED flag?

How do we use that '$qmake" in *.pro file? I'm building using MSVC and opens the *.pro file using Qt-VSIntegration.

Thanks!

was a little typo: should be
$ qmake mypro.pro and should be indicate - as I don't have thought someone really is using Windows :cool: - that that is to type in the console when you building the app. How To integrate it in MSVC I don't know. To be explicate: You set the WIN32_ADVANCED flag via the console when you building.

sheeeng
23rd April 2009, 09:37
Thanks, Lykurg. However, I did not able to add the custom DEFINES and custom CONFIG succesfully.

Lykurg
23rd April 2009, 09:46
Ok, here is a console output which I can't better clarify:

lykurg@XXX:~/Dokumente$ cat test.pro
# normal *.pro file here
message(normal stuff)

contains(DEFINES, WIN32_ADVANCED) {
message(WIN32_ADVANCED stuff here)
}

lykurg@XXX:~/Dokumente$ qmake test.pro
Project MESSAGE: normal stuff

lykurg@XXX:~/Dokumente$ qmake "DEFINES += WIN32_ADVANCED" test.pro
Project MESSAGE: normal stuff
Project MESSAGE: WIN32_ADVANCED stuff here

lykurg@XXX:~/Dokumente$

sheeeng
23rd April 2009, 09:57
Thanks Lykurg. I guess I have to convert output to Qt-Windows and then open using Qt-VsIntegration.

sheeeng
23rd April 2009, 10:46
I think I should rephrase my question...

How to carry forward "DEFINES += WIN32_ADVANCED" to all *pro under SUBDIRS?

Lykurg
23rd April 2009, 11:17
global.pro

TEMPLATE = subdirs
SUBDIRS = testsub

qmake "DEFINES += WIN32_ADVANCED" global.pro
results in Makefile

//..
testsub/$(MAKEFILE):
@$(CHK_DIR_EXISTS) testsub/ || $(MKDIR) testsub/
cd testsub/ && $(QMAKE) testsub.pro -unix DEFINES\ +=\ WIN32_ADVANCED -o $(MAKEFILE)
sub-testsub-qmake_all: FORCE
@$(CHK_DIR_EXISTS) testsub/ || $(MKDIR) testsub/
cd testsub/ && $(QMAKE) testsub.pro -unix DEFINES\ +=\ WIN32_ADVANCED -o $(MAKEFILE)
//..

just fine;)

sheeeng
23rd April 2009, 14:57
Lykurg, thanks a lot for your help on this issue. Appreciated your effort by showing me the solution the UNIX way.
However, I'm having a hard time to make it work on Qt Visual Studio Integration tool.


win32:DEFINES += WIN32_ADVANCED
win32:CONFIG += win32_Advanced

SUBDIRS += simple_build.pro //WIN32_ADVANCED and win32_Advanced should be also available inside simple_build.pro

I do not want to open all the *.pro files and add the WIN32_ADVANCED and win32_Advanced manually.

Hope someone know how to do it on Windows.

Lykurg
23rd April 2009, 15:50
Qt is platform independent so all I have said is also usable under Windows, but if you can't transfer I do It for you:

c:\global.pro

TEMPLATE = subdirs
SUBDIRS = testsub // and all the other subdirs you have


c:\>qmake "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" global.pro
results in:

################################################## ###########################
# Makefile for building: global
# Generated by qmake (2.01a) (Qt 4.4.3) on: Do 23. Apr 15:45:05 2009
# Project: ..\global.pro
# Template: subdirs
# Command: c:\Qt\4.4.3\bin\qmake.exe -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o ..\Makefile ..\global.pro
################################################## ###########################

first: make_default
MAKEFILE = Makefile
QMAKE = c:\Qt\4.4.3\bin\qmake.exe
DEL_FILE = del
CHK_DIR_EXISTS= if not exist
MKDIR = mkdir
COPY = copy /y
COPY_FILE = $(COPY)
COPY_DIR = xcopy /s /q /y /i
INSTALL_FILE = $(COPY_FILE)
INSTALL_PROGRAM = $(COPY_FILE)
INSTALL_DIR = $(COPY_DIR)
DEL_FILE = del
SYMLINK =
DEL_DIR = rmdir
MOVE = move
CHK_DIR_EXISTS= if not exist
MKDIR = mkdir
SUBTARGETS = \
sub-testsub

testsub\$(MAKEFILE):
@$(CHK_DIR_EXISTS) testsub\ $(MKDIR) testsub\
cd testsub\ && $(QMAKE) testsub.pro -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o $(MAKEFILE)
sub-testsub-qmake_all: FORCE
@$(CHK_DIR_EXISTS) testsub\ $(MKDIR) testsub\
cd testsub\ && $(QMAKE) testsub.pro -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o $(MAKEFILE)
sub-testsub: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE)
sub-testsub-make_default: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE)
sub-testsub-make_first: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) first
sub-testsub-all: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) all
sub-testsub-clean: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) clean
sub-testsub-distclean: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) distclean
sub-testsub-install_subtargets: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) install
sub-testsub-uninstall_subtargets: testsub\$(MAKEFILE) FORCE
cd testsub\ && $(MAKE) -f $(MAKEFILE) uninstall

..\Makefile: ../global.pro ../Qt/4.4.3/mkspecs/win32-g++/qmake.conf ../Qt/4.4.3/mkspecs/qconfig.pri \
../Qt/4.4.3/mkspecs/features/qt_functions.prf \
../Qt/4.4.3/mkspecs/features/qt_config.prf \
../Qt/4.4.3/mkspecs/features/exclusive_builds.prf \
../Qt/4.4.3/mkspecs/features/default_pre.prf \
../Qt/4.4.3/mkspecs/features/win32/default_pre.prf \
../Qt/4.4.3/mkspecs/features/debug.prf \
../Qt/4.4.3/mkspecs/features/debug_and_release.prf \
../Qt/4.4.3/mkspecs/features/default_post.prf \
../Qt/4.4.3/mkspecs/features/win32/rtti.prf \
../Qt/4.4.3/mkspecs/features/win32/exceptions.prf \
../Qt/4.4.3/mkspecs/features/win32/stl.prf \
../Qt/4.4.3/mkspecs/features/shared.prf \
../Qt/4.4.3/mkspecs/features/warn_on.prf \
../Qt/4.4.3/mkspecs/features/qt.prf \
../Qt/4.4.3/mkspecs/features/win32/thread.prf \
../Qt/4.4.3/mkspecs/features/moc.prf \
../Qt/4.4.3/mkspecs/features/win32/windows.prf \
../Qt/4.4.3/mkspecs/features/resources.prf \
../Qt/4.4.3/mkspecs/features/uic.prf \
../Qt/4.4.3/mkspecs/features/yacc.prf \
../Qt/4.4.3/mkspecs/features/lex.prf
$(QMAKE) -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o ..\Makefile ..\global.pro
..\Qt\4.4.3\mkspecs\qconfig.pri:
..\Qt\4.4.3\mkspecs\features\qt_functions.prf:
..\Qt\4.4.3\mkspecs\features\qt_config.prf:
..\Qt\4.4.3\mkspecs\features\exclusive_builds.prf:
..\Qt\4.4.3\mkspecs\features\default_pre.prf:
..\Qt\4.4.3\mkspecs\features\win32\default_pre.prf :
..\Qt\4.4.3\mkspecs\features\debug.prf:
..\Qt\4.4.3\mkspecs\features\debug_and_release.prf :
..\Qt\4.4.3\mkspecs\features\default_post.prf:
..\Qt\4.4.3\mkspecs\features\win32\rtti.prf:
..\Qt\4.4.3\mkspecs\features\win32\exceptions.prf:
..\Qt\4.4.3\mkspecs\features\win32\stl.prf:
..\Qt\4.4.3\mkspecs\features\shared.prf:
..\Qt\4.4.3\mkspecs\features\warn_on.prf:
..\Qt\4.4.3\mkspecs\features\qt.prf:
..\Qt\4.4.3\mkspecs\features\win32\thread.prf:
..\Qt\4.4.3\mkspecs\features\moc.prf:
..\Qt\4.4.3\mkspecs\features\win32\windows.prf:
..\Qt\4.4.3\mkspecs\features\resources.prf:
..\Qt\4.4.3\mkspecs\features\uic.prf:
..\Qt\4.4.3\mkspecs\features\yacc.prf:
..\Qt\4.4.3\mkspecs\features\lex.prf:
qmake: qmake_all FORCE
@$(QMAKE) -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o ..\Makefile ..\global.pro

qmake_all: sub-testsub-qmake_all FORCE

make_default: sub-testsub-make_default FORCE
make_first: sub-testsub-make_first FORCE
all: sub-testsub-all FORCE
clean: sub-testsub-clean FORCE
distclean: sub-testsub-distclean FORCE
-$(DEL_FILE) ..\Makefile
install_subtargets: sub-testsub-install_subtargets FORCE
uninstall_subtargets: sub-testsub-uninstall_subtargets FORCE

sub-testsub-sub_Debug: testsub\\$(MAKEFILE)
cd testsub\ && $(MAKE) debug
debug: sub-testsub-sub_Debug

sub-testsub-sub_Release: testsub\\$(MAKEFILE)
cd testsub\ && $(MAKE) release
release: sub-testsub-sub_Release

mocclean: compiler_moc_header_clean compiler_moc_source_clean

mocables: compiler_moc_header_make_all compiler_moc_source_make_all
install: install_subtargets FORCE

uninstall: uninstall_subtargets FORCE

FORCE:



Look:

testsub\$(MAKEFILE):
@$(CHK_DIR_EXISTS) testsub\ $(MKDIR) testsub\
cd testsub\ && $(QMAKE) testsub.pro -win32 "DEFINES += WIN32_ADVANCED" "CONFIG += win32_Advanced" -o $(MAKEFILE)


c:\>make

be happy...


...or I don't get your problem.

sheeeng
23rd April 2009, 16:01
Thanks, Lykurg! Felt so bad to trouble you on this simple task.
I think the problem is not with us. It is with the Qt Visual Studio Integration. I just open *pro using Visual Studio by Qt | Open Qt *pro file and it will generate the Visual Studio *sln and *proj files for us.
I did not use any qmake or make commands when building Qt. It was taken care of by the integration tool.
I do feel that this should NOT be a big issue.
Anyway, thanks a lot! Really appreciate your pointers!

monst
12th January 2012, 14:47
I faced the same problem. I would like to forward a definition into each subdir mentioned in my .pro file:



DEFINES += SOME_VERY_COMMON_MACRO
TEMPLATE = subdirs
SUBDIRS = testsub


But unfortunately it doesn't work! SOME_VERY_COMMON_MACRO is not forwarded to testsub Makefile.

Lykurg's solution is enough, but it's less convenient: it makes me write build instructions for other project developers. And it makes them read it :). I think there must be simpler solution.

Lykurg
12th January 2012, 16:11
I haven't time to check it right now myself, but as an other possibility you can create a *.pri file and include it once in each other *.pro file. This is a one time work and then you only have to alter the *.pri file. Thus nobody has to read your build instructions.

monst
12th January 2012, 17:04
Yes, that will do. But in my case one of sub directories is something like a third party project, and I don't want to modify its .pro files.
All I want is to provide a macro definition to a compiler when I build a whole project. Maybe I want a strange thing :).