I was having this issue with a much larger project but I reproduced it with a quickly throw together example. Visual Studio correctly generates everything using CMake, and if I then go to the toolbar and select Build -> Build All it will successfully compile and link everything, and I can run the resulting executables from powershell... The Output from CMake is shown to be this:
According to the Visual Studio CMake documentation I should see an additional line stating: Target info extraction done.. I believe that is the core of my issue, but I am unsure why it is not occurring as everything else seems to work fine.
If I try to run (or debug) anything from Visual Studio itself, it simply says I need to select a "Startup Item", but none are available:
Looking at Visual Studio CMake documentation here, I should be able to simply go to the CMake Targets View in the solution explorer, and see all of my targets, but I do not see any:
While I should see (in this very simply example) a target for hello, given I have the following CMake structure:
Root CMakeLsits.txt file:
cmake_minimum_required(VERSION 3.9)
project(vira LANGUAGES CXX VERSION 0.9)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
add_subdirectory(src)
src/CMakeLists.txt:
add_executable(hello hello.cpp)
src/hello.cpp:
#include <iostream>
int main()
{
std::cout << "hello world!\n";
return 0;
}
I have been driven crazy by this as the people I've asked about it have simply reiterated what the Visual Studio documentation says, yet neither for this simple project, nor the larger actual project I've been working on, do any CMake targets ever show up. Meaning I'm unable to use any of the debugging features of Visual Studio, since I cannot select a startup item.
Am I doing anything incorrectly? Visual Studio clearly at least recognizes the CMakeLists.txt as it automatically generates everything, and will Build the project if I manually go to Build -> Build All. That has me even more confused as to why I'm experiencing this.
I have attempted even reinstalling Visual Studio from scratch, yet nothing has helped.
Related
I'm working on a project in C++ configured using CMake.
Each time I build the project, all files are recompiled, which is unbearable considering the time it takes.
With Visual Studio Code
I'm trying to use VSCode and the CMake Extension.
The CMake:Configure command works well and so does the CMake:Build command. It takes a lot of time to compile because I use highly templatized header-only libraries like Eigen3, Boost and CGAL, so I need to build a pre-compiled header. It build some librairies too. But it is normal that it takes time for a first compilation.
However, if I build the target again (without modifying anything in the code) the compilation is completely redone ! I just want the modified source files or librairies to be re-compiled. Or if a header in the list of pre-compiled headers is modified, only then should the pre-compiled headers be recompiled.
With Visual Studio 2019
I thought that VSCode didn't work with a concept of "project", and therefore had to reconfigure and recompile the whole project every time it was launched.
So I decided to generate a Visual Studio Solution with the CMake-GUI program and use Visual Studio 2019 instead of VSCode.
Now when I build a second time my project, nothing is recompiled and the program is directly lauched. Visual Studio say :
========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========
Wonderful!
Unfortunately not..., because if I modify a file, Visual Studio keeps telling me that everything is up-to-date and my modifications are not taken into account. I need to "Rebuild" the project so that everything will be recompiled, which does not solve my problem as it takes a lot of time.
Question
How to recompile only modified source files, librairies or pre-compiled headers (with VS Code, Visual Studio or both) ?
Plaform : Windows 10
Edit 15.04.21
I did some more methodical testing today to find out what the problem might be.
I used a very simple project to do the tests.
CMakeList.txt
cmake_minimum_required (VERSION 3.8)
project ("HelloCMake")
add_executable (HelloCMake "HelloCMake.cpp")
HelloCMake.cpp
#include <iostream>
int main() {
std::cout << "Hello CMake !" << std::endl;
return 0;
}
1. With VSCode and CMake Tools Extension
With the CMake Tools Extension, VSCode can configure and build a CMake project. It generate a Visual Studio Solution (.sln).
Build the target with CMake Tools Extension which makes VSCode to run the command :
cmake --build <PROJECT_DIR>/build --config Release --target HelloCMake --
/maxcpucount:14
The entire target is compiled
I build it again (without changes)
The target is completely recompiled again
The problem is that the target is completely recompiled, every time. I asked the question directly to the CMake Tools developers. GitHub Issue
2. .sln project generated by CMake (Visual Studio 2019)
I generated a Visual Studio Solution with CMake and open it.
I build my project HelloCMake
I do some modifications in source file
I build the project again, but Visual Studio never takes my modifications into account. The builds are always "up-to-date".
The problem is that Visual Studio does not take my changes into account. I have to rebuild the project, which takes too much time.
3. Directly open CMake project with Visual Studio 2019 (Ninja)
If you open a CMakeList.txt with Visual Studio 2019, it is not a Visual Studio Solution that is generated but a Ninja one.
It seems that this way the compilation works correctly !
According to this research, it is the CMake Tools Extension for VSCode as well as the Visual Studio Solution that cause problems.
I can't figure out why such a simple project doesn't compile properly with a Visual Studio Solution generated by CMake.
Basically, I've got the same question as in
How to configure CMake so that the generated Visual Studio project finds the executable?. None of the answers there worked for me.
I have a CMake project, and I just want to create a Visual Studio 2019 Project from it:
So I just run cmake . from the root directory.
Then I have a *.sln file in my root directory.
After opening it with Visual Studio 2019, I press the "Local Widows Debugger" button, it compiles successfully but then the IDE complains with:
Unable to start program 'C:\Users...\x64\Debug\ALL_BUILD'. The system is unable to find the specified file.
Using travis everything compiles fine, too: https://travis-ci.com/Necktschnagge/markov_chain_analyzer/builds/144941090
You can see the code here: https://github.com/Necktschnagge/markov_chain_analyzer/tree/old
What do I need to do so that CMake creates a VS solution, that is well-configured so that I can run the debugger?
When you create a Visual Studio solution with CMake, it automatically sets the Startup Project to ALL_BUILD. This is a CMake pre-defined target, which builds all of the projects in the solution. It is not a project containing an executable that can be debugged.
You must change the Startup Project in your Solution Explorer to one of your executable projects before debugging. You can do this by right-clicking on the project you want to debug, and selecting Set as Startup Project. There are some more instructions here if you're using VS with CMake integration.
You can also tell CMake to set a different Startup Project by default when building CMake from scratch, using VS_STARTUP_PROJECT. Put something like this in your top-level CMake file:
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT MyExeTarget)
Now, CMake will set MyExeTarget as the Startup Project in Visual Studio, instead of ALL_BUILD.
I have a simple project where the file structure looks like this:
- CMakeLists.txt
- main.cpp
The CMakeLists.txtlooks like this:
# Project initialization
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial main.cpp)
When I run the Cmake GUI I get:
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_CXX_COMPILER could be found.
I have Microsoft Visual Studio 2017 installed. I have compiled and run apps from it. The basic example from the CMAKE tutorial does not work.
Can anyone tell me why?
I'm not sure what is going wrong but you might want to take a look at:
https://learn.microsoft.com/en-us/cpp/ide/cmake-tools-for-visual-cpp?view=vs-2017
Visual Studio 2017 is able to open cmake files directly (should do the generator step for you behind the scenes) which may avoid the problem you have.
Use the integrated Visual Studio CMAKE.
The error reporting in the VS build/output/error window has become somewhat complex, and many errors are delegated between the toolchains, etc. (i.e. the whole VS has become modularized lately)
Universal truth: log files are your best friend.
In my case,
CMakeError.log has been constantly complaining it can't find kernel32.lib
Guess what? I forgot to install Windows SDK.
Basically, for any serious work, you need at least MSVS and Windows SDK, if you want to build for Windows. (Windows SDK is also now known as "Windows Kits", which is what you'll get in the StartMenu).
Essentially, your problem might simply be you don't have proper dev libs installed.
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
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.