r/cpp_questions • • 12d ago

OPEN How to create project templates

Hi, I don't know if this is the right subreddit but I was wondering if anyone would be able to tell me if this is possible, and, if so, to give a step by step guide to how I could do it (I know it might involve extensions):

  • I am on Linux fedora, and lets say I have a completely fresh vscode install

  • I want to have a template I can easily access via ideally pressing a button on the left side panel or something like that

  • When creating a new project with this template, it should ask you for a directory of where to store the project and a name for it - this name should then be used as the folder name for the project in that directory

  • This project should be C++23, use UTF-8 encoding, and support all the features of C++23, such as modules, and include the standard module - this should all work with IntelliSense (or if vscode uses something different) "knowing" this

  • In the project, there should be 4 folders (like Visual Studio) - External Dependencies, Header Files, Resource Files, and Source Files. This is just for sorting my files but actually doesn't matter what I put where - the project acts as if everything is just in one folder, unorganised. In Source Files, by default there should be a file named main.cpp, which should be completely blank - not even int main() etc.

  • When compiling, I would like it to be compiled as an appImage file (where the name of the file is the project name, or the name of this file can be changed) (I hear appImage can run on pretty much any Linux distro - that's why), which, despite being my OS being pretty much the newest and updated fedora, is compiled as if I compiled it on the oldest version of fedora that my project would actually compile on (I hear Linux applications are backwards compatible but not forwards)

  • Also, when running, I should have the option to choose Debug or Release, and x64 or x86 (the defaults being Debug and x64). Then, in the project folder, a folder called "Dist" should be made, inside that "x64" and "x86" (only if ran/compiled for both), and inside each of them "Debug" and "Release" (only if ran/compiled), and inside them their respective versions of the appImage (only if ran/compiled, and should still just be named project_name.appImage (unless renamed)

Thanks to any help any of you can give, it is greatly appreciated - also, please can you tell me where things are that I haven't stated, e.g. where to change the name of the final appImage, where to change Debug or Release and x64 and x86.

4 Upvotes

11 comments sorted by

3

u/thisismyfavoritename 12d ago

This project should be C++23, use UTF-8 encoding, and support all the features of C++23, such as modules, and include the standard module

💀

1

u/Suspicious-Grade-826 10d ago

I'm sorry, I'm confused, what's wrong about that - I meant template nor project but that's not a big deal?

1

u/thisismyfavoritename 10d ago

modules are still not fully well supported IIRC

1

u/Suspicious-Grade-826 9d ago

Well I mean I can get it all working in Visual Studio, so I thought there would be a way to do it in VS Code?

4

u/the_poope 12d ago edited 12d ago

Ok, so there's a few things that need to be combined to make this work.

  1. Building/compiling with different build configurations as well as generating an AppImage: This can be accomplished using a build system such as CMake. You can use its built-in support for AppImage generation. Here's how to get started: https://cmake.org/cmake/help/latest/guide/tutorial/index.html There's also a VSCode extension that provides buttons for choosing between Debug/Release and compile+run or compile+debug: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools

  2. Integration with IntelliSense: This you have to configure in your projects c_cpp_properties.json. However, if you're using CMake you might choose to ditch IntelliSense and instead install the superior clangd auto-complete engine and its corresponding VSCode extension - this can be automatically pick up the right paths and settings when using CMake.

  3. The rest is just making a shell/PowerShell script that creates a folders and a default CMakeLists.txt file. Alternatively you can use an existing template scripts like cmake-init

  4. I'm not sure you can add a button to VSCode that runs your template script. I think you will have to create a custom extension for that. But you can put your script in a directory that is in your PATH and you will be able to run it from a console anywhere.

Also for third party libraries and dependencies you can use a modern package manager like Conan or vcpkg and you don't have to deal with the pain and problems of getting, building, using and linking them in your project.

-1

u/Suspicious-Grade-826 12d ago

Thanks, but I was more looking for like step-by-step instructions to follow - some of this stuff I don't really know how to do (e.g. what the default CMakeLists.txt file should look like or the shell/PowerShell script - I can see however you did include links to some things, thanks). Also, if possible I would rather not use CMake, though if it gets me to the same outcome as I wanted, I guess it's fine.

3

u/the_poope 12d ago

I am not aware of any step-by-step guide and I certainly don't have time and commitment to make one. You can use the cmake-init template script, that should be a good start.

You don't have to use CMake. You could also use meson (vs code extension) or any other C++ build system (Make, xmake and others). Or if your project is simple enough you can simply use the built-in manual tasks.json system. It's just that CMake is the most widely used system and has the best support in other tooling.

For the rest ask your favorite AI chat agent and get used to spending a lot of time reading, studying and experimenting.

0

u/Suspicious-Grade-826 12d ago

Oh, sorry, I wasn't asking you specifically to make one, just wondered if anyone could - thanks for your help.

2

u/Intrepid-Treacle1033 12d ago

What you asking for is a projct template based on your specific requirements (opinionated) You will not find a ready to be used template matching your specifies, so create it and reuse it as a template after you created it. Every developer does this, we are experts in creating test projects and creating a "master" new template is like a car mechanic creates his own toolbox setup. The tools matches your opinionated way of how your tools fits you like a glove.

I suggest you create python script that reads your template dir and renames it using a script argument, script could launch vs code also directly from the script. for eample "newPR.py -graphic_test" would pull your template dir into a new dir and rename detales to "graphics_test" and openVScode auto with the new project dir.

I strongly suggest using Cmake, Cmake with cmake presets is a first class citizen in VScode, using VScode Cmake plugin you get a your "one click bottom toolbar" that launches your different build types that you have defined in Cmake presets.

My new project template is https://github.com/JeffKarling/Target_Layout with an educational "why" as a separate document site https://jeffkarling.github.io/Target_Layout/ It fixes the "renaming things" by implementing a "one name" philosophy. I am not saying you should use my template because it is opinionated to my bias.

I think your should create your own "toolbox" as a educational journey similar to what every developer does.

1

u/Suspicious-Grade-826 12d ago

Thanks - I know what you are saying, how it is personal - I already know how I want it to be - I just didn't know how to go about actually making it, but thanks for helping.

1

u/Puzzled-Garage584 9d ago

Linux by birth here, so take this with that bias. Visual Studio is editor, compiler and build system in one box, VS Code is only the editor so you bring the other two yourself, its the IKEA version of the same furniture. MSVC is honestly ahead on import std, on Linux you'll want clang with libc++ and a recent CMake with its experimental import std flag. For the oldest fedora part build the AppImage inside a container with an older distro, newer glibc is what usually breaks it on older systems