VSCode CMake Extension rebuild all project each time - c++

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.

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).

Linking errors in Poco with Conan package manage on Windows [duplicate]

I have a project which i have generated with cmake and running in visual studio 2010.I changed the configuration to x64,in visual studio my active solution and the Target Machine in(Properties->Linker->Advanced) is set as x64.I still get the linker LNK1112 error.Is this something which i set in cmakelist.txt if so what is the command?
-swetha
It's not something you'd want to set in CMakeLists.txt. Basically, CMake has multiple generators for different compilers. (The x86 and x64 compilers are two distinct compilers on Windows.) When you generate the build files, you need to pass along the correct compiler for CMake to use, or if you use the GUI, select Win64. From the command line:
cmake -G "Visual Studio 14 Win64" path/to/your/CMakeLists.txt
or whatever version of Visual Studio you want. You can see the available generators with:
cmake --help
If this didn't solve your problem, try it again after deleting the generated build files.
If that still doesn't solve the issue, you are linking to a third party dependency built for x86.
Try to delete all *.obj files in your solution and let compiler compile all files again. This problem may cause of compiler try to reference obj files that was compiled x64

Cocos 2d x not generating libcoco2d.vcxproj or libspine.vcxproj

My config:
Windows 10, python 2.7, Visual Studio 2015 community
I downloaded cocos, I successfully run setup.py
Then I proceed to execute "cocos new -l cpp -p com.whatever.Test -d D:\Projects\Cocos\Test".
Everything seems to run ok, no error message whatsoever. If I go to my folder, I can see all the project folders etc.
I open the sln file within Win32 and it opens the project in visual studio 2015.
Here comes the problem: the solution has 6 projects. 2 of them are not loading and, as a consequence, the main project fails. The failing projects are libcocos2d and libspine. There are no project files generated in their respective folders, so VS can't find the required libcoco2d.vcxproj or libspine.vcxproj.
I'm lost here people, I don't have a single clue on what do I have to do to generate both projects.
Any help will be very much appreciated.
Thanks
I found what was wrong. The cocos.py script, at some point, calls CMake. I downloaded CMake and I built the projects using that. It failed. Then I realized that CMake was using the wrong compiler. Even having Visual Studio 2015, the correct compiler is Visual Studio 2014, not Visual Studio 2015 (Thanks a lot M$...). Then CMake was working. After that I tried the script again, and everything is perfect now.

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.

TInyXML++ Premake with Visual Studio 2010

I tried to compile TinyXML++(or TICPP) using premake4 with these settings: "premake4 vs2010 [--unicode] [--ticpp-shared] [--dynamic-runtime]". It compiles the .libs but I can't compile TiCPP.vcxproj because the compiler says "Unable to start program c:\ticpp\lib\ticpp.lib. This file is an unrecognized or unsupported binary file." Anyone got a clue how to make this work? Oh and one more thing, why does Windows ask me if I want to open a .vcxproj with Visual Studio 2010 or Visual Studio 10? How do I make it so that it only picks one, and what's the difference. I tried the first answer, but it didn't work.
Download last version of premake, it does support VS2010
I have compiled tinyXML++ using it, by executing:
premake4 vs2010
it does generate valid sln and vcxproj files.
From oolua build instructions
Visual Studio 2010 (vs10) is not currently supported by premake yet if you have vs10 installed you can run the following command, it will generate vs9 projects and update them using the vs10 command line tool.
premake4 vs2010
As vs10 adds files which have not been created directly by premake, a specific clean operation has been added to the premake script.
premake4 cleanVS10
Scripts to build a local install, build and run unit test etc. have been added for vs10 in the script directory.
Notes:
* oolua's premake4 script may not be the same as yours.
* VS2010 support is borked in premake. Current workaround is to make a 2008 project and make VS2010 convert it for you.