Using cmakesettings.json with cmake - visual-studio

I am novice at cmake, so please be gentle.
I have a medium sized project that has been using visual studio with cmake. I would like to use the build/install features that I currently use in visual studio, but with cmake in the terminal.
Currently the project uses a cmakesettings.json to switch between the different builds and locations. When I searched this though, it seems to be specific to visual studio?
https://github.com/microsoft/CmakeSettings
Is there a tool or easy way to use these settings with cmake instead of visual studio?

Note that in VS2019, the correct way to do this is to use CMakePresets.json and then you can build in a VS2019 or higher, VS2022, or in a CI using cmake from the cmd.exe. CMakeSettings.json is deprecated. To enabled support for them, there is an option that needs to be enabled. See this link https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-160 for more information.

Related

Compiling a static program in Visual Studio (VCPKG / CMAKE)

I am trying to create static program in Visual Studio, where I end up with a single executable that I can deploy to other PCs.
I am using VCPKG to static download libraries, as per the instructions here:
https://devblogs.microsoft.com/cppblog/vcpkg-updates-static-linking-is-now-available/
In the following post, the answer to the question is to use VCPKG and then CMAKE
Using Cmake to build ssh.dll with Visual Studio 2017
My question is with regards Cmake. If VCPKG downloads and creates folders that my project links to. What's Cmake for and why would I need to use it?
What's Cmake for and why would I need to use it?
https://cmake.org/ read the text below the logo.
It can be used to generate a project files for different build system, like make, msbuild, ninja etc.
It also can be used as a general scripting language.
You don't need to use it but it is highly encouraged for consuming other dependencies.
vcpkg however will download cmake since it is used as a scripting language within vcpkg.

Visual Studio, CMake and Toolchain Files

before I explain my current problem with Visual Studio in combination with CMake, I try to explain what I want to accomplish as there might be a better solution.
At our company we have a huge code base mainly written in C for QNX. For development we are using the standard IDE QNX momentics. As this is based on a quite old eclipse version ... the whole IDE sucks.
I'm currently evaluating to use Visual Studio as a replacement. With a really simple HelloWorld-programm I was able to build and debug the project from within VS using CMake and a CMakeSettings.json.
As our code base is basically organized in a tree-like structure, I've created a typical hierarchy of CMakeLists.txt which allows me to build single/multiple projects.
As QNX delivers its own compiler I've created a toolchain file, this is running fine so far, the required toolchain is invoked once a build is triggered via cmake.
Now for the part that I'm struggling with:
How to use this CMake setup in order to develop/debug code from VS?
I had two ideas in mind which don't really work or I'm doing something wrong.
Use the CMake generator for VS 2019 and generate a complete solution. Problem: The toolchain file is not "used" as all projects will be setup with the internal VS compilers. Is there any way to get this working? I thought calling cmake -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=$FILE would do the job. Additionally: how would I incorporate the debugger configuration? With this solution the CMakeSettings.json seems to be ignored as VS solutions are used.
Use File->Open->CMake to open the root project. Shouldn't this show all "contained" projects which also include a CMakeLists.txt as projects? I can only see the folder tree in the Solution Explorer. Build a single project from the solution explorer is also not possible ... there is simply no option.
Anyone with experience on this topic?
Minor comment please limit questions to 1 question in the future. Multiple questions clutters questions and makes search results worse.
============================================================
I'll answer question #1 this part of question number 1.
"Use the CMake generator for VS 2019 and generate a complete solution. Problem: The toolchain file is not "used" as all projects will be setup with the internal VS compilers. Is there any way to get this working? I thought calling cmake -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=$FILE would do the job."
Instead of using Visual Studio as your generator try using "Ninja" instead. Ninja will work if your toolchain code is correct. Toolchains and visual studio are complicated to say the least. If you wanna go down that rabbit hole I can help but that's a separate question
Basically try switching to Ninja instead of Visual Studio as your generator for CMakeSettings.json. Ninja is very easy to write toolchains for.
======================================================================
"Additionally: how would I incorporate the debugger configuration?"
The MSDN articles are actually quite good. I figured it out this week. In my opinion the debugger configurations are more powerful with the CMake approach than vanilla visual studio.
Configure CMake debugging sessions
https://learn.microsoft.com/en-us/cpp/build/configure-cmake-debugging-sessions?view=msvc-160
"Tutorial: Debug a CMake project on a remote Windows machine"
https://learn.microsoft.com/en-us/cpp/build/cmake-remote-debugging?view=msvc-160
Trust me once it clicks you'll love it.

Using Clang-Tidy to gate check in for Visual Studio

I'm trying to use Clang-Tidy to automatically gate check-ins/builds on Visual Studios.
I know that in cmake there is a variable "CMAKE_CXX_CLANG_TIDY", but setting it to my tidy executable with the proper checks doesn't seem to do anything when building the project.
Also, I am unable to easily generate a compile_commands.json for Clang to use. Using CMAKE to generate it messes up individual dependencies, and the only way to generate it through Visual Studios seems to be non automated methods such as Extensions.
I am running the latest version of Clang, Cmake 3.11.3, and Visual Studios 2017.

How to link against Windows system libraries with CMake?

I am building a simple Windows application that needs to link against gdiplus and winmm.
Previously I was using
find_library(GDIPLUS gdiplus)
target_link_libraries(target ${GDIPLUS})
but CMake does not find the library when using the Visual Studio 2015 target with the Visual C++ Build Tools. According to the answer here I should probably drop the find_library and just use
target_link_libraries(target gdiplus.lib)
but I'm not sure if this will only work for Visual Studio or for example also with Cygwin.
So what is the correct (or best) way to link against Windows system libraries from CMake?
I have been looking to link with Rpcrt4.lib and this worked on windows
target_link_libraries(${"LIB_NAME"} rpcrt4.lib)

How to use GCC with Microsoft Visual Studio?

I am creating a very large project (a few thousand lines) and so would rather not use Notepad++. An IDE would make it so much easier. I have experience with Microsoft Visual Studio and love it. Is there some easy way to use Cygwin's GCC from within Microsoft Visual Studio?
Alternately, are there any other good Windows IDEs for GCC besides NetBeans and Eclipse? (I hate both of them with a passion.)
There are several ways to go here:
Option 1: Create a Custom Build Tool
Visual Studio 2005 and newer will let you register custom build tools. They tell the IDE how to transform files of one form (e.g. a .cpp file) into another form (e.g. an .obj file).
So far as I know, no one has done this yet for GCC. And, doing it yourself requires writing COM code, which is probably too deep a pool to dive into just for a single project. You'd have to have a compelling reason to take this project on.
You then have to manually adjust each project to tell it to use the custom build tool instead of the default, since you're using a file name extension (.cpp, probably) that Visual C++ already knows about. You'll run into trouble if you try to mix the VC++ and g++ compilers for a single executable built from multiple modules.
On the plus side, if you were looking to start an open source project, this sounds like a good one to me. I expect you'd quickly gather a big user base.
Option 2: Makefile Project
Start Visual Studio and say File > New Project.
In the Visual C++ section, select Makefile Project
Fill out the Makefile Project Wizard:
Build command line: make
Clean commands: make clean
Rebuild command line: make clean all
You can leave the Output (for debugging) field alone if you've named your executable after the project name and it lands where Visual Studio expects to find it.
Leave the rest of the fields alone unless you know what they are and why you want to change them. As an example, you might choose to pass a -D flag on the Preprocessor definitions line to get separate debug and release outputs. If you know you want this, you know how to set it up, so I'm not going to make this long answer even longer in order to explain it.
You'll be asked the same set of questions for the Release build. If you want to bother with separate debug and release builds, you'd make any changes here.
Having done all this, you still have to create the Makefile, and add a make.exe to your PATH. As with the debug vs. release question, going into that level of detail would push this answer off topic.
As ugly as this looks, it's still easier than creating custom build tools. Plus, you say you need to port to Unix eventually, so you're going to need that Makefile anyway.
Option 3: Cross-Platform Development
You say you want to port this program to Unix at some point, but that doesn't mean you must use GCC on Windows now. It is quite possible to write your program so that it builds under Visual C++ on Windows and GCC/Makefiles on *ix systems.
There are several tools that make this easier. One very popular option is CMake, which is available as an installation time option in newer versions of Visual Studio. There are many alternatives such as SCons and Bakefile.
Clang
You can use the Clang compiler with Visual Studio to target Android, iOS, and Windows.
If you are targeting Android, you can use the Clang/LLVM compiler that ships with the Android NDK and toolchain to build your project. Likewise, Visual Studio can use Clang running on a Mac to build projects targeting iOS. Support for Android and iOS is included in the “Mobile Development with C++” workload. For more information about targeting Android or iOS check out our posts tagged with the keywords “Android” and “iOS”.
If you are targeting Windows, you have a few options:
Use Clang/LLVM; “Clang for Windows” includes instructions to install Clang/LLVM as a platform toolset in Visual Studio.
Use Clang to target Windows with Clang/C2 (Clang frontend with Microsoft Code Generation).
GCC
If your project targets Linux or Android, you can consider using GCC. Visual Studio’s C++ Android development natively supports building your projects with the GCC that ships with the Android NDK, just like it does for Clang. You can also target Linux – either remotely or locally with the Windows Subsystem for Linux – with GCC.
Check out our post on Visual C++ for Linux Development for much more info about how to use Visual Studio to target Linux with GCC. If you are specifically interested in targeting WSL locally, check out Targeting WSL from Visual Studio.
Source: https://devblogs.microsoft.com/cppblog/use-any-c-compiler-with-visual-studio/
I'm from the future.
I keep (poking at) a C/C++ toolchain using Visual Code on Win/Lin/Mac and MinGW installed from Choclatey.
(This was done for my sanity - install GDB and GCC however you want)
I've run it with GCC and GDB with IntelliSense using MS's own weird JSON makefiles.
Someday, someone (you?) will write a Gradle or Python script to generate these; for now the examples online in the docs seem to work.
It seems to require three types of JSON thing;
a single IntelliSense configuration for the whole workspace
a Debugging Configuration entry for each binary you want to debug
these can invoke the build tasks
a Build Task per-artifact
I don't think that there's a "require" or "dependency" thingie-mah-bob; sorry

Resources