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

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.

Related

Select MSVS build configuration when generating a buildsystem with CMake

I have a multiplatform CMake project, and occasionally I have to build it manually for Windows. I generate a buildsystem like this:
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" -A x64 ../path/to/source
Then I open *sln file and press F7 to build. It runs for 40 minutes, and after that I understand that I didn't select proper configuration in the combobox. It's annoying! When command line option was -DCMAKE_BUILD_TYPE=Release, but combobox was set to Debug, build fails after spending a decent time.
Is it possible to generate an MSVS project with build configuration selected from command line?
Note that I'm aware of msbuild command and it's -p:Configuration=xxxxx flag. The question is about cases when for some reason you need to build from Visual Studio's GUI.
Changing the selected configuration for the GUI is not possible with CMake at this moment.
The main reason for this is that this information is stored in the .suo file generated by Visual Studio. CMake itself only generates the project and solution files, while the .suo file will be generated by Visual Studio itself.
Alternatively, use CMake's command line build option for this. After configuring your project and generating the VS .sln file from CMake as usual, simply run:
cmake --build <path_to_build_directory> --config Release
This works independently of the selected generator and is the most reliable way of building CMake projects.

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/

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

QtWebKit for Qt Visual Studio Add-in

I want to learn how to use QtWebKit by creating a simple project, but I can't even install it. I found some tutorials like this, but it's for the standard Qt package. I am using for another project the Visual Studio Qt Add-in, so I don't want to uninstall it.
I found some WebKit source code in Qt-VS, but I don't know what to do with it. It does not contain any Perl script so the tutorial above is not good for it, but it does contain some makefiles. Or should I download the QtWebKit package separately, and follow the tutorial above? Will it generate compatible libraries? (I could not find any Qt command prompt, and the tutorial says the VS command prompt must be compatible. How do I know it?) Also, where should I move the binaries generated, so the project made with the built-in Qt template in Visual Studio to be able to find these new files?
Sorry for these lame questions, but I get lost really fast when it comes to building stuff from source and not given in binary form.
I would appreciate any feedback or link to stuff
EDIT:
The only thing I could pull of is to install another copy of Qt, the normal one, and use it for the WebKit project changing the Qt environment variable value each time I'm switching the project, but that would be the lamest thing ever.
Download the Qt 4.7.3 source code:
http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-4.7.3.zip
Unzip and copy to a convenient location.
Open a Visual Studio 2010 command prompt.
You may need to run the command prompt as administrator. To do this, go to Start Menu > All Programs > Microsoft Visual Studio 2010 > Visual Studio Tools, right-click on Visual Studio Command Prompt (2010) and select Run as administrator.
To make a 64-bit build, select Visual Studio x64 Win64 Command Prompt (2010) instead.
Change the working directory to the Qt top folder.
Run the commands:
configure.exe -platform win32-msvc2010
nmake
Wait an hour.
This will build Qt, with all components, including QtWebKit.
Does this answer your question?
user763305's steps will work with these additions:
You need to install Perl. I used "ActiveState Perl"
You need to install WinFlex and WinBison. For some reason the QT script refers to the flex as "win_flex" but tries to find an executable called "bison.exe." I renamed "win_bison" to "bison.exe" and it worked.
I was also able to configure it successfully and build for msvc2012
configure.exe -platform win32-msvc2012
nmake
And it didn't take a few hours on my machine. Just about 55 minutes.

Resources