Question

I've been using cmake in linux for a while now, but I needed to move over to working in Visual Studio for a few reasons...

I have the cmake project generated, and I can load up the solution in Visual Studio, but when I try to add a file, it gets added to to location of the build files, rather than into the source directories. Is there any way around this, or do they have to be added through Explorer, then regenerate the cmake build files, and then re-open the solution? This seems a tad clunky, and I was hoping for an easier method.

Was it helpful?

Solution 2

To add the new file via Visual Studio, you can select the "Add -> New Item" option. In the resulting window, there should be a "location" text input box. This has the build directory specified by default, but the path can easily be changed to somewhere in your source tree.

OTHER TIPS

Yes, you should add files via the CMakeLists.txt file rather than through VS. There are a couple of choices here.

The recommended way is to list all source files explicitly in the CMakeLists file. If you want to add a new file, it has to be added to the CMakeLists.txt. The advantage with this method is that after you modify the CMakeLists file, the entire build is "out of date", so just trying to build any target in VS will cause CMake to re-run and add the file to the appropriate target.

Another option is to make use of CMake's file(GLOB ...) command. This will search the given path for sources and can be used to automatically generate your list of sources. The upside is that it's less tedious than listing all files by hand in the CMakeLists file. However, the downside is that CMake would have to be manually re-run if a new file was added to a source directory, since CMake wouldn't know that the source dir was out of date. I have to say that I don't generally find this to be a problem - if you've just added a file and it doesn't appear in your VS explorer it's pretty obvious, and certainly by build time it's even more obvious since the build fails (or you're not the using the file anyway).

Note that for either option, you don't need to close VS if you're re-running CMake.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top