How to Add Linux Compilation to Cmake Project in Visual Studio - c++

Visual Studio has added lots of new features for C++ in the past year.
CMake
With the CMake support, I can do "Open Folder" and select a folder with a CMakeLists.txt file in it. Visual Studio does a lot of nice work in discovering and building it automatically.
Linux Compilation
Visual studio now supports remote compilation on Linux over SSH. Several tutorials show how users can create a new "Linux Console Application" in Visual Studio, and it will automatically ask to setup an SSH connection to be used for building it. I don't see any instructions for how to do this on an existing project of any kind.
Particularly with a CMake project, is it possible to open a CMake folder in Visual Studio 2017 and have it built over on a remote Linux machine? IfSoHow?

There is no build-in support for a VS "Linux Console Application" in CMake yet (as for CMake version 3.9).
Edit: Visual Studio 2017 15.4 now comes with something similar without generating actual .vcxproj files. See Visual C++ for Linux Development with CMake
With a standard CMake version besides the possibilities described here using existing .vcxproj files as a template, you can only trick CMake into generating those project types:
cmake_minimum_required(VERSION 3.7)
project(HelloLinux)
file(WRITE main.cpp [=[
#include <iostream>
int main()
{
std::cout << "Hello from Linux Console!" << std::endl;
}
]=])
add_executable(HelloLinux "main.cpp")
set_target_properties(
HelloLinux
PROPERTIES
VS_GLOBAL_KEYWORD "Linux"
VS_GLOBAL_ApplicationType "Linux"
VS_GLOBAL_ApplicationTypeRevision "1.0"
VS_GLOBAL_TargetLinuxPlatform "Generic"
VS_GLOBAL_LinuxProjectType "{D51BCBC9-82E9-4017-911E-C93873C4EA2B}"
)
This actually works and produces a Linux .vcxproj project that is accepted by VS. But since we sidestepped CMake here, none of the other compiler/linker options you define in your CMake script will be assigned.
So my recommendation is to raise a feature request for CMake itself to directly support this (e.g. via platform toolset option Remote_GCC_1_0).

It does not seem to work as you expect, yet. It seems you need to create separate linux vcproject for your existing cmake codebase. There is nothing like linux target in VS options. For more information see comments in this msdn blog.
You may either create 'new linux project' and copy your sources or try (and adapt) using for existing sources these unofficial scripts:
https://github.com/robotdad/vclinux

Related

How can I import a CLion C++ project into Visual Studio?

It seems like this should be easy and I've spent hours trying to find this answer online but haven't had any luck.
I can open the CLion folder path in Visual Studio, but that option doesn't give me the usual options to build and start the project. To do that, I need to open CLion as a Project/Solution, but I can't seem to do this.
My professor requires that my C++ code be executable in Visual Studio, but I prefer CLion. So I've done all my work in CLion and want to test that it runs in Visual Studio. How can I import my CLion project?
Thanks!
The link provided describes Visual Studio's CMake integration, which (similar to CLion) will install a version of CMake that Visual Studio will use. These instructions are pretty thorough and should provide everything needed to get your CMake project working in Visual Studio.
Now, you probably have two versions of CMake installed on your machine, one that came with CLion and one that came with Visual Studio. I would recommend installing the latest version of CMake on your machine separately, and configuring both Visual Studio and CLion to use that version instead. However, this is probably getting outside the scope of your immediate problem.
As you follow the Microsoft's instructions for "CMake projects in Visual Studio", you mentioned receiving the error:
1> [CMake] CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.17/Modules/CMakeDetermineSystem.cmake:173 (file):
1> [CMake] file failed to open for writing (No such file or directory):
This looks like a permissions issue, specifically while running CMake within Visual Studio, so be sure you have read/write access to all the files in your project, and the CMake packages in your Visual Studio installation. Hopefully, this doesn't require you re-install Visual Studio in another location on your machine, or run Visual Studio with elevated privileges, but perhaps that is necessary.
If you decide to install CMake separately, the instructions would be the following:
Install the latest CMake on your machine (somewhere you have adequate permissions), and ensure it is available in your Path environment variable. You can verify this by running cmake -version from the command line to see it is the version you just installed.
Using Windows command prompt, navigate to your CMake project directory (containing the top-level CMakeLists.txt file), and run the following:
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
You may run -A Win32 with the cmake command instead if your project is a 32-bit build.
Open the .sln Visual Studio Solution file that was generated in the build directory. Once, the Solution is loaded in Visual Studio, you can build the project (CTRL + SHIFT + B).

how to compile cmake on linux so that it can generate vsproj files?

I would like to generate visual studio project files on linux using cmake.
Unfortunately, the visual studio project file generator is not enabled by default on linux.
The cmake build instructions don't mention how to enable this feature on linux. It seems like the generators are enabled based on the host platform cmake is being build. Has anybody a patched cmake to enable the Visual Studio Project Generator on linux ?
We have tools that work on linux and windows for analyzing projects by parsing .sln/.vcxproj files. However generating .sln/.vcxproj files on windows is quite inconvenient as our main development is done on linux/osx.
You cannot generate Visual Studio Solutions under Linux. Nowadays you can generate Visual Studio solutions via ssh to a Windows docker container.

C++ OpenGL Project Setup using GLEW, assimp, SDL2 and CMake with CodeBlocks

I've done a whole tutorial about making a 3D Rendering Engine in OpenGL with Java and lwjgl and the guy who made the tutorial also wrote the engine in C++
To increase my knowledge about programming I wanted to take a look at the C++ version too. Also I believe (and I might be totally wrong) that I'm actually able to do a lot more with C++ than with java. The main problem I have is that I cannot get the engine running despite the included instructions and I would really appreciate if someone can help me out.
this is the engine i want to setup: https://github.com/BennyQBD/3DEngineCpp
I want to use CodeBlocks for this project since it was recommended and unlike Visual Studio it is free.. I also already downloaded glew, assimp and sdl and I installed CMake which are needed for this to run. Now I have to put that together according to this instruction here:
###Windows/MinGW###
- Make sure CMake is both installed and added to the system PATH.
- Open a Terminal and run:
```Shell
# install dependencies
# Install GLEW in %PROGRAMFILES%/GLEW or SET %GLEW_ROOT_DIR% to where GLEW is on your machine (Example: D:\PATH_TO_GLEW)
# Install SDL2 in %PROGRAMFILES%/SDL2 or SET %SDL2_ROOT_DIR% to where SDL2 is on your machine (Example: D:\PATH_TO_SDL2)
# Install ASSIMP in %PROGRAMFILES%/ASSIMP or SET %ASSIMP_ROOT_DIR% to where ASSIMP is on your machine (Example: D:\PATH_TO_ASSIMP)
cd build
# REPLACE "Visual Studio 12" with your preferred build toolchain (Maybe you want "Codeblocks - MinGW Makefiles")
# BTW VS 10 is VS 2010, VS 11 is VS 2012 and VS 12 is VS 2013, BLAME MicroSoft for the naming! LOL!
cmake -G "Visual Studio 12" ../
# open the generated SLN file (or cbp file if using CodeBlocks) and build!
```
- Copy the DLLs in /lib/_bin/ to /build/Debug/ and /build/Release/
- In Visual Studio, set the Startup project to 3DEngineCpp
- Move the res folder into the build folder
- Run
Major problem is, since I've only done java coding in eclipse I'm a bit confused..
What does he mean by "Open a Terminal and run: '''Shell" ?? and how am I supposed to install glew, sdl2 and assimp? what's cd build? and why do I need CMake, it isn't really mentioned what it does..?
Maybe someone can elaborate (step by step if possible) what I need to do in order to get this running, thanks a lot!
No guarantees my steps will work flawlessly as external dependencies in C++ is still very painful to deal with for me, but I'll give you some leads and hope you make some progress. Also, I've never used Code::Blocks, so I'm not sure if Visual Studio projects are compatible with it. This is the only way I know how to do things on Windows.
First, you'll need to install CMake. CMake is a utility that generates project files so that the project can be easily compiled on certain platforms. It generates Visual Studio project files on Windows, which will allow you to open the project in Visual Studio, and compile them from there.
In order to build the project, you'll have to sort out its dependencies first.
GLEW:
Download GLEW's sources and extract everything. It comes with Visual Studio project files
Open up Visual Studio with Administrator permissions
Open up GLEW's project
Build everything
Run the install "project" to get Visual Studio to install GLEW
Assimp:
Download Assimp's source from GitHub
Extract the project root directory somewhere. The root directory is the directory where CMakeLists.txt is in
Open CMake's GUI utility
Click Browse Source, and select that directory
Select an output directory by clicking Browse Build
Click Configure, when that's done click Generate. This will generate a Visual Studio project file for you
Open project with Visual Studio, build everything. Run install like you did before to install Assimp
SDL2: SDL's sources come with Visual Studio project files, so you can repeat the steps for GLEW to install it.
Now you can finally start attempting to get the engine to work. No guarantees that it'll work, but I'd try the same thing I suggested for Assimp as they're both CMake projects. Once the project files are generated, you can open it up in Visual Studio. Except this time, you won't really be installing it. You can modify the sources and run it like you would any other C++ project.

Open Cmake Project in Visual Studio like Qt Creator

I used to program at Qt Creator in linux But now I am working on Windows and Visual Studio (I am forced to it). When I was programming in Qt Creator these features was so great and useful for me:
Opening several cmake project at the same time and easy switching between them.
There was no need to run cmake command in terminal before opening the project and Qt Creator was handling this matter itself. Therefore, if there was any error in CMakeLists.txt I could correct it from inside of Qt Creator
Now, In Windows you should run cmake command in cmd before you open the project in Visual Studio.
Are there any Extensions or Add-on's for Visual Studio to have the mentioned features of Qt Creator for opening a cmake project?
Do you have any other suggestion for working on a cmake project in Visual Studio?
For your first question, Visual studio provide you a complete solution and you can add and switch between projects easily inside a solution. (see differences between projects and solutions in this post)
But for your second question, I don't know any official extension to do that. But once you created a solution with Cmake, you can easily change the CmakeList in VS.
If you insist in doesn't running Cmake commands, you can write an extension for visual studio see this for more information.
No. QtCreator plays better with cmake than Visual Studio. With QtCreator you can open the CMakeLists.txt as a "project" and run the cmake config step directly. With Visual Studio, you must run the initial cmake config (with the generator option) first to generate the projects and solution. With QtCreator you can also open multiple projects (corresponding to multiple top-level CMakeLists.txt files) together. With Visual Studio, you can only open one solution at a time, and each top-level CMakeLists.txt corresponds to a solution.
While QtCreator works better with cmake than Visual Studio, cmake and Visual Studio are still an excellent combination (and as a whole my preferred working toolset)--just one for which you must run the inital cmake configure step before being able to open the solution. Note that it is just the initial cmake config step that is required. Once you have generated the solution and project files and are using the VS IDE, subsequent changes to CMakeLists.txt files or any input files to the configure_file command will cause cmake to reconfigure before VS builds the solution.
Also Visual Studio 2013 works better than 2010 because when you do trigger a cmake reconfigure it will ask you if you want to reload all the projects. VS 2010 will prompt you to reload each one, which is a pain when you have a lot of projects. (I typically have 20-100 projects in a medium to large codebase.) And sometimes VS 2010 will crash with cmake reconfigures. (Nothing is lost--it is just a pain to have the IDE crash and have to re-open it.)
As for other suggestions:
my comment above about automatic reconfigures, is based on not setting CMAKE_SUPPRESS_REGENERATION to ON as suggested in a comment above by drescherjm. (Based on your workflow, this may be a fine way to proceed, it just conflicts with the way I have used cmake/VS and will prevent reconfigures as I described.)
For building you do not have to use the vcvarsall.bat script as described by DevSolar because that is all sorted out by cmake with the -G argument. For a 64-bit build with VS 2013, I use -G "Visual Studio 12 2013 Win64". (Note VS 2010 is vc10, VS 2012 is vc11, and VS 2013 is vc12.) It is still, however, helpful to have a correct runtime environment, which I get using
call "%VS120COMNTOOLS%....\VC\vcvarsall.bat" amd64
You can get a command line build by cd'ing to the build directory (the one containing the generated solution file) and executing
cmake --build . --config Release
You can also specify which project to build by adding --target MyProject
Now (2015), in Windows you should run cmake command in cmd before you open the project in Visual Studio.
Not anymore, with recent version of Visual Studio 19 (2020): See "Build systems and projects"
Open a folder that contains a CMakeLists.txt file.
CMake support is integrated into Visual Studio. You can use the IDE to edit, test and debug without modifying the CMake files in any way. This enables you to work in the same CMake project as others who might be using different editors.
CMake is the recommended approach for cross-platform development. For more information, see CMake projects.
No need to generate any files first.
That is why a project like Git, for instance, will ignore said generated files: they are not needed anymore to open a CMake project:
With Git 2.29 (Q4 2020), using the CMake support added some time ago for real with Visual Studio build revealed there were lot of usability improvements possible, which have been carried out.
See commit 0ad621f (30 Sep 2020) by Junio C Hamano (gitster).
See commit f2f1250, commit b490283, commit 2d9eb4e, commit 8c35e82, commit f1bd737, commit 8f45138 (30 Sep 2020), commit e18ae4e, commit 72b6eeb (28 Sep 2020), and commit 3eccc7b (25 Sep 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 8250ab0, 05 Oct 2020)
cmake: ignore files generated by CMake as run in Visual Studio
Helped-by: Đoàn Trần Công Danh
Signed-off-by: Johannes Schindelin
As of recent Visual Studio versions, CMake support is built-in: https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019
All that needs to be done is to open the worktree as a folder, and Visual Studio will find the CMakeLists.txt file and automatically generate the project files.
Let's ignore the entirety of those generated files.
That has practical consequence on a project on-boarding.
If we take the example of a project like Git, again:
cmake (Windows): initialize vcpkg/build dependencies automatically
Signed-off-by: Johannes Schindelin
The idea of having CMake support in Git's source tree is to enable contributors on Windows to start contributing with little effort. To that end, we just added some sensible defaults that will let users open the worktree in Visual Studio and start building.
This expects the dependencies (such as zlib) to be available already, though. If they are not available, we expect the user to run compat/vcbuild/vcpkg_install.bat.
Rather than requiring this step to be manual, detect the situation and run it as part of the CMake configuration step.
Note that this obviously only applies to the scenario when we want to compile in Visual Studio (i.e. with MS Visual C), not with GCC. Therefore, we guard this new code block behind the MSVC conditional.
This concludes our journey to make it as effortless as possible to start developing Git in Visual Studio: all the developer needs to do is to clone Git's repository, open the worktree via File>Open>Folder... and wait for CMake to finish configuring.

How to build Qt for Visual Studio 2010

I struggled finding a how-to which provides a stable solution for using Qt with Visual Studio 2010, so after collecting all the bits of information and some trial and error, I would like to write my solution into a guide.
The problem, or why is it not possible to use prebuilt binaries?
It seems that using binaries built for Visual Studio 2008 might work in some special cases, but I found them not to work. In my case they compiled OK, but they produce runtime errors, like this:
or when started from Visual Studio 2010:
Update: I found a blog post analysing why does it work for some people, while it does not for others. In one word, it depends on whether you have Visual Studio 2008 installed on the same machine, or not.
http://blog.paulnettleship.com/2010/11/11/troubleshooting-visual-studio-2010-and-qt-4-7-integration/
The most important thing (that I stupidly didn’t realize) was the fact that you CANNOT use the Visual Studio 2008 compiled libraries and dll’s (available on the Qt webpage) if you don’t have Visual Studio 2008 installed. The reason is because the Qt SDK you download is a debug build which is dependant on the VC9.0 DebugCRT, meaning it needs the Visual C++ 2008 Debug Runtime installed, which is NOT available as a redistributable installer. The only way to install the DebugCRT is to install the entirety of Visual Studio 2008.
First of all, it’s very important to understand that for using Qt with Visual Studio 2010, it's not possible to use the pre-built binaries which were made for Visual Studio 2008, but you have to compile it from source.
Downloading Qt
On https://www.qt.io/download/
Update 2017: the latest Qt 4.x branch (Qt 4.8.6) has 2 pre-built packages, which are now in the archive section:
http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2010-4.8.6.exe
http://download.qt.io/archive/qt/4.8/4.8.6/qt-opensource-windows-x86-vs2008-4.8.6.exe
You should not download Qt by clicking "Qt libraries 4.8.6 for Windows (Visual Studio 2008, 218 MB)", but by clicking on the "zip" link above it.
On that link, you get a big zip file like "qt-everywhere-opensource-src-4.8.6.zip". Unzip this into a folder and make its path something nice and small, for example "E:\Qt"
Visual Studio Command Prompt
Now that we have the sources, we need to build the binaries. To do it, open the Microsoft Visual Studio 2010\Visual Studio Tools\Visual Studio Command Prompt (2010) link from your start menu, or even pin it to the taskbar (a good idea). This is a special command prompt which has all the variables set for building with Visual Studio 2010 tools.
Once within the command prompt, navigate to your extracted Qt folder using old-school DOS way, which means you have to change drive letter by E:, enter directories by cd Qt and list dir contents by dir. You can use the tab key for helping you with the directory names. When you have arrived at the correct directory, a dir command should return something like this.
Building Qt
Now it’s time for configure and build. For configuring a minimalist Qt, I'm using the following flags with configure.exe. Just copy and paste it into the command line. Look in the Qt reference manual for what flag to use or not to use.
configure.exe -release -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg
Once configure.exe has finished (it was 10 minutes for me), you'll need to start the build process. It will take about 20-30 minutes with the above flags. To start it, just type:
nmake
Setting environment variables
Basically, we are done. All you need to do is to set your environment variables (QTDIR and PATH), which tell programs where to find Qt. If you are on Windows 7, you can use the following command to set QTDIR to your installation dir.
setx QTDIR e:\Qt
For setting the PATH, I strongly recommend using Path Editor. Within Path Editor
add the directory of Qt\bin to your PATH
(it doesn't matter if it's in system path or user path)
If you prefer to use Control Panel\System\Environment Variables, then you can set these there, too.
Qt Visual Studio Add-in
Here you go, after a logoff-logon or a restart, all the Qt demo applications should start correctly (I recommend have a look at bin\qtdemo.exe). Now you can download and install the Visual Studio Add-in (qt-vs-addin-1.1.9.exe) from the Qt download page, it will work perfectly.
Appendix A: Official Instructions:
There is a page at the official wiki at the Qt website called Qt 4.8 Installing Qt for Windows, but I found it lacking important information.
References
Qt DevNet forums
Recommended flags for a minimalistic Qt build
Building Qt 4.5 with Visual C++ 2010
How to compile Qt as static
Qt 4.8: Configure options for Qt
Edit the PATH environment variable in
Windows without pain - op111.net
Qt V4.8.0 contains prebuilt binaries for Visual Studio 2010 so you don't need to do this anymore:
http://qt.nokia.com/downloads/windows-cpp-vs2010
Refer below link and it might be useful
1)https://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers/Build_Instructions/Prerequisites/Qt#Windows_3
2)http://eecs.vanderbilt.edu/research/hmtl/wp/index.php/qt-vs/