How add existing file to Different Visual Studio Project Programmatically - c++

I am newbie.
I want to create a Visual C++ project as programmatically.
I have tried EnvDTE objects but they are working on instance of current solution but I want to edit different project file of different solution that not opened anywhere.
How can I get instance of different project to edit it?
And I am using Visual Studio 2013 Express. Is there any limimation for it? Should I use VSPackage's?
Thanks.

In general I would recommend considering a build system like CMake or Scons. It allows to generate build scripts for arbitrary platform (as such it includes MSVS solution files).
However, you might simply edit project file with some script or program. Project files are plain xml files and it's easy to add external files.
<ItemGroup>
<ClCompile Include="some_file.cpp" />
</ItemGroup>

Related

How to specify which .cpp files to compile based on the current build configuration?

I have multiple build configurations in my project, and I'd like to swap some .CPP files based on the currently selected configuration. How can I do that in Visual Studio 2013?
In the GUI, see properties of a cpp file and set "Excluded From Build" to yes for the configurations where it's excluded.
In the project file would look like:
<ClCompile Include="my_platform_specific_file.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
true
</ExcludedFromBuild>
</ClCompile>
In the IDE.
Select the configuration you want to alter (either from Build > Configuration Manager or the drop down in the toolbar. From the solution explorer, on the file you wish to; right click > Properties > Excluded from Build > Select Yes or No.
In the project file itself, locate the file being excluded
<ClCompile Include="xyz.cpp">
Add the following element;
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='MyConfiguration|Win32'">true</ExcludedFromBuild>
Where MyConfiguration is the configuration you wish to exclude the file from.
This might be a good reason to start looking into more generic project managers.
I can recommend you looking into CMake , which is very powerful and can generate VisualStudio projects. The learning curve might be a high (for someone who is used to Visual Studio automation) but the gains are very high.
Some links:
Linking different libraries for Debug and Release builds in Cmake on windows?
http://cmaketools.codeplex.com/ - support for CMake files editing
http://www.youtube.com/watch?v=w9sKd8f0kFo - video displaying cmake integration into VisualStudoi
http://www.cmake.org/Wiki/CMake - random help
The project site is: http://www.cmake.org

How does Visual Studio 2010 hosts MSBuild for C++ projects?

I have a solution with several C++ projects. For some of the projects I need some custom file copy, e.g. to copy some configuration files to the output directory or to copy the output files of one project to a specific folder after build.
In some cases I don't want or cannot add these files to the projects directly through the Visual Studio IDE. I created simple .targets files which I can reuse and add to the projects which need the file copying.
Here is a simple example .targets file for copying configuration files:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
CopyCustom
</BuildDependsOn>
</PropertyGroup>
<ItemGroup>
<CustomFiles Include="$(ProjectDir)Config\**\*.xml" />
</ItemGroup>
<PropertyGroup>
<DestCustFolder>$(OutDir)Config\</DestCustFolder>
</PropertyGroup>
<Target Name="CopyCustom"
Inputs="#(CustomFiles )"
Outputs="#(CustomFiles ->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')">
<Message Text="Copy custom files..." />
<Copy SourceFiles="#(CustomFiles )" DestinationFiles="#(CustomFiles->'$(DestCustFolder)%(RecursiveDir)%(FileName)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
Through the "Build Customization" dialog in Visual Studio I add it to the project so it will be included like this at the end of the project file:
<ImportGroup Label="ExtensionTargets">
<Import Project="..\Targets\CopyCustom.targets" />/
</ImportGroup>
This should enable incremental build of my custom target. If I just edit one of my custom files (and none of the C++ files) and build it form the console with
msbuild foo1.vcxproj
it will actually detect the changes and does an incremental build for my custom target. If no changes are made the target is skipped.
If I do however build inside Visual Studio it will not detect changes to the custom files and only and gives me the message that the project is up to data:
========== Build: 0 succeeded, 0 failed, 5 up-to-date, 0 skipped ==========
I would have to additionally change one of the C++ files to make it check all targets again and to the incremental build.
I was expecting that Visual Studio just executes MSBuild which will then do the up-to-date check on the projects, so it should be the same result as running MSBuild from the console. I was trying to get more information by setting the verbosity level to diagnostic but I just get the same line. It seems to me that MSBuild is not even executed for the project but Visual Studio itself determines that the project is up-to-date.
So I was wondering how Visual Studio actually determines when it should execute MSBuild for a project.
I asked basically the same question before on the MSDN forum but couldn't get a clear answer.
See this suggestion on Microsoft Connect.
Basically you need to set DisableFastUpToDateCheck property to true to disable the fast-up-to-date check.
Just add to your vcxproj or your targets file:
<PropertyGroup>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
I found an answer by looking into the book "Inside the Microsoft Build Engine, Second Edition".
Note: I also updated the same in my question in the MSDN forum but I will mainly duplicate the text here again for completeness.
On page 280 they actually saying that the IDE does a "fast up-to-date check" on the project-level. It only spawns a project build and does a more fine-grained check on the individual tasks if this rough project-level check fails.
When running MSBuild from the command line however there is always a fine-grained up-to-date check on the individual tools.
So the IDE only seems to do this fast check on the files which are added to the projects directly and set as one of the "Input File" types.
In my opinion this is not a good design. I would prefer that the IDE is only used to edit the MSBuild project files and then just invokes MSBuild to do the up-to-date check. This would make it much clearer.
I can understand that in a solution with a lot of projects it can make the up-to-date check much faster but there should be at least an option to disable the fast up-to-date check. I was checking the IDE if there is a way to disable this behavior but could not find anything.
The solution suggested here actually works and I am using it at the moment. But I added several custom targets for different kinds of custom files. If I add a new custom file I should never forget to set it to "Custom Build Tool" otherwise the incremental build for this file will not work.
I guess a solution would be to make a full build customization for my custom files and use specific file extensions for them so Visual Studio will automatically detect them when I add them to the project and sets the right Item Type.

How to create Visual studio solution from make files?

I have a source code for a project with their make files. I want to create a Visual Studio (2005) solution from it. Is there any direct way to do this? can anyone help me please. I spent hours for searching, but couldn't find a way to do this.
Thanks.
Unfortunately, Microsoft removed this capability after VC++ 6.
If all you're looking to do is to build a Visual Studio project from a command line or script, you can use the devenv command to build using the settings in a project.
Something like:
devenv /build debug /project myproj myapp.sln
Ans starting with VS2010, C++ projects will use the MSBuild system, so you can drive builds using that technology.
If you really want a makefile, you'll need to write it up by hand (or maybe there's some 3rd party tool out there that I'm unaware of).
I'm not sure whether this solution can help you. Which I tried and it worked well in my previous projects. It need manually add the files.
Create a blank VS solution/project. Add the source files into that project.
Mark all source files as "Excluded from building". You can right click the files in project explorer and find the setting. So now nothing will happen when you build your project.
In project setting, find something like "Custom build step". Add the commands that invoke your original build command. (You may write different build command for debug/release ). You can also set the post-build actions such to copy your result to some folder....
Now you can edit and build source files.
For my experience, I can even debug it after setting the executable.
Hope this can help you.
If this is a one-off then it is easier to just create the VS project manually in visual studio.
If you are going to need to do this often look at ceating the project in something like cmake or Qt's .pro whcihc an generate makefiles and VS build files from the same defintion.
Do you want to use the makefile to build? You can create a project from existing source in VS 2005 and setup the project to use make to build (and the wizard will take you through all of this).
I am using VS2010.In order to build you can create a project from existing code. In VS2010 you can create project from existing code File->New->Project from Existing code. You can specify the other parameters and then ready with the solution. I did not go with make file but followed this approach which is working great.

Can the list of C++ files in a Visual Studio project be dynamically filled?

I have a tool that generates most (but not all) files that need to be compiled in Visual Studio. The tool reads a configuration file and generates c++ files afterwards. This list can differ from one call to another when the configuration is altered.
I am wondering whether it would be possible to adapt the compiling process to my needs, which would be :
Launch the tool (not needed if configuration file has been modified)
Retrieve new list of C++ files to be compiled (ideally isolated in a folder inside the project)
Compile C++ files
EDIT: Having to close Visual Studio for this process to work is a no-go for me. The idea would be to have cpp files dynamically added as the first step of the compilation process.
Use a pre-build step to run your tool.
Also, create a file containing the list of includes and sources
This file name should be fixed (so that you don't have to change project properties or the vcproj file) -- add it to the project. E.g:
Project Properties > Command Line > Additional Options > #headerListingFile
You are not trying to integrate lex/yacc output with VS, are you?
Would CMake help? It's an automated project manager that generates Makefiles and VS projects for projects you define. Just add a source file, re-run CMake and you're done.
I think what you should do is create a custom makefile and use that for builds.
Please see this page for more information.

How to use T4 code generation templates with VS C++ projects?

T4 template files are automatically recognizable by the IDE under C# projects, but I have no clue on how they can be integrated into C++ projects (other than using make files).
Any ideas?
T4 Template files can be integrated into C++ projects, but it's a bit more work than with a C#/VB project. Create a new text file in your C++ project and give it a .tt extension. Then write your template as normal. A C++ project then needs further work to get it to transform the templates. The quick and dirty way I got it to work was to add a custom build step and have it call "C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.1\TextTransform.exe" directly. Another way I found was to add a custom MSBuild task. Instructions can be found here
This page has more information and some good links to other pages on using T4 code generation.
MSBuild Task will not work as this is a vcproj file (C++) so vcbuild is used. The easiest way to get the tt compiled is to add a custom build step like below..
"C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\1.1\TextTransform.exe" -out "$(ProjectDir)\VSProject.cpp" -I "$(ProjectDir)" "$(ProjectDir)\VSProject.tt"
I spent several hours investigating the MSBuild Task solution above and it's pretty good for managed code but I can't see any way to use it for C++ (bar converting the vcproj to csproj eek)
For Visual Studio 2017 (and maybe 2015?)
The commands listed in the other answers are partly incorrect, as the file TextTransform.exe has been moved to a new directory: the devenv root folder, e.g.:
C:\Progra~1\Visual Studio 2017\Enterprise\Common7\IDE
The command should now be updated to the following:
"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)xxx.cpp" "$(ProjectDir)xxx.tt"
where xxx is the file name of yout .tt template file
Having experimented with some of the above (and found them unsuitable for my specific case), I took a different approach that others may find helpful.
Rather than fight to get VS to accept a T4 template within a C++ project, I added a separate (empty) C# project to my solution. I put my template file at the root of the solution and add a link to it from the C# project. I #include the generated .h file from my C++ project.
I used the template to generate a version string that updates with every build. I added the AutoT4 tool to VS so that the string is updated with every build of the solution.