What paths/libs to include when using Qt static in Visual Studio?
-
wrote on 18 May 2021, 21:22 last edited by
Hello, guys :)
I compiled Qt 5 source to use it staticaly in my program. The thing is: I want to code my program in Visual Studio (so i downloaded Qt Visual Studio Tools extension). After, I created a new project in VS of type "Qt Widgets Application". Until now okay, all worked and I dont need to include nothing manually.
Buuuut a different case is: If I want to add a new form MANUALLY in an existing project (let's say I have a Visual Studio empty project)? In fact, I know how to add the form, but when I try to compile, it shows lots of logs for "unresolved external symbol" and these things...
I already tried to make some includes for paths/libs from Qt in my project configuration, but it still showing me errors of this type (unresolved external symbols).
How can I discover the correct paths/libs to include in my Visual Studio project configuration? So i can add Qt forms manually and fix my problem.
Thanks for any help =)
-
It's a bit more complicated than just adding paths. Qt uses code generation tools: moc, qrc and uic for the metaobject system, resources and widget code. Those tools generate code that you need to include in your project, compile and link.
That having said what you need to do is
- Add Qt include paths to your project. This means for every Qt module used, so for example if you have Qt installed in
C:\Qt\
and want to use core, gui and widgets modules you'd includeC:\Q:\include\QtCore
,C:\Q:\include\QtGui
andC:\Q:\include\QtWidgets
- Same goes for libraries. You need to add libraries path like
C:\Qt\lib
and then a library for each module you use, soQt5Core.lib
,Qt5Gui.lib
andQt5Widgets.lib
. These are for Release configuration. For debug those will have ad
postfix, soQt5Cored.lib
and so on. - Every .h file containing a QObject derived class needs to be run through moc, so you'd need to write a custom build step for those files and then include the output file in your project. Similarly for .qrc and .ui files.
Those steps, especially the last one, will quickly become extremely tedious to do manually, so it's a lot (and I mean a lot) easier to use the extension. It does all that work for you. If you have an existing project it's a bit of manual work to convert it to a "Qt aware" one, but it's doable. Open the .vcxproj file of your project in a text editor and fond the
<Keyword>
key in the "Globals" PropertyGroup. Change its value to beQtVS_v304
. After that reopen the project in VS and go to
Extensions->Qt VS Tools->Convert custom build steps to Qt/MSBuild
. After that you can go to project's settings and in theQt Project Settings
set up all you need - Qt version, used modules etc. - Add Qt include paths to your project. This means for every Qt module used, so for example if you have Qt installed in
-
It's a bit more complicated than just adding paths. Qt uses code generation tools: moc, qrc and uic for the metaobject system, resources and widget code. Those tools generate code that you need to include in your project, compile and link.
That having said what you need to do is
- Add Qt include paths to your project. This means for every Qt module used, so for example if you have Qt installed in
C:\Qt\
and want to use core, gui and widgets modules you'd includeC:\Q:\include\QtCore
,C:\Q:\include\QtGui
andC:\Q:\include\QtWidgets
- Same goes for libraries. You need to add libraries path like
C:\Qt\lib
and then a library for each module you use, soQt5Core.lib
,Qt5Gui.lib
andQt5Widgets.lib
. These are for Release configuration. For debug those will have ad
postfix, soQt5Cored.lib
and so on. - Every .h file containing a QObject derived class needs to be run through moc, so you'd need to write a custom build step for those files and then include the output file in your project. Similarly for .qrc and .ui files.
Those steps, especially the last one, will quickly become extremely tedious to do manually, so it's a lot (and I mean a lot) easier to use the extension. It does all that work for you. If you have an existing project it's a bit of manual work to convert it to a "Qt aware" one, but it's doable. Open the .vcxproj file of your project in a text editor and fond the
<Keyword>
key in the "Globals" PropertyGroup. Change its value to beQtVS_v304
. After that reopen the project in VS and go to
Extensions->Qt VS Tools->Convert custom build steps to Qt/MSBuild
. After that you can go to project's settings and in theQt Project Settings
set up all you need - Qt version, used modules etc.wrote on 18 May 2021, 23:08 last edited byI will analyze what i can do based in your reply. Your first hints (about what to include), was basically what I've made, I just "ignored" the third point about:
- Every .h file containing a QObject derived class needs to be run through moc, so you'd need to write a custom build step for those files and then include the output file in your project. Similarly for .qrc and .ui files.
Can you explain me more about it? Or give me a link where I can learn? (Im not a very experienced user)
Those steps, especially the last one, will quickly become extremely tedious to do manually, so it's a lot (and I mean a lot) easier to use the extension. It does all that work for you. If you have an existing project it's a bit of manual work to convert it to a "Qt aware" one, but it's doable. Open the .vcxproj file of your project in a text editor and fond the <Keyword> key in the "Globals" PropertyGroup. Change its value to be QtVS_v304. After that reopen the project in VS and go to
Extensions->Qt VS Tools->Convert custom build steps to Qt/MSBuild. After that you can go to project's settings and in the Qt Project Settings set up all you need - Qt version, used modules etc.
I will make tests with it too.
Thanks for your help :D
- Add Qt include paths to your project. This means for every Qt module used, so for example if you have Qt installed in
-
I will analyze what i can do based in your reply. Your first hints (about what to include), was basically what I've made, I just "ignored" the third point about:
- Every .h file containing a QObject derived class needs to be run through moc, so you'd need to write a custom build step for those files and then include the output file in your project. Similarly for .qrc and .ui files.
Can you explain me more about it? Or give me a link where I can learn? (Im not a very experienced user)
Those steps, especially the last one, will quickly become extremely tedious to do manually, so it's a lot (and I mean a lot) easier to use the extension. It does all that work for you. If you have an existing project it's a bit of manual work to convert it to a "Qt aware" one, but it's doable. Open the .vcxproj file of your project in a text editor and fond the <Keyword> key in the "Globals" PropertyGroup. Change its value to be QtVS_v304. After that reopen the project in VS and go to
Extensions->Qt VS Tools->Convert custom build steps to Qt/MSBuild. After that you can go to project's settings and in the Qt Project Settings set up all you need - Qt version, used modules etc.
I will make tests with it too.
Thanks for your help :D
@Armux said:
Can you explain me more about it?
When you have a class like this:
class Something : public QObject { Q_OBJECT signals: void whatever() const; };
you need to run this header through moc (Meta-Object Compiler). It's a tool located in Qt's installation bin directory. It generates a .cpp file with implementation of the signals and the meta object stuff for the class that the Q_OBJECT macro signifies.
Similarly, for form .ui files you need to run them through uic (UI Compiler) tool, also located in Qt's bin directory. This again generates a C++ file that you need to include in your project. The third Qt tool is rcc (Resource Compiler) used on .qrc files that also generates C++ code files to include.Or give me a link where I can learn?
More info on these tools and how to use them: moc, uic and rcc.
You could run them manually, but it's not practical. What you want to do is run them every time the source files change or you rebuild your solution. In VS projects this can be achieved by specifying custom build steps. You'd define a pre-build step for .ui file to run the uic tool and then mark the output as source file, so that it is treated as part of the project. Qt extension goes a bit further and specifies custom project property pages to automate this process more.
If you want to learn how all this works you can generate a simple Qt project with a wizard and analyze project and property page files it creates, but I'll warn you upfront that it's complicated.
Setting all this up is a pretty laborious process when done manually. If you're new to this I would strongly discourage you from going this path until you get a better hang on Qt's build process. The concept itself is pretty easy - you just run some source files through Qt tools and include them in the build. It's one or two additional commands when building a single file from command line, but setting this up in a maintainable way in a VS project with many files can get hairy fast. It's a lot easier to stick to the extension that takes care of all of this stuff for you and exposes Qt specific project settings with a nice ui within VS.
1/4