Jump to content
  • Advertisement
Polydone

C++ Structuring C++ code (Visual Studio)

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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:)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!