4

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?

Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55
  • Maybe [including verything from a folder via wildcard](http://stackoverflow.com/a/2587401/205233) could be a solution instead of modifying the project every time. – Filburt Jul 20 '13 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. – Vlad Otrocol Jul 20 '13 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 ``. Or use a wildcard like: ``. – jessehouwing Jul 20 '13 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. – Filburt Jul 20 '13 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. – Filburt Jul 20 '13 at 19:46
  • @Filburt I edited my question providing more details. – Vlad Otrocol Jul 22 '13 at 12:27
  • And also this code you describe above.. where do I add it? Is there a config file in the vs project that I need to modify (something similar to apache?), do I make a new file and add it to my project? What type of file? I wouldn't insist with so many questions.. but google was not helpful. – Vlad Otrocol Jul 22 '13 at 12:45

1 Answers1

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 1
    Always glad to help ;-) You can do amazing things with MSBuild and extending VS projects like this is just a very little part. – Filburt Jul 23 '13 at 17:42
  • Damn, dude, been struggling with DTE to add files to a Shared Project. Glad I found ur answer. – vikAy Mar 17 '22 at 18:48
  • 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` ... – 0xC0000022L Aug 05 '23 at 20:27