Structuring C++ code (Visual Studio)

Started by
10 comments, last by matt77hias 6 years ago

I'm getting back into C++ after not playing with it for many years.

Being spoiled as a C# programmer I'm finding quite a few things about the Visual Studio environment surprisingly puzzling.
I'm used to just being able to add a class to a folder, and I want to structure my code in folders with matching namespace, but whenever I try to add a class to a folder Visual Studio just puts it in the root. I can of course move it but I can't help thinking - if VS is making it so hard for me to do what I want am I doing something wrong? How do you structure your C++ code?

Developer journal: Multiplayer RPG dev diary

Advertisement

Hi Polydone!

I think the files are supposed to be in the same folder, Visual Studio uses filters to organize the files within the IDE. It looks like they are in folders but they are in the same folder in the file-system. I use filters to organize my files according to namespace.

you could also brake up your code into several projects in the same solution and the files for each project would go into different folders.

i also found this if you want the filters in Visual Studio to look like the folders in the filesystem.

https://marketplace.visualstudio.com/items?itemName=Dllieu.GenerateCProjectFilters

 

Thanks for the link. I'm actually putting the files in folders because I'm choosing "Show all files" but it isn't optimal. I guess I'll just accept the "VS" way and use their filters.

Developer journal: Multiplayer RPG dev diary

I use CMake to generate my VS projects with filter layouts that match my disk folder layouts.

I was quite surprised when I started a C# project and realized that VS would do this automatically there! 

Surely there's some hidden option to turn in on for C++ projects (/ off for C# projects)? 

As Hodgman say, use CMake with 'source_group(folder files)' to organize your files.

A recommendation if you are just starting out your project is to use cmake from the beginning. It's easier than to add your dependencies later.

It is there, but you're right that it is confusing.

In the Solution Explorer window, select the project. A toggle button at the top should be available called "Show All Files". 

When "show all files" is enabled it shows the actual directory structure. When "show all files" is disabled it shows the Filters view.  

Filters can work when you've got a large complex project and the directory structure doesn't match the logical structure. Consider a project that has publicly-exposed portions for modding or third party development and hidden portions kept internal to your organization. That project might have headers in one area of a source tree, proxy or intermediate or other PImpl implementation files in another, and the internal or private use headers and implementation files in a third and fourth location.  There are other cases, but that is probably the most clear within this industry. The filters can put all the different pieces (the public headers, the publicly visible classes, and the internal non-published code) all together in a logical place to make it easier for developers to work with.

When you want to switch between them, hit the toggle button.

On the other hand, if your tool fails to do what you need, you shouldn't adjust yourself to your tool's limitations.  You should get a better tool.

There are many alternatives to Visual Studio available.  Try Microsoft's VS Code for example.

Stephen M. Webb
Professional Free Software Developer

5 hours ago, Hodgman said:

I use CMake to generate my VS projects with filter layouts that match my disk folder layouts.

I was quite surprised when I started a C# project and realized that VS would do this automatically there! 

Surely there's some hidden option to turn in on for C++ projects (/ off for C# projects)? 

I'll give it a try if I decide to move past prototype for C# -> C++ migration thanks :)

It also seems like it would make it a lot easier to move to another IDE?

2 hours ago, Bregma said:

On the other hand, if your tool fails to do what you need, you shouldn't adjust yourself to your tool's limitations.  You should get a better tool.

There are many alternatives to Visual Studio available.  Try Microsoft's VS Code for example.

Yeah you're right - I haven't kept up with alternatives and I probably should.
15 years ago alternatives to VS were all either unstable immature visual IDE's or emacs etc.

But no matter what product I choose there will probably one or more features I dislike:)

Developer journal: Multiplayer RPG dev diary

On 4/12/2018 at 1:14 PM, Polydone said:

Thanks for the link. I'm actually putting the files in folders because I'm choosing "Show all files" but it isn't optimal. I guess I'll just accept the "VS" way and use their filters.

Do not do this.

Your folder structure should strictly always match your IDE tree structure.  It is never acceptable to browse the actual folder tree and just have them all blobbed together in a mess of random files.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Going one step further, do never split header (.h) and implementation (.cpp) files because it is pure bad design to do so especially but not limited to greater projects! I always feel wasting my time to seek for the coresponding .h or .cpp files when investigating projects on GitHub.

A clear folder structure and strong naming will always be your best friend in such things. What you will still to have is setting VS options correctly because even if you structure your files correctly on disk, VS will throw them all together so either work in folder view or (in my case because my project structure has some more sub levels) use filters. But be aware of VS creating files for you, it will throw them into your main directory even if filters indicate something else.

I personally have written my own in-house tool for that (and a lot of other) task(s) but CMake or anything else fill also fit it.

I don't really like VS Code because it seems like a variant of Visual Studio but behaves completely different in some cases but thats my taste ;)

This topic is closed to new replies.

Advertisement