Force VSProps settings to override project settings - c++

I have a vsprops file that defines the optimizations all of our projects should be built with for Visual Studio 2008. If I set the properties for the project to "inherit from parent of project defaults" it works, and fills them in the vcproj file. However, this doesn't protect me from a developer checking in a project file that changes the optimizations. In this case, the project settings are used over the vsprops settings. I need to make it so that vsprops always takes precedence over what is in the vcproj file. Is this possible? Other workarounds are also welcome.

Hmya, that's just not how Visual Studio was designed. It is quite free from "boss override" switches, it gives its user unfettered access to configuration settings. Which ought to make sense to you, a developer can do far more damage with his code than with tinkering settings. If a dev intentionally changes an optimization setting then, surely, it is because he profiled the code and determined a better setting.
Anyhoo, Visual Studio isn't just useful to the dev, it is also useful to you. Write a little utility that parses the .vcproj file and checks if the optimization settings were overridden. It is a simple .xml file, you'll need about 5 lines of code. Run it in a pre-build event on your build grunt and fail the build if you deem it inappropriate. Dealing with the pissed-off developer cannot be automated however.

The only advice I can give is, if you open up the Property Manager for the project, and Right Click on one of the Configuration folders, it should popup a menu with an item called "Overidden Properties..." which in turn opens a dialog box that lists each property set in the project configuration that overrides a property sheet property sheet - along with the facility to select some or all, and delete the overrides.

Related

Visual Studio: how to control specific configuration parameters on student PCs?

Can anyone tell me how to control default configuration parameters in VS 2017 C++? The scenario is that we generate one sample PC with the software we want (Win10, VS2017, et cetera), and this image is cloned onto about 100 PCs in student CS labs.
The two specific things I would like to control are (1) use of precompiled headers and (2) message C4706 (= versus == in an if condition). There are some others, but these are at top of the list.
I know students could fix this themselves, but so many are confused in the first few weeks of an introductory programming class that I don't want to try that.
A search of MSDN has not been productive, but I probably am using the wrong search terms.
Thank you in advance!
You could also edit the default platform property pages found at \\AppData\Local\Microsoft\MSBuild\v4.0, Microsoft.Cpp.Win32.user.props and Microsoft.Cpp.x64.user.props
I'm not sure how you would automatically distribute those files but so long as you are using MSBuild as the build system anything in those files is picked up prior to project-specific settings.
There are two ways I can think of:
Change the default settings in property pages for each configuration
Create a custom project template with all the project settings necessary. This will only affect new projects created with this template. Also it will keep the default template intact and will probably allow students to experiment a bit more.
For enabling Precompiled header.
Right click your project->Properties->( a property pages window appears)Configuration Properties-> C/C++ -> Precompiled Headers -> (on the right side) Select Use(/Yu) by clicking Precompiled Header.
And for suppressing C4706, set compiler warning to level less than 4
Right click your project->Properties->( a property pages window appears)Configuration Properties-> C/C++ -> General ->Warning Level to less than 4
To retain the settings and share with others....
I do not have proper VS2017.... Can you try as said below.....
First export the existing project settings using
project(visual studio menu)->Export template.... continue and and save the template and make a note of the path....
usually by default the templates is saved in below path as a zip file.
C:\Users\<>\Documents\Visual Studio 2017\My Exported Templates
Now copy the zip file and and put it in
C:\Users\<>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C++ Project
Check if your template is automatically picked up.
If not open visual studio command prompt tool and run
devenv /installvstemplates.

How to automate changing the project settings in Visual Studio 2005 and later

i have a visual studio 2005/2010 projects and each of these project is made up of 20 projects and all these projects are managed by a single solution file(.sln)
now if i had to change any compiler or linker settings, i have to manually do it for all the 20 projects, is there anyway this can be automated using a script or Addin or any other method.
appreciate your time and attention
What you are looking for are property sheets. Visual Studio already has a mechanism that almost enables you to do everything you are asking for.
A property sheet (*.props) basically is a separate settings file that you can attach to as much projects that you like, and all the configured properties are applied to these projects. Think of it as if the projects are "inheriting" from another configuration - and that's exactly what's happening.
Let's say you have a base configuration of include directories, linker directories, library includes, preprocessor defines and so forth, and you know that each project uses them in each configuration (Debug, Release, ...). You can create one property sheet defaults.props and apply it to all your 20 projects. Then, if you require a change, just edit the property sheet file, and all the changes will be reflected to all 20 projects.
To create property sheets, click on View -> Property Manager (not to be confused with the "Property Window"!). A window should pop up where you can see all your projects with all their configurations. Now right click on any of your projects and select Add New Property Sheet, type in a name and save it somewhere. This will create a .props file and adds it to all the configurations of your single project. You can then edit this property sheet from this window like it was a real project (right click on the property sheet and select Properties).
After you have created your property sheet you can go ahead and right click on the other 19 projects and do a Add Existing Property Sheet. You have to do this once for all your projects, sure, but after that your property sheet enables you to change the settings for all projects at once.
Also noteworthy is that you can have multiple property sheets applied to one project. If you do so, they are being applied in order (you can reorder them if you like) so you are able to configure your projects in more detail. For instance, you could have this default.props that applies to everything, and then you could have a default_debug.props that is only intended for Debug configurations. The first one sets up all your include and library directories, the second one does library specifications and defines specific for Debug configs.
One last important note: If you have set some properties directly in your project (which you obviously have), it will automatically override any settings you have specified in your property sheet. You can see this if a setting in your project has a bold value. If you want to revert it to the property sheet value, clear the setting and select the <inherit from parent or project defaults> from the drop down menu of the setting. Include- and Linker-Directories are a special case, in which you will ALWAYS inherit everything specified in the property sheet unless explicitly disabled.
EDIT: I am not quite sure about VS2005/VS2008, and iirc they have the same principle with a *.vsprops file, but this may be incompatible with the *.props files from VS2010+

Visual Studio 2010 default property sheets seem linked under both debug and release configurations, what's up?

I'm trying to alter the default property sheet on a new install of Visual Studio 2010 for C++ projects. Just trying to add a few directories (for Boost etc.) and optimisation settings for release builds, basic stuff like that. Obviously there are some settings that I want to be different for different configurations. Problem is that whatever I try from the property manager window of my project, everything I do seems to affect both configurations.
I tried multi-selecting both -> right click -> properties initially, to set up common settings, and that did exactly the same thing as selecting Microsoft.Cpp.Win32.User under either debug or release. So everything affects both configs.
Now, as I understand it, Microsoft.Cpp.Win32... is a single file (at least, a single file for x86 configs, single file for x64 etc.) so the fact that I can't change the configs individually actually makes some degree of sense. But if I can't do it that way, how can I do it?
Appreciate any advice!
Short answer: you can't. See comment discussion under question.

Import Existing C++ Source Code into Visual Studio

I am trying to import an existing c++ application's source into visual studio to take advantage of some specific MS tools. However, after searching online and playing with visual studio, I cannot seem to find an easy way to import existing c++ source code into visual studio and keep it structurally intact.
The import capacity I did find flattens out the directories and puts them all into one project. Am I missing something?
(This is all unmanaged C++, and contains specific builds for win/unix)
With no project/solution loaded, in Visual Studio 2005 I see this menu item:
File > New Project From Existing Code...
After following the wizard, my problem is solved!
Switching the "Show All Files" button shows the complete hierarchy with all directories and files within.
If the New Project From Existing Code... option isn't available, you'll need to add it in Tools > Customize...
I am not aware of any general solution under the constraints given - specifically having to create many projects from a source tree.
The best option I see is actually creating the project files by some script.
Creating a single project manually (create empty project, then add the files),
Configure it as close as possible as desired (i.e. with precompiled headers, build configurations, etc.)
Use the .vcproj created as skeleton for the project files to be created
A very simple method would file list, project name etc. with "strange tokens", and fill them in with your generator. If you want to be the good guy, you can of course use some XML handling library.
Our experience: We actually don't store the .vcproj and .sln in the repository (git) anymore, but a python script that re-genrates them from the source tree, together with VS 2008 "property sheet templates" (or whatever they are called). This helps a lot making general adjustments.
The project generation script contains information about all the projects specialties (e.g. do they use MFC/ATL, will it create DLL or an EXE, files to exclude).
In addition, this script also contains dependencies, which feeds the actual build script.
This works quite well, the problems are minor: python requried in build systems, not forgetting to re-gen the project files, me having to learn some python to make adjustments to some projects.
#Michael Burr "How complex are the python scripts and whatever supporting 'templates' you might need?"
I honestly can't tell, since I gave the task to another dev (who picked python). The original task was to provide a build script, as the VS2008 solution build was not good enough for our needs, and the old batch file didn't support parallelization. .vcproj generation was added later. As I understand his script generates the .vcproj and .sln files from scratch, but pulls in all the settings from separate property sheets.
Pros:
Adding new configurations on the fly. Some of the projects already had six configurations, and planning for unicode support meant considering doubling them for a while. Some awkward tools still build as MBCS, so some libs do have 8 configs now. Configuring that from hand is a pain, now it just doesn't bother me anymore.
Global changes, e.g. moving around relative project paths, the folder for temp files and for final binaries until we found a solution we were happy with
Build Stability. Merging VC6 project files was a notable source of errors for various reasons, and VC9 project files didn't look better. Now things seem isolated better: compile/link settings in the property sheets, file handling in the script. Also, the script mostly lists variations from our default, ending up easier to read than a project file.
Generally: I don't see a big benefit when your projects are already set up, they are rather stable, and you don't have real issues. However, when moving into the unknown (for us: mostly VC6 -> VC9 and Unicode builds), the flexibility reduced the risk of experiments greatly.
Create a new empty solution and add your source code to it.
For example,
File>New>Project...
Visual C++>Win32>Win32 Console Application
Application Settings>
- Uncheck "Precompiled Header"
- Check "Empty Project"
Project is then created. To add existing code:
Project>Add Existing Item...>
- Select file(s) to add
Recompile, done!
In the "Solution Explorer" you can click on the "Show All Files" button to have Visual Studio display the files as they exist on the file system (directories and all).
In my opinion this is an imperfect workaround, but I believe it's the best available. I'm unaware of a plug-in, macro or other tool that'll import a directory into an actual project with folders that mirror the file system's.
I know this question is already marked correct, but I was able to import existing code into a project with Visual Studio 2008 by doing "File" -> "New Project from existing code". The directory structure of my code was retained.
You can always switch view from project menu
For eg. Project->Show All Files
The above will display the files in unformated raw file system order
Not sure of older versions but it works on VS 2010
I understand you, I have the same problem: many .cpp and .h files organized in many folders and subfolders with include paths written for this folder structure. The only way you can do to import this folder structure together with the source files is to use "Show All Files" and then right-click on folders and select "Import in Project". This works for me when I am using C-Sharp projects. But it does not work for my C++ Projects. I am still searching for a solution...

visual C++ express 2010 and setting env variables solution wide

I'm C++ dev migrating to visual 2010 c++ from vim/g++. Here blog I've read that VC++ directories are no more and that I should use property pages in vs 2010 but I don't know how... Here is what I need to do. I have w solution (50 projects strong) and all of them use boost, pthreads, xercesc and few other libs. I have env variables that point to those libs on my hard drive. How can I tell VS to use them as additional include paths? Again, it's 2010 version so no vs per solution setup available. I do not want to set it manually in every project.
The answer to your question is also in the blog that you linked to, but it's mentined in a kind of round about way:
If you open up the Property Manager view to see the property sheets associated with your project, you’ll see that one of the property sheets is named Microsoft.Cpp.Win32.User. This property sheet is actually stored in LocalAppData, just as VCComponents.dat file was, in the directory %LocalAppData%\Microsoft\VisualStudio\10.0. Using the property editor on the property sheet (just right-click on this property sheet node and select Properties…), you can see that you are able to make edits directly to this file. Since all projects, by default, import this property sheet, you are effectively editing the VC++ directories in the same way you were able to do before.
The key is that you get to the VC++ Directories property through the "Property Manager" windows (open it via the View/"Property Manager" menu selection). The VC++ Directories setting is in the "Microsoft.Cpp.Win32.user" property sheet - that edits the global setting, so you should only have to do it once.
There seem to be quite a few people who dislike this change; I think that's because it's less discoverable and obvious than how the setting was managed before. The trade-off is that it's more flexible and integrates into the MSBuild architecture better, and once you do know about it it's just as easy to change as before (it's just harder to find, particularly if you're used to the old place).
View->Property Manager
Select all projects
Add one, new, common property page for all projects
"Microsoft.Cpp.Win32.user" - (as name say) better use for some user specific settings (or better do not use at all)
Mariusz