Can you use environmental variables in qt creator? - c++

So I use a bunch of libraries in the code I'm currently working in. Right now I include them by doing things like win32:LIBS += "C:/my/location/Tools/libcurl/trunk/lib/Debug/curllib.lib". However, I have an environmental variable that defined %TOOLS% as C:/my/location/Tools/. I tried to simply change my include to win32:LIBS += "%TOOLS%libcurl/trunk/lib/Debug/curllib.lib", but it could not find the files. I looked online and this should be doable. Am I missing something simple, like a way to tell Qt creator to look at window's environmental variables?
Thanks!

To get the content of an environment variable when qmake is processed, you can use the following :
win32:LIBS += $$(TOOLS)/libcurl/trunk/lib/Debug/curllib.lib
TOOLS should be an environment variable set to C:/my/location/Tools.
But you don't necessarily need an environment variable for this. You can simple define a variable in your .pro file :
TOOLS="C:/my/location/Tools"
And use it's value by prefixing it with $$ :
win32:LIBS += $$TOOLS/libcurl/trunk/lib/Debug/curllib.lib

Try the following:
$$VAR => QMake variable's value at the time qmake is run
$${VAR} => QMake variable's value at time qmake is run (subtle difference)
$(VAR) => Contents of an Environment variable at the time Makefile (not qmake) is run
In your Case:
$$(TOOLS) return the Path you need.

Related

Entering scopes in template lib

In a C++/Qt project, we are using .pro-files and qmake to build. Part of the project is a static lib. The .pro file goes like this:
TEMPLATE = lib
CONFIG = staticlib
#general build instructions
linux{
#some os-specific build instructions
}
win32|win64{
#some os-specific build instructions
}
However, these scopes are never entered, not on a linux system (when the first scope should execute), nor on a windows system (for the second scope).
This only appears to be a problem in the staticlib-configuration/lib-template.
The Qt-documentation mentions the different templates, but I do not see anything mentioned as to why scopes would not work or how to solve this.
Those conditions are true if they're set in the CONFIG variable. By overwriting CONFIG with staticlib, you reset all preset flags, including the ones specifying the platform.
It should work if you do CONFIG += staticlib

Qt qmake cross platform development

What is the correct way of defining cross platform behavior in qmake? I'm attempting to merge two projects that use the same codebase but have different parameters in the qmake project file for things like different compiler flags or icon files.
To clarify, if someone pulls the MyProject.pro from version control and attempts to run qmake on it on a mac, I want a couple lines to change when compared to the same operation on a windows machine. Is there any way of adding arguments to $> qmake ... or better, not have to change anything?
Yes, QMake does support conditional statements, in the form of scopes. Basically, you write something like this:
DEFINES = MY_DEF
win32 {
DEFINES += WE_ARE_ON_WINDOWS
debug {
DEFINES += DEBUG_WINDOWS
}
}
As the example shows, scopes can be nested. Operators such as | for OR are also possible.
For a full list of variables and functions, refer to QMake documentation.
You can write something like:
win32:{
# Do something Windows specific
} else {
# Options for other platforms
}
in your .pro file(s).

How to build Qt application using static linking

I am coming for Visual Studio word where you just change the project settings to static or dynamic linking so you application will not need framework dlls to run in on another machine. How do I do that in Qt?
I am following http://doc.qt.io/qt-5/windows-deployment.html but it is that helpful.
It says:
cd C:\path\to\Qt
configure -static <any other options you need>
But c:\Qt is a root folder (default in my case) and configure command is not recognized. I went to c:\Qt\5.3 and still the same case. Also what am I suppose to fill for < if anything? (I am not not filling it and hope it doesn’t mess up)
Do you really have to go command line to do that..is that the only way? I also read few say I need to add this line to .pro file
CONFIG += static
But this doesn’t do anything as well.
How do I link statically?
first of all make sure your Qt version is static .
then add CONFIG += static to your .pro file .

What is the meaning of these commands when you add in a qmake file(.pro)

In short, I want to change the type of application to win32 app, then, I've used the following commands in .pro file
CONFIG -=windows
QMAKE_LFLAGS += $$QMAKE_LFLAGS_WINDOWS
After use the previous commands the application works fine as I want.
But the problem is I don't know what the meaning of each command of these commands.
Can anybody explain each command of these commands?
Let's see CONFIG -= windows.
Excerpt from qmake manual: CONFIG variable
CONFIG
The CONFIG variable specifies project configuration and compiler
options. The values will be recognized internally by qmake and have
special meaning. They are as follows.
[...]
The following options define the application/library type:
windows - The target is a Win32 window application (app only). The
proper include paths, compiler flags and libraries will automatically
be added to the project.
console - The target is a Win32 console application (app only). The
proper include paths, compiler flags and libraries will automatically
be added to the project.
Another excerpt from the manual: Operators
Operators
In many project files, the assignment (=) and append (+=) operators
can be used to include all the information about a project. The
typical pattern of use is to assign a list of values to a variable,
and append more values depending on the result of various tests. Since
qmake defines certain variables using default values, it is sometimes
necessary to use the removal (-=) operator to filter out values that
are not required.
The -= operator removes a value from the list of values in a variable:
DEFINES -= USE_MY_STUFF
The above line removes USE_MY_STUFF from the list of pre-processor
defines to be put in the generated Makefile.
So with CONFIG -= windows you are removing the value windows from the list of values in variable CONFIG. Looks like windows is among the default values of CONFIG on your platform and you need to remove that value. The value windows defines that your target is a Win32 window application. By removing it you declare that you would not like to have a Win32 window app. If your target is a Win32 console application instead then it is recommended to declare it explicitly: CONFIG += console.
And now let's see QMAKE_LFLAGS += $$QMAKE_LFLAGS_WINDOWS.
Excerpt from the manual: QMAKE_LFLAGS
QMAKE_LFLAGS
This variable contains a general set of flags that are passed to the
linker. If you need to change the flags used for a particular platform
or type of project, use one of the specialized variables for that
purpose instead of this variable.
[...]
QMAKE_LFLAGS_WINDOWS
This is used on Windows only.
This variable contains link flags when building Windows GUI projects
(i.e. non-console applications). The value of this variable is
typically handled by qmake or qmake.conf and rarely needs to be
modified.
This means that if you would like to change the flags then you should change either QMAKE_LFLAGS_CONSOLE or QMAKE_LFLAGS_WINDOWS. However you changed QMAKE_LFLAGS directly by adding the value of QMAKE_LFLAGS_WINDOWS which is strange because it contains link flags for building Win32 window apps and you declared with CONFIG -= windows that you would not like to have a Win32 window app.
CONFIG tells qmake that you're building for a specific platform, QMAKE_LFLAGS specifies which platform specific libraries you need.

Retrieve revision number in VS with qmake

My current workflow:
hg update (or whatever one uses to check out a revision)
MyProject.pro → qmake → MyProject.vcproj
Open Visual Studio, edit files
Build project
During the build step, how can I update my config.h header file with information from version control system (e.g. hg id)?
MyProject.vcproj is generated by qmake, so I shouldn't edit it by hand.
You can execute external commands from inside qmake. The easiest way to make the information available in your sources would be to use a define:
HGID = $$system(hg id)
DEFINES += HGID=\\\"$$HGID\\\"
I'm not sure if you can edit an external file from qmake. You could use an external tool, but on Windows you normally don't have things like sed, so it might be a little more problematic.
You can accomplish that using a custom build target and the PRE_TARGETDEPS keyword. Assuming config.h.in has the folowing format:
#define HGID $HGID
You can define a custom build target that will process hgid.h.in and output to hgid.h prior to building your main target as follows:
hgid.target = hgid
hgid.commands = sed s/\\\$$HGID/`hg id`/ hgid.h.in > hgid.h
QMAKE_EXTRA_TARGETS += hgid
PRE_TARGETDEPS += hgid
One opton is to enable the Keyword Extension. Put something like this in your hgrc (or Mercurial.ini if that's your thing):
[extensions]
hgext.keyword=
[keyword]
config.h =
[keywordmaps]
HGREV = {node}
Then in config.h put:
#define HGREV "$HGREV$"
You might need to parse the hex value out of the "$HGREV: deadbeefdeadbeef $" that you'll get, but that's easily done by whatever code is accessing the HGREV define.
In addition to Lukáš Lalinský and goodrone's comment, I'd like to mention that qmake can link directly to the script, not only to it's output. So one can say
DEFINES += REPO_ID=\\\"`./setlocalversion.sh`\\\"
and the script will be freshly executed for every single target.