5

I want to use sublime to edit a visual studio project. I have a custom build:

{
   "cmd": ["c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe"],
    "working_dir": "${project_path:${folder:${file_path}}}/../Project"
}

But if I add new files I also need to include them in the project.

Is there a way to do this from the command line, maybe at compile-time?

I am working with opengl using c++;

I basically set up a project using one of the examples provided on the opengl website.

Then I opened the project folder in sublime text and successfully compiled it using the custom build system.

However, when I add NEW source files to the project (*.h and *.cpp) I get a linking error.

I get the same error when I build in visual studio.

The error disappeared after I had included the files by manually browsing and adding them to the project.

What I wanted was a way to automatically add all the source files in a folder to the project(via command line, or wildcard or smth else).

This way I can easily work on a vs2010 project in sublime, add new source files and build the project.

Or maybe there already is a better workflow for this?

7
  • Maybe including verything from a folder via wildcard could be a solution instead of modifying the project every time. Commented Jul 20, 2013 at 16:55
  • Yes.. making the whole folder visible to the project is exactly what I need. Sadly I feel a bit overwhelmed about the information in your link. I still haven't managed to understand what a wildcard is and what the process of creating one is like. Commented Jul 20, 2013 at 17:22
  • 1
    It's not that easy, since a lot of items require specific metadata for them to work. In the end the project file is just XML, so any XML manipulation tools should do the trick. basically you need to inject a <ItemGroup><Compile Include="filename.cs"/></ItemGroup>. Or use a wildcard like: <ItemGroup><Compile Include="**/*.cs"/></ItemGroup>. Commented Jul 20, 2013 at 18:03
  • @VladOtrocol jesse summed it up quite good: Wildcard means you specify *.cs which would include any c# source file in the given Include path. **/*.cs would in also include files from subfolders. The main benefit from this approach is that you would never need to update your .csproj again and wouldn't have to bother about XML manipulation. If you need to include different file types just add ItemGroups as needed. Commented Jul 20, 2013 at 19:41
  • @VladOtrocol If you added some more details about the files you want to include I could come up with more detailed answer. Commented Jul 20, 2013 at 19:46

1 Answer 1

8

You could try to modify your .vcxproj file to include any .h and .cpp file in your project folder or folders below.

In case of a c++ VS project you can try to alter your .vcxproj file like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- rest of project file untouched -->

    <!-- start of modified part -->

    <ItemGroup>
        <ClInclude Include="**\*.h" />
    </ItemGroup>
    <ItemGroup>
        <ClCompile Include="**\*.cpp" />
    </ItemGroup>

    <!-- end of modified part -->

    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    <ImportGroup Label="ExtensionTargets">
    </ImportGroup>
</Project>

Be aware that adding files to your project from inside VS at later point will replace the modification described above!

As an alternative you could also create an external project file holding the same <ItemGroup /> elements described above and include this project file into your .vcxproj.

I'll add an example of this alternative if you're interested.

Sign up to request clarification or add additional context in comments.

3 Comments

Always glad to help ;-) You can do amazing things with MSBuild and extending VS projects like this is just a very little part.
Damn, dude, been struggling with DTE to add files to a Shared Project. Glad I found ur answer.
I hold that it should not be done this way, however. Because it may prevent the IDE (VS) from making changes to files changed in this way. For C++ -- to this day -- including by wildcard isn't "a thing". For the new C# SDK-style projects it's the default, however. So it works. Nevertheless VS will choke on various manual changes, even something as simple as leaving out the Condition keyed on the Configuration/Platform pair can cause VS to either fail being able to edit the project file or roll back your changes. Use a .targets or .props file instead. Of your own *.*proj ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.