Compiling a simple Qt "Hello World!" application within Visual Studio 2010 Express? - visual-studio-2010

I'm trying to build a basic Qt "Hello, world!" application inside Visual Studio.
I got the moc step to work (I think), but now I am at a loss as to how to fix this linker error:
1>moc_mainwindow.obj : error LNK2001: unresolved external symbol "public:
static struct QMetaObject const QMainWindow::staticMetaObject"
(?staticMetaObject#QMainWindow##2UQMetaObject##B)
I've done a lot of searching but I am at a loss.
Here are my include directories:
i:\Qt\4.6.3\include\QtCore;
i:\Qt\4.6.3\include\QtGui;
i:\Qt\4.6.3\include;
i:\Qt\4.6.3\include\ActiveQt;
reease;
.;
i:\Qt\4.6.3\mkspecs\win32-msvc2008
Here are the libraries I am linking against:
i:\Qt\4.6.3\lib\QtGui4.lib;
i:\Qt\4.6.3\lib\QtCore4.lib;
gdi32.lib;
comdlg32.lib;
oleaut32.lib;
imm32.lib;
winmm.lib;
winspool.lib;
ws2_32.lib;
ole32.lib;
user32.lib;
advapi32.lib;
libpng.lib;
msimg32.lib;
shell32.lib;
kernel32.lib;
uuid.lib;
Does anyone have any ideas?

qmake will generate the moc voodoo from the header file in .pro file. As you aren't using qmake, by the sound of it, but a native visual studio project, this is probably the cause of the problem.
If you use qmake to generate your visual studio project all your problems will go away and life will be sweet. Probably!
I am using the open 2010.05; obviously you want to substitute the correct path for your version.
set up the environment
start 2010 command environment from the start menu
-set include=%include%;C:\Qt\2010.05\qt\include
-set lib=%lib%;C:\Qt\2010.05\qt\lib
-set path=%path%;C:\Qt\2010.05\qt\bin
-set QMAKESPEC=win32-msvc2010
write code, create files etc
generate the initial pro and makefile and fire up VS
-qmake -tp vc
-qmake
you should now have a makefile - check that it works by running:
-nmake
now launch visual studio
-VCExpress.exe /useenv
-XXX.vcxproj can now be opened
If this doesn't work you may need to build qt at against visual studio. This is very straightforward - go to the qt directory (from within the visual studio express command window) and type:
configure.exe -platform win32-msvc2010 -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-multimedia -no-qt3support -fast

You cannot install the Qt VS plugin on the Express edition of VC++. Assuming you got the moc to compile, you also need to make sure you're including the appropriate libraries (*.lib files) at link time. This goes under Project properties > Linker > Input > Additional Dependencies.
You will need qtcore4.lib at a minimum.
Also make sure the Qt library path is in your library search path. On my computer it's c:\qt\4.6.2\lib.

I was able to get QT to work with Visual C++ Express 2010 using http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/ and http://portfolio.delinkx.com/files/Qt.pdf as guides. Just in case anyone still is having problems.

Have you create the visual studio project using qmake first? The problem seems to be the moc compilation. Do you have qt plug-in installed and the qt path in enviromental variables? Can you add you hello world code so I can have a look at it?

You need to add commands to generate QT metaclasses, then also include the generated files in your project as c++ code.
Generating the QT metaclasses:
First, add your QT bin path into the Executable Directory. (This is in Configuration Properties > VC++ Directories)
Add your Header files that contain Q_OBJECT macros to the project.
Multi-select your header files, then right click on a header file, click Properties.
Change "Item Type" from "C/C++ Header" to "Custom Build Tool".
Set Command line to this: moc.exe "%(FullPath)" > "$(ProjectDir)MetaObjects\moc_%(Filename).cpp"
Set Description to this: QT: Generate $(ProjectDir)MetaObjects\moc_%(Filename).cpp (optional)
Set Outputs to this: $(ProjectDir)MetaObjects\moc_%(Filename).cpp
Run Build just to make it generate the metaobject code
Add the generated C++ files from the Project Directory Metaobjects folder into your project

Related

Visual Studio 2017 - How to create a project from the source using CMake?

I have a pretty large software library using CMake to be compiled. We use Linux mostly, but now a new colleague wants to use Visual Studio.
Is there any way to create a new VS 2017 project from the existing source codes with CMake structure?
I know, it's possible to do it with CLion, but I have no idea about VS, as I have a very little experience with it.
Other questions seem to focus on creating an empty project, which will use CMake, but not on creating a project from already existing source files.
Creating a cmake project with visual studio
Creating a project with visual studio 2017
I'm not sure why you asked for details but...
Assuming you are using cmake 3.13 then you can do the following in a command shell:
cmake -G "Visual Studio 15 2017" -S path_to_source -B path_to_build
This will then create a solution file. Actually it creates a solution file for every project() command that is issued in CMakeLists.txt.
You can then open the solution file in Visual Studio, and build the project as usual.
You don't even need to do this in the Visual Studio GUI. After creating the initial project you can also issue the command:
cmake --build path_to_build
Which will kick off the build at the command line.
Now if your CMakeLists.txt in path_to_source is using Linux specific libraries or gcc specific compiler settings then the CMakeLists.txt will have to get updated to the Windows equivalent.
The alternative is to start Visual Studio and then use File->Open->CMake and open the CMakeLists.txt file in path_to_source. It'll then start to generate the project. But I prefer using the command line method.

Generate Visual Studio Project File to build a library with qmake syntax and where to find the project file?

I have an old script to create Visual Studio Project Files to build libraries. The syntax is
cd Kernel
cd core
qmake -t vclib
cd ccMigration
qmake -t vclib
cd ..
and so on for all of the different directories.
I understand why we use qmake and vclib, but what I am not sure is what is the use of -t in this line:
qmake -t vclib
Looking at this : http://doc.qt.io/archives/qt-5.5/qmake-platform-notes.html it says:
Creating Visual Studio Project Files This section describes how to
import an existing qmake project into Visual Studio. qmake is able to
take a project file and create a Visual Studio project that contains
all the necessary information required by the development environment.
This is achieved by setting the qmake project template to either vcapp
(for application projects) or vclib (for library projects).
This can also be set using a command line option, for example:
qmake -tp vc
Why do they use -tp and is the old script syntax correct by using -t?
After I run the script it takes about 1/2 seconds and where can I find the project files for Visual Studio?
Finally, When I open the project files how can I build a library using Visual Studio? Can someone point me to a place where I can read some material?
Thank you for all the help!
Edit: I ran the script and it created .vcproj files in the directories that qmake was run. Example: In folder Kernel -> core it created ccCore.vcproj project file. When I open the file in Visual Studio 2017 it wanted me to perform a One-way upgrade that will automatically make functional changes to the project in order to open it. Is this okay?

LNK1104 error with example CMake project in Visual Studio 2017

I decided to give the built-in CMake support in VS2017 a try, and instead of figuring out why our multi-library, multi-executable project has problems with find_package in the hand-crafted CMakeLists.txt files that it has used for years, I decided to try a simple project first, the FooBar example from the CMake wiki: https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
This project creates a shared library foo and an executable bar that links to this library, and is about as simple as they come. I unzipped the project, opened it with File->Open->Folder, the CMake configuration starts and succeeds. Fist thing I notice is that there's no more Build menu, no F7 shortcut to build my project, but instead, I have to select CMake->Build CMakeLists.txt :-( The Debug menu is similarly stunted, and quick debugging is why I use Visual Studio in the first place. This is already looking bad, but it gets worse:
The build fails with this output:
foo.vcxproj -> C:\Users\Enno\AppData\Local\CMakeBuild\639e9ecd-8891-eb38-b26b-ce84aa448eea\build\x86-Debug\foo\Debug\foo.dll
C:\Users\Enno\AppData\Local\CMakeBuild\639e9ecd-8891-eb38-b26b-ce84aa448eea\build\x86-Debug\bar\LINK : fatal error LNK1104: cannot open file '..\foo\Debug\foo.lib'
Thoughts:
The build directory is in %APPDATA%? That's going to be annoying.
There is indeed no .lib file in that location, just the .dll.
At this point I was becoming skeptical that this may not be a problem with VS2017, but maybe with the sample project itself, or with CMake. So I created a solution for VS2015 with cmake.exe -G "Visual Studio 14" ., which I opened in VS2015 and Voila! I got the same error message.
Is there a CMake genius on SO that can tell me what is wrong with this project?
Turns out: The example on the CMake wiki is not portable in the first place, so this has nothing to do with Visual Studio's built-in CMake support. It does not take into account that Windows needs export libraries for DLLs. Adding the correct __declspec(dllexport) incantations to foo.h resolves the error message.
I found all the information that I needed about shared libraries on Windows at this link:
http://gernotklingler.com/blog/creating-using-shared-libraries-different-compilers-different-operating-systems/

Using boost 1_47 with Visual C++ Express 2010

I have installed and built (successfully as far as I can tell), boost 1_47_0. I am now trying to get the sample program (the regex one) with their install guide to run and it is giving me the following link error:
LNK2019: unresolved external symbol "private: class boost::basic_regex ....
I suspect that the problem is that the libraries were built as vc100 using b2 and the Platform Toolset is Windows7.1SDK. I cannot change the Platform Toolset to v100 or it generates a kernel32.lib missing error. And I cannot seem to figure out how to build boost so that it is sdk7. I am using Visual C++ Express 2010 and have also tried building from the Windows7.1SDK command line prompt.
Any ideas?
I meet the same issue when I install boost with installer from "BoostPro Computing". And I solve it by compiling boost lib from source code.
Here is the steps:
enter Visual Studio 2010 Express Command Prompt
cd to the unzipped boost dir
bootstrap.bat
.\b2
Done.
Here is my environment:
Win7 64bit
Visual Studio 2010 Express
boost 1_52_0
I build boost using a batch file that calls bjam, and I have not had any problems using the regex library in my projects. I am using VS2010 Pro. Here are the lines from my batch file, the extra library locations might not be important to you, I'm just putting them here for completeness:
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
SET ZLIB_SOURCE=%LIBS%\zlib
SET ZLIB_INCLUDE=%LIBS%\zlib
SET BZIP2_SOURCE=%LIBS%\bzip2-1.0.5
SET BZIP2_INCLUDE=%LIBS%\bzip2-1.0.5
SET BZIP2_BINARY=libbz2
SET ZLIB_BINARY=zdll
SET EXPAT_INCLUDE=%LIBS%\Expat\Source\lib
SET EXPAT_LIBPATH=%LIBS%\Expat\Bin
SET EXPAT_BIN=%LIBS%\Expat\Bin
bjam.exe --disable-filesystem2 --build-type=complete --user-config=%UTILS%\user-config.jam
Of course the path to your libraries and your user-config.jam will be different, and the only one line in user-config.jam that is really important is:
using msvc : 10.0 ;
After a successful build you will want to add the path to the stage/lib directory to you additional libraries settings for the project so the linker is satisfied. Also since this is an express build you may have to fiddle with the include path to pick up the platform SDK. I run this batch file from the top level Boost directory.

Building Qt for Visual Studio 2010 - cannot open file 'qtmaind.lib'

I'm trying to start a C++ Qt application and have it run and compile in Visual Studio 2010.
Doing some googling, I found that there was a Visual Studio Qt Add-in, and so I installed this. I already had a MinGW Qt binary installed, and when this did not work, I found that you have to compile the source for Visual Studio 2010 (the VS 2008 binary will cause deployment issues).
Using this as a guide: How to build Qt for Visual Studio 2010, I compiled the open source version, and added it to the PATH, along with a QTDIR env variable. Hoping that I got it finally working, I created a "Qt Application" using the New Project Wizard in Visual Studio 2010. Once I finished, I tried to build the program, only to see the following error:
1>LINK : fatal error LNK1104: cannot open file 'qtmaind.lib'
I looked in the C:\QT\lib folder, and found that I only have a qtmaind.prl, but no clue as to why there is no qtmaind.lib.
I am able to echo the QMAKESPEC environmental variable to get a 'win32-msvc2010' output.
I've tried several different combinations of flags for the configure step, including the one in the link, and even tried manually setting the -platform flag.
If anyone can offer any help, it would be greatly appreciated! :D
Just ran across this same problem. I changed the "-release" flag from the linked guide to "-debug-and-release", and then it built the qtmaind.lib library (presumably, that "d" suffix stands for debug).
I encountered this issue when running a project whose settings were hardcoded for a particular machine setup.
I could see that the vcxproj file had something like:
C:\Qt\4.8.1\libqtmaind.lib
I could not find this lib file in my machine. I replaced the line by:
C:\Qt\4.7.3\lib\Qtmaind.lib
It looks like the names of library files are different across Qt versions.Or, maybe the library files were renamed.
Also, the 'd' denoted debug. If you cannot find any lib file post-fixed by 'd', it is likely that you did not 'make' a debug version of Qt.
You can cross check this by opening the .Sln file created by configure. Mine was named Projects.sln. You can open this in notepad and see if win32-debug configurations are present.
This might be a QMAKESPEC issue. Try setting your QMAKESPEC environment variable to
win32-msvc2010
and rebuild Qt like that. This should give you .lib files in your Qt folder
in QT 5.3 with vs 2013
in properties -> linker -> input
remove ANY path for qt libraries such qtmaind.lib
this is known BUG

Resources