defining preprocessor definitions on a single file - c++

I'm in the process of migrating a project to C++17. There are a couple instances of old code with deprecation warning not within my control. An example of one of these warning is, _SILENCE_CXX17_SHARED_PTR_UNIQUE_DEPRECATION_WARNING. In visual studio, I've been adding these to properties->Preprocessor Def. So far this is working fine.
To prevent the possibilities of me or someone else creating new deprecated code in the future somewhere else in the project, is there a way to minimize the scope of the preprocess definition down to a single file instead of an entire project?

is there a way to minimize the scope of the preprocess definition down to a single file?
Yes, it is possible to have different preprocessor macros for a single file in Visual Studio.
In the Solution Explorer, RMB -> Properties on a specific file to change its properties.

Related

How to add a custom preprocessor for .cpp files to Visual C++ / MSBuild projects

I would like to add a custom preprocessor for .cpp files in my Visual C++ MSBuild project.
Currently I am using a custom buildstep to find all .cpp files and rewrite them (destroying the original content) before they a being compiled. This is no longer acceptable as the preprocessor should make changes "invisible" to the developer (converting lines with a scripting language to real C++ code).
The preprocessing should be done "On the fly". Dependencies should not be affected at all. The custom preprocessor is able to create #line preprocessor directive so compiler errors can be redirected to the original file.
It seems to me that build events and custom build steps are no viable way to do this so.
I tried to replace to compiler binary with my own which calls the custom preprocessor and feeds the output into the original compiler, but this was more of a "proof of concept" and is surely not a production-ready solution.
Q1: Where do I have to look for what I am trying to achieve?
Q2: and is there a possibility to do this in a solution without changing the Visual C++ installation (The custom preprocessor is also part of the solution and compiled before any of the other projects)

Visual Studio warning about copies of files with different contents

When I go to debug my C++ project in Visual Studio, up pops a little warning dialogue box that tells me:
A copy of datum.h was found in
c:/users/brad/desktop/source/binary/datum.h, but the current
source code is different from the version built into
c:/users/brad/desktop/source/binary/datum.h.
I'm having trouble understanding what this is even trying to tell me, let alone how to fix it. At first I thought it might be complaining that I'd accidentally duplicated a file in the directory, which I checked, and found nothing of the sort, which leaves me pretty stumped. I also tried excluding the file from the solution and adding it again, which didn't fix the problem either.
The warning doesn't appear to actually hinder the development of my project, but I suppose warnings exist for a reason, so if anyone knows what's gone wrong, any advice would be greatly appreciated. To my knowledge, I didn't change anything to cause the message to appear, it just popped up one time I went to debug the solution and has kept on appearing ever since.
Also, more copies of the same warning have started popping up, pertaining to other header files in my solution (I haven't recieved any about .cpp files yet, but it could be a coincidence, because it's only been going on for about 20 minutes).
Try removing breakpoints from the file in question.
This worked for me when it occurred with Visual Studio 2013 for a header file in debug build.
Source: Release mode file sync issue - current source code different from the version built
Additional notes: Clean / Rebuild also works, but that is painful for regularly changing code. Enabling the break point after starting debugger merely delays the message.
I solved it:
Close the window of the .h file in Visual Studio if it's open.
Close Visual Studio.
CUT the .h file from its normal location and paste it into a temporary folder that VS doesn't know about.
Restart VS and compile. It'll complain about the missing .h file. Good -- Make the bastard beg for it!
Paste the .h file back into its original location.
Compile. VS will gratefully accept the missing file. (Damn I hate Microsoft!)
This occurs if you rename an implementation file (*.c, *.cpp, etc.) to a header file.
This is because the Item Type still remains as C/C++ Source File, making it get compiled as a separate translation unit rather than as an actual header, preventing Visual Studio from recognizing its inclusion as a header elsewhere.
It took me quite a while to figure this out.
To fix this:
Right-click your header file in Solution Explorer and select Properties.
Select All Configurations, All Platforms.
Under General, change Item Type to C/C++ Header.
Press OK.
Force-recompile any file that #includes your header (or just Rebuild the solution).
The problem is that the debugger thinks that the checksum of the source file is different from what the compiler calculated and put in there. The debugger will then refuse to apply breakpoints in the files that mis-match, to prevent you from seeing data it can't guarantee is correct.
I have had this keep happening even after a clean rebuild. This is with VS 2015. My guess is perhaps the debugger and the compiler disagree on how to hash newlines or something like that? The fix is to turn off "require source files to exactly match the original version" in Debug -> Options -> Debugging -> General
Could you by any chance be debugging another executable (not the one actually built?). This is a common issue in scenarios where Visual Studio builds the binaries in one directory but then they are copied over to some other directory for debugging. I'd suggest you compare the target path under the debugging settings and the output directory under the general settings in Visual Studio.
This would explain the issue, since you are actually debugging some older version of the binary (not the one built currently) and thus the warning, since Visual Studio can't find the version of the source files for that version of the binary.
The reason may be circular header dependencies. datum.h may includes another_header.h (directly or indirectly), which includes datum.h.
I see the real reason of this question is not answered. So for someone still looking, here it goes...
The most common reason of this problem is that the source files used to build the existing obj files are different than the existing ones. In other words the
particular project did not build after new modifications to source. The solution to this problem is to rebuild the project after modifying.
This happened to me in situation where I had modified my static library projects files and then without building that project I started my application project which was using this static library project.
this worked for me:
close VS
delete *.vcxproj.filters file
restart VS
problem should be gone.
this worked for me:
clean project
debug/delete all breakpoints :)
This worked for me (as of March 2019):
Click the 'Build' drop-down menu in the top left of your Visual Studio window
Select 'Rebuild Solution'
I've changed the file name and it works now.
Just encountered this. In my case, one of my .h files contained implementation (a class with static methods), which was #included by one of my .cpp files, but the Project Settings were also telling Visual Studio to compile the .h file.
I manually edited both the .vcxproj and .vcxproj.filters project files, moving the .h file from the <ClCompile> ItemGroup to the <ClInclude> ItemGroup.
This did the trick for me; I never saw the "A copy of...is different from..." pop-up again.(Note that this was after I had thoroughly failed in attempts to get <DependentUpon> to work.)
My solutiion:
Build -> Configuration manager
Switch to another configuration (any, example Releas or Debug)
Switch to previous configuration
It is possible to have multiple projects, each with their own entry point within a solution. Make sure that the correct project is being run.
The source code is different message can appear in a project A's source when you are running project B. In this case, the message can mean This breakpoint won't be hit because you're running a project that doesn't include it

Visual Studio "hidden" preprocessor definition

In a project I'm working on, I have two preprocessor definitions (CPU_RENDER and GPU_RENDER) that control whether the build uses the CPU to render a scene or the GPU. I've been flipping back and forth between these two as I develop the software.
Suddenly, however, a few of the files claim GPU_RENDER is defined despite the fact that I removed it from Visual Studio's preprocessor definitions. Others work correctly. This isn't just visual, as the CPU build fails on link now, trying to find the source code for certain GPU only functions.
I've performed a search of the entire solution for "#define GPU_RENDER", just in case I did something stupid, but that came up empty. I then searched for every time I reference GPU_RENDER, but all those were just ifdefs.
I haven't used any new libraries so I don't think this could be a name collision.
Apparently the offending files had acquired their own set of preprocessor definitions. (I must have hit "Properties" with a file selected instead of the project at some point, making the file have a local set of definitions)
Deleting the file's personal definitions and ticking the "inherit from parent or project defaults" box solves the problem.

My Visual C++ compiler compiles out of date source

I'm a beginner starting to use Microsoft Visual C++ Express 2010 for Windows Programming. I've created a new C++ application using native code, not managed or MFC. I had Visual Studio create for me the basic windows code to create one window with simple menus (chat.cpp). I modified this file and I was able to compile correctly and see my changes take effect.
Now I've added one more source file to the solution (intro.cpp) which I include in my header and call from within chat.cpp. This seems to work just fine, but with one problem. The compiler doesn't seem to be applying my code changes occasionally: I make various edits, recompile, and the "old" code seems to be running. Then, after a while, after I make only a tiny modification, the compiler seems to "catch up" and it runs the new code including all the previous changes I had made.
Is there some kind of cache? Do I need to tell the compiler which files to compile and which ones to just link from object files? Did I make a mistake when I added the file to the solution?
Does intro.cpp have a header file? is that in your solution too?
It's hard for me to imagine that this area of such a mature IDE has a bug here, so I would examine your file list first. Make sure that the Solution Explorer shows all the files you have added and are editing. This is the list that VS uses to determine rebuild is needed.
EDIT: I admit it's not clear to me from your description why it would fail right now. However, typically header files do not include code, it works the other way around (except for class template header files, such as the STL headers). There is some discussion about pros and cons here. The most compelling argument to me in favour of code including headers rather than vice versa is that the header file contains the interface, while the code file contains the implementation.
I would try restructuring your code to a more traditional structure where intro.cpp includes intro.h and any others it needs, and the same with chat.cpp. Then your compilation units are intro.cpp and chat.cpp, and they depend on the associated header files, so provided they are properly listed in the SOlution Explorer, all should work. You can clean up the build dependencies to avoid dups and reduce build time once you have it working as you wish.

Intellisense with single source file in multiple projects

In a single solution, if I have the same source file present in multiple projects (with different project settings, e.g. different #defines), Intellisense and all the features depending on it seems to arbitrary pick one of the project settings when I edit the source file.
How do I get Intellisense to switch which project settings it uses for that file ?
If that is not possible, it would be at least useful to know which project Intellisense picks ?
I have exactly the same problem using VS2005. I read a while back that Microsoft found this to be 'intended functionality' and would not sort this out.
It seems that intellisense uses the first loaded project of the solution to apply the #defines for.