Qt $$[...] - configuration option that were set when Qt was built - qt

I'm looking at this Qt5 .pro file. It has the following entry:
DESTDIR = $$[QT_INSTALL_PLUGINS]/ms_plugins
I know that this means the compiled module's output (a plugin) should go into the ms_plugins subfolder in the Qt5 plugins install location. I have verified that the plugin does indeed go into that location.
My questions are:
Where is QT_INSTALL_PLUGINS defined?
On a related note, is there a similar variable that holds the
location of the build directory. Typically, something like
build_Desktop_Qt_5_2_1_clang_64bit-Debug on my Mac.
Any documentation of all available variables?
EDIT: I've found some description of these variables here, although, I still don't see where they're defined.
EDIT2: Mostly for future visitors. The documentation mentions The special $$[...] operator can be used to access various configuration options that were set when Qt was built:. So in order to figure out what QT_INSTALL_PLUGINS is we can put the following in a .pro file:
message(Plugins: $$[QT_INSTALL_PLUGINS])

QT_INSTALL_PLUGINS is one of the built-in properties of qmake. The manual of qmake in Qt 4.8 talks about qmake's built-in properties but does not mention QT_INSTALL_PLUGINS specifically. The manual of qmake in Qt 5 shows a much longer list of built-in properties including QT_INSTALL_PLUGINS. If you take a look at the source of qmake you can see that the value of a built-in property is determined by calling QLibraryInfo::location() (source, doc).
The location of the build directory can be found in a variable called OUT_PWD: OUT_PWD specifies the full path leading to the directory where qmake places the generated Makefile.
You can find the documentation of all available variables here.

QT_INSTALL_PLUGINS is a persistent property of qmake. You can print its value on the command line using
qmake -query QT_INSTALL_PLUGINS
To change this location, use qmake -set <property> <value>. Type qmake -help for more information.

Related

How to set qt5 path with cmake find_package on Windows?

I am adding QT like this:
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
...
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick ${OpenCV_LIBS})
but cmake finds some python artifacts instead of expected C:\QT...
How can I change this? Tried to find some examples, but didn't find any clear instruction, which subfolder of C:\QT\ I should specify (any where).
find_package search order is following:
Search in cache variables:
CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH
Search in environment variables: <package>_DIR, CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH.
Search in the HINTS option.
Search the PATH environment variable.
And in some more "desperate" places. More about that here.
With that in mind there are several ways to provide a proper version to QT:
Have an environment variable pointing to the proper version of QT (e.g. QTDIR). And use it in the CMake files:
like set(CMAKE_PREFIX_PATH "$ENV{QTDIR}")
or find_package(Qt5 HINTS "$ENV{QTDIR}" COMPONENTS Core Quick REQUIRED)
Have an environment variable explicitly named Qt5_DIR pointing to the proper version of QT. Then no additional changes to CMake files are required.
Make sure that required version of Qt is the first one to be found in the PATH environment variable, for example, for windows C:\Qt\Qt5.10.1\5.10.1\msvc2017_64
You can set click the Add Entry button in CMake Gui and add a new variable called Qt5_DIR, select its type as PATH and its value to something like C:\Qt\5.11.0\msvc2017_64\lib\cmake\Qt5 where 5.11.0 is the Qt version. This folder must contain Qt5Config.cmake that CMake needs to set things up correctly.
My workaround was to put desired QT to the top of PATH variable. It has to be in PATH if you want CMAKE to find it.

How to add library paths in Qt Creator like LIBPATH in Visual Studio?

Question
a) How do we add library paths where the project should look for depended libraries in Qt Creator?
b) How are the settings in project >> Run >> Build Environment related to similar in .pro file? Does the environment variable listed there applies to .pro file as well (well they don't) so what are they exactly?
Context/Details:
Visual Studio has a various environment variables for folders where a project looks for include files, library files or executable files etc. This is rather confusing in Qt Creator and I havne't seen good documentation on it.
The only thing which is obvious is INCLUDEPATHvariable which points to the directories where to look for the include files (.h)
However how do I set the library paths, the path where it should look for dependent libraries/dlls etc? I can specify the exact library with LIBS variable in .pro file, there don't seem to equivalent of LIBPATH variable where it should look for other libraries if not found in current folder.
I have worked around this be adding library path the following way basically using LIBS variable but dropping the library file name and that seems to work and add the path but I don't see this documented anywhere.
LIBS += -L"$$_PRO_FILE_PWD_/Xerces/bin/"
But what makes things more interesting is the settings in Projects >> select 'Run' from current configuration and expand the Run Environment settings.
''
Here there is LIB variable and LIBPATH variable but there are clearly not .pro environment available. It also says here that these settings are local to user and saved in .pro.user file which perhaps suggest it's a different way to set but it doesn't say how to set them in .pro file but it does suggest to set them there if want to apply for all users!
Likewise there are DEPENDPATH AND VPATH and it is not really clearly what they are used for.
I don't have enough Rep to add comments to your question, So am adding my comment in form of answer. I am pretty new to Qt and have been developing Qt GUI application on Linux.
I set this LD_LIBRARY_PATH environment variable with the path to my Qt libraries. Am not sure how much it will be helpful to you since you are using visual studio on windows.
in Projects property go to Build Environment and add a variable with the libs path, example NAME_LIBPATH. So in the .pro file add the following:
# your lib configuration
LIBS += -L$$(NAME_LIBPATH) \
-llibname

Undefined references - I'm including correct header

I'm trying to subclass from ProjectExplorer::ProjectExplorerPlugin but I'm getting error telling me about undefined references. Any ideas how to fix it?
class MyPluginPlugin : public ProjectExplorer::ProjectExplorerPlugin
{
Q_OBJECT
...
};
error: undefined reference to `imp__ZN15ProjectExplorer21ProjectExplorerPluginC2Ev'
The fact that you don't get a compilation error, but an undefined reference usually means that your project knows where the header files are, but it doesn't know where the library is which contains the already compiled source code.
I've never written a plugin for Qt Creator but I've taken a quick look at its source code structure and I see the following options:
Option A)
There is a projectexplorer.pro file in Qt Creator's source under src/plugins/projectexplorer. You could manually build that project in order to get a ProjectExplorer.lib (plus a .dll or a .a) and then reference this library.Example: Assuming the library would be created in the same directory as its .pro file (I have no idea if it is like that) and you created your plugin withing Qt Creator's source under src/plugins/myplugin, you would define your LIBS variable like this:
LIBS += -L../projectexplorer \
-lProjectExplorer
The first line adds "../projectexplorer" as an additional library directory and the second line adds "ProjectExplorer" as a library to search in any of the defined directories (it automatically adds the OS-specific file extensions like .lib on windows etc).
Obviously if your project or the library is located somewhere else, you need to change the first line accordingly.
Option B)
You could include the source and header files of the projectexplorer directory to your own .pro file using the HEADERS and SOURCES variables. I'm not sure if this wouldn't interfere with any other plugins (including projectexplorer itself) though.
Option C)
There probably is a way to include the projectexplorer.pro file so that you have a master project which first builds the project explorer library and then your own plugin. This would be the safest way to go as it ensures the Project Explorer library is built and up-to-date before your own project is linked against it.
However I have limited experience on this.
If anyone reading this can give a detailed explanation on this option, feel free to edit or provide your own answer.
If you are using Qt Creator built from source coded after April 2013 which includes Commit: #66a3553 - make library and plugin dependencies declarative, then you can simply specify dependencies for your plugin in its .pro file:
# myplugin.pro
QTC_PLUGIN_DEPENDS += \
coreplugin \
projectexplorer

How can the install path be set for a qt project

I'm looking for the equivalent to ./configure --prefix= for qmake. Basically, I want to override the default install/deployment directory. How is this specified with command line qmake? I also use QtCreator to build a lot of my gui projects, and I'd like to know how to do the same thing while building inside of QtCreator. Is there a variable that I can manipulate in the .pro files to do this, or do I change my project settings?
Thanks!
For me, it seems that qmake PREFIX=/usr/local doesn't work
(try with the source of qtcreator)
So the solution is to use qmake normally, but then, you do
make
INSTALL_ROOT=/usr/local make install
I've found the solution to this, and it is just as easy as specifying the --prefix option to configure.
For qmake on the command line, you simpy add a PREFIX= parameter:
qmake PREFIX=/usr/local
There are two ways to do this in QtCreator. First, you could change your .pro file to include an explicit PREFIX variable definition. However, this is not recommended, as the prefix is a preference specific to each user, and it is preferable to keep the distributed project files generic. A better way to do this, is in your own project settings. Simply go to the build configuration that you are using, expand the qmake settings, and add PREFIX= to the additional arguments.

How to add pre processing defs (macros) to qt creator?

In Eclipse there is an option to specify pre processing defines (#ifdef macros) to a project by using the Symbols option in Paths and Symbols. This helps in effective indexing of code which is cross platform. Is there any option to provide these in Qt creator?
It depends:-)
The following is assuming you are using qmake based projects:
First you can add DEFINES += SOME_DEFINE=value into your .pro file. That is picked up inside creator and when building on the command line and should also show up when creating a MSVC or XCode project from the .pro file.
Then you can add DEFINES += SOME_DEFINE=value to the qmake call that Qt Creator will issue when configuring the project. That happens in the Project Mode, Build Settings, QMake Steps.
Finally, you can put #define SOME_DEFINE value liens into a header file and include that. That works for all kinds of projects:-)
From the QT Documentation:
The defines are specified in the .config file. The .config file is a
regular C++ file, prepended to all your source files when they are
parsed. Only use the .config file to add lines as in the example
below:
#define NAME value
That is, if you import a project named MyProject, then the pre-processor definitions should be specified in MyProject.config
For my projects it causes the indexer to recognize this define project wide and changed the auto-complete to reflect this.
I think the initial answers are good, but they require that you manage your configuration manually whereas there're ways to let the IDE manage this for you automatically based on whether you have a release or debug configuration selected.
This bit may be redundant, but please note that this will work for you only if you are using the IDE for building. Obviously, if this is not the case, you will need a different solution.
Steps
Since pictures are worth a thousand words, here's an example of how you define a debug macro for your debug build using Qt Creator 4.3.1:
Make sure you have your Debug configuration selected;
Go to the Projects section on the left menu;
Go to the Build section
Under Build Steps, look for the Additional arguments input box;
Enter your macro definitions (e.g. DEBUG for your #ifdef DEBUGs in the code; in my case it's __CTS_DEBUG__)
The macro will now only be defined when you're using your debug config; when you choose your Release config (see step 1), it will become undefined automatically and your conditionally-compiled debug code will be removed, as shown in the pictures below, which is just what you always wanted.
Results
With the debug config selected
With the release config selected:
I wanted to specify a #define string in the .pro file, but my code ended up with the contents of the string without the quotes. I had to escape the quotes, and then escape the escapes to get one pair of quotes to last all the way to my code. This is because qmake strips off one set of escapes and some quotes, then the command line strips off the rest of them. This worked for me:
DEFINES += "VERSION=\"\\\"0.1.0\\\"\""
On Windows, this VERSION string can then be used in the .rc file to create the version stuff where Windows wants it and as well as in code for an "About" menu.
You can define some PREPROCESSOR in the Project settings in QtCreator. I do not have QtCreator here but i remember there is a tab for project configuration.
First suggestion from #Tobias Hunger's answer worked for me. I was doing this in my .pro file:
DEFINE += KEY=value
which did not work until I changed it to:
DEFINES += KEY=value
That said, if you want to do everything from the command line, an almost hybrid solution that does not use Qt Creator GUI, but does use .pro files, is described here:
https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/
Using the method described in the above link, you can use qtcreator -project to produce a .pro file, then use notepad or vim or some other text editor to add the DEFINES += KEY=value line to the .pro file you just created. Then use qmake thusly: qmake <your_project>.pro, to do the Qt pre-processing, and finally just make to build an executable.
I've also heard lore of a qtcreator -D option to add preprocessor defines from the command line as described here:
https://doc.qt.io/archives/qt-4.8/qmake-variable-reference.html#defines
but I've never tried it.
Hope something works for you! Your dedication to the CLI is admirable.

Resources