Storing source files outside project file directory in Visual Studio C++ 2009 - visual-studio

Visual Studio projects assumes all files belonging to the project are situated in the same directory as the project file, or one underneath it.
For a particular project (in the non-Visual Studio sense) this is not what I want. I want to store the MSVC-specific files in another folder, because there might be other ways to build the application as well, for example with SCons. Also all the stuff MSVC splurts out clutters the source directory.
Example:
/source
/scons
/msvc <- here is where I want my MSVC-specific stuff
I can add the files, in Explorer, to the source directory manually, and then link them in Visual Studio with the project. It's not the end of the world, but it annoys me a bit that Visual Studio tries to dictate the folder structure of my project.
I was looking through the schemas for the project files but realized that this annoying assumption is in the IDE and not the format of the project files.
Do someone know a neater way to solve this than manually linking files to the project from the source directory?

I use this sometimes, pretty sure it's what you want:
make sure the Show All Files option is on in your solution explorer.
create a symlink that targets your source directory and put the link at the same level as your project, or even lower if you want finer control. The command is mklink /j target source
For the example project structure you show, you'd run mklink /msvc/source /source and in the project the source directory will show up as if it was in the project dir (well, actually it is). Additional bonus: adding new items through VS also automatically puts them in the right directory.

You can add files with links like this, they are searchable, view-able, but they do not checkout if you try to change them, also visual studio leaves the wildcards in place:
<ItemGroup>
<Content Include="..\Database Schema\Views\*.sql">
<Link>Views\*.sql</Link>
</Content>
</ItemGroup>
This goes inside the .proj file.

Related

Reorganizing directory structure for Visual Studio solution

My Visual Studio (VS) project/solution works as intended, but VS has somehow organized the directory structure (source files) of my project in a non-logical way.
The current directory structure
Solution Folder
Project 1 folder
Project 1 source files
Sub folder
Project 2 source files
.sln Solution file
Project 3 folder
Project 3 source files
I guess I have made a mistake at some point when adding the projects to the solution in VS, since it is organized like this now.
What I would ideally like is this more clean structure:
Solution Folder
Project 1 folder
Project 1 source files
Project 2 folder
Project 2 source files
Project 3 folder
Project 3 sources files
.sln Solution file
When I tried this - or whenver I move/try to reorganize the files/folders outside of VS according to the above by simply moving them around, and then try to build the solution in VS afterwards, I get the following error for (pretty much) all items:
The item (name) does not exist in the project directory.
It may have been moved, renamed or deleted.
So my question is:
How do I reorganize my folders/files for my solution outside of VS, without breaking my solution?
For people who might be having similar obstacle, I found the answer to it from this thread:
Visual Studio move project to a different folder
As described in there, you have to delete the project you wish to move from inside your Solution Explorer in Visual Studio (VS). Afterwards, you move the project folder (outside VS) to where-ever you want it. Finally, in VS Solution Explorer, you right click on your Solution --> Add --> Existing project.
Make sure you dont separate any folders or files, that belong together (be very careful/aware of this).
If you were referencing the different projects e.g. through namespaces, you have to add this again simply by right clicking the project (in Solution Explorer) that you want to reference another project --> Add --> Reference.
EDIT: Visual Studio might, for whatever reason, change which project is initializing first when building/debugging etc. If you look in VS Solution Explorer, the project which is initialized first is bold. If you need to change this, right-click on your desired start-up project --> Set as StartUp Project.
All of the above is for Visual Studio 2017

Visual Studio: How to add linked item between folders within the same project?

I'd like to share a common .targets file between different folders within the same project (using Visual Studio 2015):
MyProject
MyNupkgContent1
build
MyNupkgContent1.props
Common.targets
MyNupkgContent2
build
MyNupkgContent2.props
Common.targets <-- should be added as link
When I try to "add as linked" Common.targets to MyNupkgContent2/build, the dialog closes but does not actually add the link. Adding files as links from a different project works fine. Is it possible to link files between folders in the same project?
I'm not certain as to how you should go about linking it but depending on contents and size of application, your best bet may be simply going into your packages and manually copying + pasting them into MyNupkgContent2

How do I extract source code files from Visual Studio pollution?

In a directory where source code resides, there are also Visual Studio related files, like *.user, *.vcxproj. And Debug and Release folders.
I would like to change project directory layout so it looks like this:
For example, the project is named ProjectX.
In the ProjectX main folder, there would be only two subfolders:
ProjectX/Source
ProjectX/Build
In ProjectX/Source there would be all source code files, but nothing else.
In ProjectX/Build there would be all Visual Studio-related files.
How can I do it?
To move the vcxproj into the build directory, move the vcxproj into the build directory.
To move your source files under a source directory, move your source files under a source directory.
After either or both of the above steps, you'll need to re-add the files to Visual Studio, but I'm sure you knew that. Visual Studio isn't psychic and can't guess where your files are if you move everything around.
The SDF file can be moved by asking Stack Overflow how to move the SDF file: How to change ipch path in Visual Studio 2010
Visual Studio 2015 should not be creating .user files any more. Perhaps you're not fully updated?
At this point you have everything you wanted, even though it's weird. Except for portable projects, people aren't generally moving their vcxproj files. The vcxproj file is typically considered the root of the project, just like a makefile would be; under it is build and source.
Now let me give you advice. Don't do all of that. Do this instead:
Move your source files in a source directory. Now in Visual Studio, go to project properties and change Output Directory and Intermediate Directory to $(ProjectDir)Build\$(Platform)\$(Configuration)\
Now you have one Build directory instead of a Debug and Release directory. That means a lot to some people. It means more to people with more build configurations.

Saving Visual studio Project as new name

Basically what I'm looking for is what is visual studio 'Save As'.
I have a program that I would like to use as a base for building a couple of other programs off of. For the life of me I cant find out how to simply make a copy of my program so I have a back up of the original. Any ideas? What am I missing?
If you want a copy, the fastest way of doing this would be to save the project. Then make a copy of the entire thing on the File System. Go back into Visual Studio and open the copy. From there, I would most likely recommend re-naming the project/solution so that you don't have two of the same name, but that is the fastest way to make a copy.
Apparently Visual Studio is missing the 'Save As' feature, so you'll have to back up your projects by manually copying them to a new location.
Here's how I do a project "Save As" for C# projects. The challenge is finding all the places where your project name is and giving it a new project name. You need to rename two folders, rename two files, go into .sln to edit project and path, and go into three files and edit the namespace:
Copy project folder.
Rename project folder.
Go into project folder.
Rename the inside project folder.
Rename .sln file.
Edit inside .sln file and rename project and project path.
Go into inside project folder.
Rename .csproj file.
Edit inside Form1.cs and rename namespace.
Edit inside Form1.Designer.cs and rename namespace.
Edit inside Program.cs and rename namespace.
I use Notepad to go inside the .sln and .cs files. It goes pretty fast.
I also use GIT, but there are definitely good reasons for a new project, like when you want to do major tweaks and a new project is appropriate, but you want to quickly leverage off your old project. Maybe Microsoft will add a "Save As" for projects in the future!
Just found a way of doing it that haven't been mentioned yet, go to :
File > New > Project From Existing Code ...
Choose your language, the folder where the project you want to base yourself on is located and the new name of the project and it's type. Done !

How do I add an existing directory tree to a project in Visual Studio?

The issue is simple really. Instead of creating folders in Visual Studio, I create a directory structure for my project on the file system. How do I include all the folders and files in a project, keeping the structure?
If I "Add Existing File" on a folder named Services and navigate to a file in the directory structure .. Services > AccountManagement > CreateAccount.cs, it appears in Visual Studio like so: Services > CreateAccount.cs. I do not want this.
I have an entire directory structure worked out already, as I am mimicking our client developers using the same structure for organization. How do I add all the folders and files to the project in Visual Studio? Or do I have to do what most Microsoft users do and "put up with it" and recreate each and every folder through Visual Studio?
You need to put your directory structure in your project directory. And then click "Show All Files" icon in the top of Solution Explorer toolbox. After that, the added directory will be shown up. You will then need to select this directory, right click, and choose "Include in Project."
You can also drag and drop the folder from Windows Explorer onto your Visual Studio solution window.
In Visual Studio 2015, this is how you do it.
If you want to automatically include all descendant files below a specific folder:
<Content Include="Path\To\Folder\**" />
This can be restricted to include only files within the path specified:
<Content Include="Path\To\Folder\*.*" />
Or even only files with a specified extension:
<Content Include="Path\To\Folder\*.jpg" >
Copy & Paste.
To Add a folder, all the sub-directories, and files we can also Copy and Paste.
For example we can:
Right click in Windows explorer on the folder, and Copy on the folder with many files and folders.
Then in Visual Studio Solution explorer, right click on the destination folder and click paste.
Optional add to TFS; Then in the top folder right click and check in to TFS to check in all sub-folders and files.
You can use a symbolic link. This makes modifying the file in one project modify it in the other (as it's actually the same file).
To do this:
Open cmd prompt as administrator
mklink /d [current project directory name] [directory in other project it should point to]
This has it's drawbacks and pitfalls, but I use it on occasion for duplicate libraries that need different names.
Edit for Anoop:
Steps to add to Visual Studio:
Create link in the project folder using the steps above.
In Visual Studio... select project in Solution Explorer.
At the top of Solution Explorer... click the Show All Files button (may need to click it twice if already active).
The link will now show in your project... right-click and choose Include In Project.
These are the steps I follow and works for a couple different projects.
To expand on Yuchen's answer, you can include files and paths as a link. This is not the same thing as adding the existing items because it doesn't make an extra copy in your project's folder structure. It is useful if you want one canonical folder / file etc to be used in a lot of different places but you only want to maintain one version/copy of it.
Here is an example of what you can add to a *.csproj file to create the link
<Compile Include="$(Codez)\z.Libraries\Common\Strings\RegexExtensions.cs">
<Link>Helpers\RegexExtensions.cs</Link>
</Compile>
<Compile Include="..\..\z.Libraries\MoreLINQ\MoreLinq\ExceptBy.cs">
<Link>Helpers\ExceptBy.cs</Link>
</Compile>
<Content Include="C:\Codez\Libs\Folder\OtherFolder\**\*.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
$(Codez) is a Windows Environment variable I defined, you can use the built-in Environment variables in the same manner.
The last example group is a bunch of content files I need in the final output. See https://stackoverflow.com/a/11808911/492 and other answers & links there for more on that.
More MSBuild info at https://msdn.microsoft.com/en-us/library/bb629388.aspx
In Visual Studio 2017, you switch between Solution View and Folder View back and forth. I think this is a better option, because it will keep the solution cleaner. I use this to edit .gitignore, .md files, etc.
I think I found a way to do this with the Compile Include=".\Code***.cs"
What I wanted is to include code recursively under my Code folder.
Here is the project file sample.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0" DefaultTargets="BuildTarget">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<PropertyGroup>
<RootNamespace>Autogen</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Remove="#(Compile)" />
<Compile Include=".\Code\**\*.cs" />
</ItemGroup>
<Target Name="BuildTarget">
<Message Text="Build selected" Importance="high"/>
</Target>
</Project>
In Visual Studio 2013, I couldn't get "Include in Project" to work when right-clicking on a folder. What did work is expanding the folder, selecting all the files then choosing "Include in Project". It was quite tedious as you have to do each folder one by one (but at least you can do all files in each folder in one go), and it appears to store the file path (you can see this by viewing properties on the file and looking at the "Relative Path" option.)
I am hoping to use this to deploy some data files in a Visual Studio Installer project, and it seems to pick up the included files and preserve their paths.
Visual Studio 2017 and newer support a new lightweight .csproj format which has come to be known as "SDK format". One of several advantages of this format is that instead of containing a list of files and folders which are included, files are wildcard included by default. Therefore, with this new format, your files and folders - added in Explorer or on the command line - will get picked up automatically!
The SDK format .csproj file currently works with the following project types:
Class library projects
Console apps
ASP.NET Core web apps
.NET Core projects of any type
To use the new format, create a new .NET Core or .NET Standard project. Because the templates haven't been updated for the full .NET Framework even in Visual Studio 2019, to create a .NET class library choose the .NET Standard Library template, and then edit the project file to target the framework version of your choice (the new style project format can be edited inside Visual Studio - just right click the project in the Solution Explorer and select "Edit project file"). For example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
</PropertyGroup>
</Project>
Further reading:
Old csproj to new csproj: Visual Studio 2017 upgrade guide
Demystifying the SDK Project
MSDN: Additions to the csproj format for .NET Core
I found no answer to my satisfaction, so I figured out myself.
Here is the answer to you if you want to add external source codes to your project and don't want to copy over the entire codes. I have many dependencies on other gits and they are updated hourly if not minutely. I can't do copy every hour to sync up. Here is what you need to do.
Assume this is structure:
/root/projA/src
/root/projA/includes
/root/projB/src
/root/projB/includes
/root/yourProj/src
/root/yourProj/includes
Start your VS solution.
Right-click the project name right below the Solution.
Then click the "Add", "New Filter", put the name "projA" for projA.
Right-click on the "projA", click "Add", "New Filter", enter name "src"
Right-click on the "projA", click "Add", "New Filter", enter name "includes"
Right-click "projA"/"src", click "Add", "Existing Item", then browse to the /root/projA/src to add all source codes or one by one for the ones you want.
Do same for "projA"/"includes"
Do same for projB.
Now the external/existing projects outside yours are present in your solution/project. The VS will compile them together.
Here is an trick. Since the projA and projB are virtual folders under your project, the compiler may not find the projA/includes.
If it doesn't find the projA/includes, then right click the project, select the "Properties".
Navigate to "C/C++". Edit "Additional Include Directories", add your projA/include as such "../projA/includes", relative path.
One caveat, if there are duplicated include/header files, the "exclude from project" on the "Header file" doesn't really work. It's a bug in VS.
As far as I can tell, the only way to do this in VS2010 is akin to the drag and drop method. Right click the solution to which you want to add a project. The application menu will have an add ... item. Opening that, you find that one of the options is to add an existing project to the solution.
In the dialog box that opens, navigate to the folder containing the project file for the solution and select it. VS will, as part of importing that project file, also import the entire directory and, I assume any subordinate directories which are part of that project.
As this does require an existing project file, it won't be impossible to import a directory tree until that tree has been converted to a project.

Resources