These are sample files that, hopefully, will help you use a folder with single-file C/C++/Assembly programs in Visual Studio.
I use these files with my DrFrankenstein/prompts repository, which contains my solutions to Advent of Code, Project Euler, etc. problems. All of those solutions are self-contained, single-file programs. Maintaining a solution, CMakeLists, or Makefile for a bunch of small programs can get tedious, as a target needs to be added for each one. (Granted, CMake isn't that hard to use, but still...) I wanted to be able to just create a .c file, then build/run it with zero configuration.
This can also be used for running quick tests without creating a project. Just drop the (adjusted) config files where you need them and open the folder.
- Open your folder in Visual Studio (File/Open/Folder...). This requires VS 2017 or newer (I believe).
- In Solution Explorer, enable the "Show All Files" button.
- Copy the
CppProperties.json
file from this gist to your folder, and update the IncludePaths to point to your vcpkg include folder. Alternatively, you can remove that folder if you don't use vcpkg. - Create a
.vs
folder (if missing) at the root of your folder, and copy thelaunch.vs.json
andtasks.vs.json
files to it. - Update
tasks.vs.json
so that theCFLAGS
andCPPFLAGS
lines point to your vcpkg include folder. Alternatively, you can remove the/I
flag completely if you don't use vcpkg. Add any flags you might want to use (such as optimizations). - Right click a .c, .cpp, or .asm file, then select Build to compile it (sometimes it takes a couple of right clicks for the item to work). A .exe file will appear next to your source.
- To debug that .exe file, right-click it and select "Set as StartUp element", then hit F5.
- If you use the debugger, you can use the entries in
launch.vs.json
as a template to customize how your programs are launched.
Optional: I have all these files in my .gitignore
, as the build setup is really up to whoever clones that repo.
The tasks file just calls nmake
with ${fileBasename}.exe
as the target, and compiler flags passed as environment variables. It takes
advantage of the Predefined rules so that you don't need to
create a Makefile for your folder. If you do need to customize how a specific program is compiled, you can create a makefile with rules
just for those outliers.
The CppProperties file configures Intellisense to work with your folder.
Note that if you prefer using .cxx as the extension for your C++ files, you need to change CPPFLAGS
to CXXFLAGS
in the variables.
If you use .cc for your C++ files, it'll use the CFLAGS
variable, which may be undesired behaviour. This can be fixed by creating a
Makefile with a custom .cc.exe
rule (but that's what we've been trying to avoid, right? just use .cpp).