defining a target when compiling a visual c++ project using xbuild - c++

I have a visual c++ project that outputs a library and I would like to build it on Linux using xmake. I can build it in monodevelop but I want to be able to build it from a command line.
If I try building the project using "xbuild" call then I get the following error:
....ItemMinerLibMono.cproj: error : Target named 'Build' not found in the project.
I understand from the documentation that I need to add a Target named "Build" in the csproj file but I don't know how to do that. I tried importing the Microsoft.Common.targets file like this:
<Import Project="$(MSBuildBinPath)\Microsoft.Common.targets" />
but then I get the error:
: error : Target 'CreateManifestResourceNames', a dependency of target 'PrepareResources', not found.
Does anybody have any idea how to successfully compile a c++ project from a command line?
Thanks,
Gregor

Add the following to you .cproj file from MonoDevelop and it will build with xbuild.
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)" Outputs="$(TargetPath)"/>
There is probably a simpler solution, but so far I have not been able to come up with one.

You need to import Cpp targets.
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
On a Windows machine with a full Visual Studio installation, some or all of these can be found at the following locations. I've not tested these under any version of Linux with xbuild:
VS2012: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v110
VS2013: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v120
VS2015: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v140

Related

Enable Visual Studio's C++ Core Check analysis only on project files?

I am a big fan of the C++ Core Guidelines and I like to follow them in all projects I work on, so I enabled the following option in my project template in Visual Studio 2017:
This tool is great and helps me write better code, but I simply cannot figure out how to make it only analyze my files. Whenever my project has a dependency such as Boost or OpenCV, I will get plastered with a wall of warnings:
These dependencies are added through vcpkg, however, the same thing happens when adding them manually with C/C++ > General > Additional Include Directories.
Is there any way to only make these warnings apply to project files, and not all included files?
As mentioned in the comments, right after the following section in your .vcxproj near the end of the file:
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
The problem may be solved by adding the following after the section mentioned above:
<PropertyGroup Condition="'$(Language)'=='C++'">
<CAExcludePath>$(QTDIR)\include;.\GeneratedFiles;$(CAExcludePath)</CAExcludePath>
</PropertyGroup>
Furthermore, if you are using vcpkg, which was the case in my situation, you will need to add the following element to the CAExcludePath:
$(VcpkgRoot)include
This will ensure that all headers from any packages will not be analyzed.

Copy NuGet Boost DLL to automatically to the Output directory

I am using several Boost libraries in my C++ project. The libraries are acquired through NuGet packages, e.g. the Boost Thread library boost_thread.
Compiling and linking works without any changes to the project properties. But debugging and running the application fails due to missing DLLs in the output directory.
One solution is to use the post build step copying the required DLLs. This is described at other places, e.g. how to make visual studio copy dll to output directory?.
This is an example for the required copy command in the Debug configuration:
xcopy /F /Y "$(SolutionDir)\packages\boost_regex-vc100.1.58.0.0\lib\native\address-model-32\lib\boost_regex-vc100-mt-gd-1_58.dll" "$(OutDir)"
The Project is Visual Studio 2010 project. But the IDE actually used is Visual Studio 2013.
But is there a better way to achieve this?
I used MSBuild's copy task for this exact purpose:
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<BoostRT>-gd</BoostRT>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release' Or '$(Configuration)'=='Release_withPDB'">
<BoostRT></BoostRT>
</PropertyGroup>
<ItemGroup>
<BoostDlls Include="..\packages\boost_log-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_log-vc120-mt$(BoostRT)-1_59.dll;
..\packages\boost_thread-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_thread-vc120-mt$(BoostRT)-1_59.dll;
..\packages\boost_system-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_system-vc120-mt$(BoostRT)-1_59.dll;
..\packages\boost_chrono-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_chrono-vc120-mt$(BoostRT)-1_59.dll;
..\packages\boost_date_time-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_date_time-vc120-mt$(BoostRT)-1_59.dll;
..\packages\boost_filesystem-vc120.1.59.0.0\lib\native\address-model-$(PlatformArchitecture)\lib\boost_filesystem-vc120-mt$(BoostRT)-1_59.dll" />
</ItemGroup>
<Target Name="CopyFiles" AfterTargets="Build">
<Copy SourceFiles="#(BoostDlls)" DestinationFolder="$(OutDir)" />
</Target>
Hardcoding the boost lib version (1.59) is not awesome but other than that it works well.

How add existing file to Different Visual Studio Project Programmatically

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>

Specify ruleset for Visual Studio code analysis on command line or from CMake

I am trying to enable code analysis for my CMake driven project. I generate Visual Studio 2013 solutions, and would like to enable code analysis with a specific rule set. I know that I can enable the code analysis by specifying add_definitions("/analyze") in my CMake file, but how do I control which rule set is used?
As far as I can see, the only way to control the ruleset used is via the xml element CodeAnalysisRuleSet in the project file, but I cannot access this from CMake.
If you are trying to solve this for a command line build--either via cmake --build or direct invocation of msbuild--and not when using the generated solution with Visual Studio, you can set the relevant properties when you invoke the build.
With cmake driving the build:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules.ruleset'
With MSBuild driving the build:
PS c:\build-dir> msbuild ALL_BUILD.vcxproj '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=NativeRecommendedRules'
If you have a custom ruleset in a custom directory, you will also need to set the property CodeAnalysisRuleSetDirectories:
PS c:\build-dir> cmake --build . -- '/p:RunCodeAnalysis=true' `
'/p:CodeAnalysisRuleSet=custom.ruleset' `
'/p:CodeAnalysisRuleSetDirectories=c:\src\ruletsets'
(Tested with CMake 3.8.0, MSBuild 15.3.409.57025, and Visual C++ 2017 19.11.25506.)
I am not sure how far this works for VS2013 solutions. With Visual Studio 2015 I am using Project User Templates (*.vcxproj.user) with something along the following lines:
C:\MyProject\Template.USERNAME.user
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CodeAnalysisRuleSet>#CMAKE_SOURCE_DIR#\SecurityRecommended.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
</Project>
You will probably have to change a few things like the ToolsVersion to 12.x.
The #CMAKE_SOURCE_DIR# will automatically get replaced by cmake when you use the CONFIGURE_FILE function to generate the actual PROJECT.vcxproj.user file. I have the following line in the CMakeLists.txt of my projects:
SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName)
SET(USER_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.vcxproj.user)
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/Template.${USER_NAME}.user ${USER_FILE} #ONLY)
In the end, I would assume you could user-define anything that's in the PROJECT.vcxproj with those templates.
VS2017:In CMakeSettings.json section "configuration" add parameter "codeAnalysisRuleset": "NativeRecommendedRules.ruleset". Then in Solution Explorer right-click on need file .cpp select menu "Run Code Analysis on File". You can choose from a variety of analyzers in VS folder "c:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Team Tools\Static Analysis Tools\Rule Sets\NativeRecommendedRules.ruleset"

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.