Changing Visual Studio default path for .cpp, .h file - c++

I would like Visual Studio to automatically put my .h file in a folder /ProjectPath/include and my src file in /ProjectPath/src. That way, if I use the "Create class wizard" for instance, it would put the good path by default without me having to change the folder. Anyone know what setting I should change to get this behaviour when I add file ?
Thanks,
Jean-Philippe

You can right click on a folder in solution explorer and go to properties, you need to set the Filter property.
For example the Source Files folder by default has a filter like this in a C++ project: cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx

Related

How can I tell Visual Studio to use the project directory instead of the current file directory when including files?

I am refactoring my Visual-Studio-C++ project to use a folder structure rather than the filters in Visual Studio but I have noticed that when I try to include a file, it will use the current directory of that file so my includes look something like...
#include "../../server/IGameServer.h"
This is quite problematic when I refactor and move things around as I then have to go into each file and change their includes...
Is there a way to make every file start from the base directory no matter where the current file is, or a way to include the folder path so I can just use
#include "IGameServer.h"
(1) Go to project properties:
And add folders to "Addiotional Include Directories". ...Make sure that you aply to "All Configurations" (Debug, Release, etc.) and "All Platforms" (X86, X64).
--
(2) Click on that line + one more click on right side, on the down-arrow) & click Edit.... It opens a dialog that looks like that (I already added some folders in this example):

Why is '.editorconfig' not doing anything in Visual Studio 2017?

I have the following version of Visual Studio:
Microsoft Visual Studio Community 2017 Version 15.1 (26403.7) Release
VisualStudio.15.Release/15.1.0+26403.7
I created a new project and added a .editorconfig file at my solution base folder.
Its content is the following:
root = true
[*.cs]
indent_style = space:warning
indent_size = 12:warning
# C# and Visual Basic code style settings:
[{*.cs,*.vb}]
dotnet_style_qualification_for_field = false:warning
The file location should be right:
.editorconfig <-- Here it is
ApplicationInsights.config
App_Data
App_Start
bin
Content
Controllers
favicon.ico
fonts
Global.asax
Global.asax.cs
Models
obj
packages.config
Properties
Scripts
Startup.cs
Views
Web.config
Web.config.backup.1
Web.Debug.config
Web.Release.config
WebApplication8.csproj
WebApplication8.csproj.user
But whenever I edit a .cs file, nothing special happen (despite indentation not following the rule), I expect a warning (after a build, for instance) to show up, but no.
Is there something wrong with my configuration, or is there something which could hinder the configuration from being applied?
I had this same problem. The solution was adding the .editorconfig file to the solution in my case. You can choose to either add it to the solution or the project, depending on whether you want it applied everywhere or just the individual project.
Solution Explorer → right-click your solution or project → Add → Existing Item... (if you have the .editorconfig file in position at the root of the solution or project) or New Item... (then search for editorconfig in the Add New Item wizard and choose the appropriate type for your work).
I solved a similar issue by placing the .editorconfig file at the root folder of the project, i.e., the same folder as your .sln file.
Try to put the .editorconfig file into the same folder as your source file. If it helps, try to move it up (into one of the parent directories) until you get it high enough to affect all the files you need.
If you have any EditorConfig extensions installed, try to uninstall them. There were some related bugs reported.
I had a similar problem and this is how I solved it:
My solution folder had a parent folder named between square brackets [], like [ParentFolderName], so I removed the square brackets and everything worked as expected.
For a reason or another, the editorconfig file does not do anything when the solution folder has a parent folder named between square brackets. The IDE (in my case Visual Studio 2017) uses its settings.

Cant open html resource file using ifstream::open - Visual Studio 2013

I am working on a project which requires me to open an HTML file and use its contents. I added it to Resource files but when I try to open it lie this:
std::ifstream templateFile;
templateFile.open("filename.html", std::ifstream::in);
The operation fails. I checked it by using templateFile.fail().
The above operation works when I provide the full path. The file lies in the project folder along with other files. I tried setting build action to content but still it doesnt work. Please Help.
Output directory, where your executable is compiled and put into differs from the source directory, where you create all your .cpp/.hpp files (I assume there is filename.html file). Local path filename.html is supposed to be local for your executable file, not the source file.
Read more about changing the output directory here: https://msdn.microsoft.com/en-us/library/ms165410.aspx
Under Configuration Properties / Debugging, see what your Working Directory is using the macros dialog box. Move your file into this folder.
Click the button shown in the figure. There, click either Edit or Browse. Browse will take you to the working directory. Edit will expose the link to open the macros box

"Error C1083: Cannot open source file" Shouldn't Be Looking For The File At All

I was trying to #include a cpp file with some functions so I can use that cpp file later with other projects. It gave me an 'already defined in .obj' error and since then that .cpp file was like binded with my project. (I understood that's not the way, the answer here helped me with the already defined)
If I exclude the .cpp file from the project, remove it from the directory and remove the #include line it still looks for it:
c1xx : fatal error C1083: Cannot open source file: 'std.cpp': No such file or directory
Diagnostic:
Outputs for D:\MY DOCUMENTS\C#\PROJECT\D3DTESTC++\COWS AND BULLS\CBMAIN.CPP|D:\MY DOCUMENTS\C#\PROJECT\D3DTESTC++\COWS AND BULLS\STD.CPP: (TaskId:15)
It shouldn't be looking for the std.cpp at all, I removed it! So is there a way I can reset the project and recompile so that the program doesn't look for it? I already tried Rebuild and Clear -> Build Project
When I ran across a similar problem with VS Express, I wound up having to open up the the .vcxproj file (which is just XML), and remove the offending
< ClInclude Include="FILEPATHANDNAME" > tags.
Many of the solutions here will not work
Fullproof method:
Open the vxproj file that is giving you trouble in a text editor.
remove all references to the file it cannot find.
OK, I have no idea how I did it but I'm still going to try to write what I did.
Save all and Close solution
Open the .vcxproj file (not .sln)
Build -> Clean [Project Name]
Save all and Close
Open the .sln file again.
Build -> Project Only -> Clean Only [Project Name]
Build -> Project Only -> Build Only [Project Name]
That's exactly what I did and worked for me. I think the main thing to do is clean, save, close, open, build, but I'm not sure.
In Solution Explorer you can select/deselect option "Show All Files".
Try both options and make sure excluded file is not included in project for both of them.
That's what I had:
I used "Show All Files" option (so you can see all the files in project directories). I excluded one of my .cpp files from project. However, it behaved as this file is in project.
That's how I managed to fix it:
I switched "Show All Files" off and saw this file still belongs to project! So I excluded this file once again.
As I see, that's a known issue.
This worked for me, hope it will be useful for someone else.
Try to verbose builder output to see exact steps of what's going on. I suppose, you use Visual Studio, right?
Go to menu "Tools -> Options"
In options dialog, select "Projects and Solutions -> Build and Run"
Change current mode of "MSBuild project build output verbosity" from "Minimal" to something like "Diagnostics" or "Detailed".
Rebuild your project and investigate Output windows
Builder dump should shed more light on your current settings (I suspect you have more references to that file than you expect)
This happened to me because I renamed folder from inside the IDE. None of the above solutions worked. The only way to fix this is by opening vcproj in notepad and you should see the offending files in the <ItemGroup>. Just delete those lines.
Or sometimes, like in my case, the issue is simply in the naming of the folders in the location. I had a very long path with folders that I like to name with special characters so they show up at the top and it's easy to access them.
As soon as I put my solution in a folder just in D: drive, the issue was gone.
When I renamed a file, I found I had to go to SolutionExplorer, Source File, select the file, first exclude from Project, then re-add it to project, and rebuild the solution it lives in. It was still showing up as the old file name under Source Files for me.
I had the same problem, but I had another .sln worked fine. After tooling around with the Project->Properties-> to make them look identical, nothing worked. I opened both .vcxproj files and copied the contents of the working version into my non-working version. (I noticed that the two files had different lengths. The non-working version was longer by about 20 lines.) I just changed the RootNameSpace to the non-working version's name. I saved the non-working file and presto! It worked.
I removed those sources from Project and re-added them. Somehow, references were messed up after a hurry project refactoring.
For people having problem related to "error C1083: Cannot open source file":
Error is caused by settings in *.vcxproj file. Probably you deleted/moved source file by file explorer, not by Visual Studio's "Solution Explorer". Thus, your *.vcxproj file is corrupted. Fix is to manually correct settings in *.vcxproj file.
How Visual Studio settings files work
Visual Studio saves solution's info into file. This file is usually in project's solution directory, has extension .sln and base name is same as name of solution, f.ex.:
NameOfSolution.sln
Similarly, project's info is saved into one file (each project has its own file). Base name of this file is name of project, extension is .vcxproj, and usually is located in subdirectory named as your project, f.ex.:
NameOf1stProject/NameOf1stProject.vcxproj
NameOf2ndProject/NameOf2ndProject.vcxproj
Both *.sln and *.vcxproj files are textual files. You can open them by using Notepad.
How to fix problem
Find *.vcxproj file responsible for your project.
If you don't know where it is, open in Notepad the *.sln file of your solution. Search for name of your solution. You will find line like:
Project("{9AA9CEB8-8B4A-11D0-8D22-00B0C01AA943}") = "NameOf1stProject", "NameOf1stProject\NameOf1stProject.vcxproj", "{A8735D0A-25ED-4285-AB8F-AF578D8DB960}"
Value under "NameOf1stProject\NameOf1stProject.vcxproj" is location of *.vcxproj file of your project.
Open found *.vcxproj file by text editor (f.ex. Notepad).
Search for line on which is filename you are struggling with.
Example: if you are looking for "RemovedFile.cpp", then you should find line:
<ClCompile Include="RemovedFile.cpp" />
Delete that line.
If you have opened Visual Studio, it asks you if it should refresh solution - select yes. If it is not opened - just start using it.
In case of any problems, try to rebuild solution (top banner -> Build -> Rebuild Solution)
In my cases, it worked. 30 mins of trying to fix, <1 minute of fixing.
This helped in my case. To sum it up, my path to the project was too long, so I moved my project to something shorter i.e. D:\my_project and everything worked in a blink of an eye.
I had this same problem, but for me the issues was that I was using Bash on Windows (WSL) to clone the repository and then using VS to compile.
Once I deleted my clone and used Windows command line (cmd.exe) to clone the repo then the error 1083 went away.
This is caused by not removing/deleting the file properly. Go to Solution Explorer, select your solution, at the left corner, activate the icon: show all files.
(if you already removed the problem file, restore it from recycle bin)
Select the problem file, do remove and delete from within Solution Explorer and you should not have this problem. And remember to do it the proper way from now on.
This is on MS 2010
If you have that file in your project directory but you still got the error, on your IDE go to Solution explorer--> Remove that file-->then open the project directory on your file explorer-->Select that file and drop it on a specific location in IDE solution explorer. I fixed it this way. I use the Windows platform.
I got this error when I got a code from my peer and I tried directly running it on my system. Ideally to avoid such errors, I should have just copied the source and header files and should have created the VS solution of my own.
To resolve the errors I removed the files from the Solution Explorer and added them again. Following image shows the Solution Explorer window.
The remove option comes after right clicking on the file names.

VS2010 - Add template to New Project window

I am trying to add a new project template for an often used pattern. Starting from the class library template I have done the following (it still does not show up in the new project window):
opened the .vstemplate file
changed name and description to 'hard coded' values (my template). The values in there pulled from the csharpui.dll resources.
changed the TemplateID, DefaultName, and ProjectItems included.
saved these to the ProjectemplatesCache folder and as a zip in the ProjectTemplates folder.
restarted VS2010 and checked the new project location which should have shown my new template.
specifically, the folders I saved to were..
C:\program files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows\1033\HostComm.zip (the zip is the folder name, not a zip file)
and
C:\program files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Windows\1033 (this folder has a HostComm.zip file in it)
Has anyone else done this? Can it be done? If it can then what did I miss?
Doing this by hand is pretty courageous. Having a folder name with an extension .zip sounds wrong, I don't see a similar one in the existing templates. Watch out for the shell's habit of turning .zip archives into folders.
Best thing to do is to use the documented procedure to create a project template and verify what effects it has so you can repro them accurately yourself. Create a new project, using one of the existing templates as a starter. You might as well make it look as close as possible to the ultimate template you want to end up with, add project items as desired.
Then use File + Export Template. Next. Fill in the text boxes, note the Output location. Finish. Have a look-see at the generated .zip file.