Swapping header/source in Visual Studio opens wrong file

Started by
9 comments, last by L. Spiro 8 years, 11 months ago

Hello,

I was not sure where to ask this question:

In my project, I have a Control.hpp and Control.cpp files.

I use a shortcut to swap between header and source files very often, however with Control.cpp, Visual Studio 2013 leads me to a different Control.h, which is completely unrelated to my project (C:\Program Files (x86)\Windows Kits\8.1\Include\um\Control.h).

Actually, when a .cpp file in my C++ project matches a .h header located in this Windows Kits thing, Visual prioritizes the wrong file.

This is very annoying, anyone has an idea how to solve this?

Advertisement
I use a shortcut to swap between header and source files very often

Which shortcut? Is it one provided some some plugin?

Sounds like it is prioritizing matches in your project include directories over those in the project's source tree. Fixing that likely depends on who implemented the shortcut.


how to solve this?

Rename your files. It's very common practice for programmers to name their own files and classes with a unique prefix to avoid just the problem you're facing. E.g., you can precede all your project files and classes with Zy. It's unlikely you'll run across ZyControl (either as a filename or class name) elsewhere.

VS also searches for files specified by #include <filename> using the "Include Directories" entries under "VC++ Directories" in the project configuration properties.

As an alternative, revise your shortcut macro to use a full pathname - i.e., prefixed by "c:/yourProjectDir/" or similar.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Are you using #include <> or #include "". The former does a deep search for the file. The latter just searches the project directory.

I use #include "Control.hpp".

The shortcut is not a macro, it's built-in. Its name is "EditorContextMenus.CodeWindow.ToggleHeaderCodeFile".

I don't want to rename all my files just because Visual Studio does that...

Strangely, if I change my header's extension to ".h", it works. But my whole project uses .hpp for C++ headers.

I don't want to rename all my files just because Visual Studio does that...

Oh well. Now you will know to put more focus on naming conventions throughout the entirety of your next project.
For now you have no choice but to either deal with it or rename something in a way inconsistent with the rest of your project.

Or file a complaint with Microsoft® and hope they release a patch.


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

Strangely, if I change my header's extension to ".h", it works. But my whole project uses .hpp for C++ headers.

Find and replace .hpp" with .h"

Go to your directory, and bulk-rename all the filenames. I use 'Bulk Rename Utility'. It's free, but its UI is like getting your brain smashed-in with a gold brick wrapped in a lemon. Once you get over how many widgets it crams in your face, and learn the basics of it, it becomes real easy to use and very convenient to have around.

Probably solved. It's a 15 minute fix, and 14 of those minutes are learning the basics of a useful tool that you'll be glad to know about at other times in the future.

The shortcut is not a macro, it's built-in. Its name is "EditorContextMenus.CodeWindow.ToggleHeaderCodeFile".

Sounds like a bug then. The sane implementation of that feature is to start looking for the counterpart file in the same directory as the current file, and then probably in all other directories under the project root, and only then fall back to project include directories outside of the root itself.
You should not need to rename your files, but if this is a bug, replacing the ".hpp" extension with ".h" is probably the least-invasive option you've got.

I don't remember why as it's been several years, but I had to stop using the .hpp file extension. I remember weird happenings that were fixed by changing to .h. I don't think I ever tracked down the cause.

I use .hpp to differentiate C++ files from C files. The only problem I had was with old compilers a long time ago, but I never had any problems until now.

AFAIK there is no really standard extension naming for C++ files, just wide use of common ones (cpp, cxx, hpp, h, hxx...).

It seems to me the ".h" naming was hardcoded somewhere in Visual or hidden in a config file maybe, because that's the Microsoft's convention. I thought intellisense would be used and see #include "Control.hpp", but it's not the case.

Maybe I could write a macro that handles this better...

This topic is closed to new replies.

Advertisement