Visual C++ project dependency on multiple architectures - c++

I have a Visual C++ solution with multiple projects. One of the projects (let's name it Project 1) depends on both x86 and x64 versions of another Project 2. It means that when doing a rebuild of Project 1, no matter which architecture is selected I want both x86 and x64 versions of Project 2 to be compiled, prior to Project 1.
Standard Project Dependency feature doesn't allow to specify dependency on multiple architectures. Is there any way to accomplish this task?

Visual C++ has a command line compiler.
You can execute it on the project you need built, or split your code into multiple slns and tell the command line to build the slns.
This dependency can be set up as a custom build step, or you could go and write a makefile that describes your project dependencies and replace your sln build with that.

You can use a Custom Build Step or Custom Build Event to call msbuild twice, once for each architecture. This is how Visual Studio builds projects, so you are effectively recursively calling the build system.
Here's more detailed information:
Understanding Custom Build Steps and Build Events
https://msdn.microsoft.com/en-us/library/e85wte0k.aspx

Related

Visual Studio 2019 C++ Reference Project Not Built Before Dependent Project Tries To Link in Release Configuration using MSBuild

I have a solution that contains both C++ and C# projects that is built in a nightly CI build on a remote machine. The build script checks out a clean copy of the source and builds both debug and release configurations of the solution using MSBuild and runs the test suite on each configuration.
About every other build, the release configuration fails to build properly. An analysis of the build log reveals that C++ Project Q, which depends on C++ Project D, tries to link before Project D is done. This error only happens for the release configuration on this particular build machine - the debug configuration builds without error. I have a separate nightly build process that runs on a separate machine where the release configuration is built with a similar script that uses MSBuild (it just does not run the test suite), and it builds the same source revision without issue. Multiple team members build the solution without issue either from update or clean checkout with one or both configurations, always from the Visual Studio 2019 IDE, on various operating systems.
Project Q is configured with Project D as a project reference and Project D is also listed as a hard-dependency for Project Q. As I mentioned, the build script is using MSBuild.
An additional item of interest from analysis of the release and debug build logs: The build of Project D is initiated differently between the two configuration builds. It is started by its own metaproject in the release configuration (as item 60, for instance), but started earlier (as item 44, for instance), by a different project in the debug configuration. Not sure why the dependency algorithm would run with such different results in the two cases since the solution and working source being built are the same.
Any ideas or suggestions would be appreciated.
Update: Inspection of differences between the release and debug build logs reveals some interesting facts. In the failure case I did a search for ") is building" in each log - should be an indication of how many projects were built, including metaproj entries. For the failure case, debug had 282 occurrences, whereas release had 175. For the success case, debug had 280 occurrences and release had a whopping 559! A similar search for "Done Building Project" yields similar results, only off by 1 or 2. That might partly explain the differences in build order between the solutions. I also need to check for conditional build entries.
Visual Studio 2019 Reference Project Not Built Before Dependent
Project Tries To Link in Release Configuration using MSBuild
It seems that project buid order was broken and Project D built later than Project Q which need the output content of Project D, so the whole build failed.
Not sure that if you use Project Dependency(Right-click on project-->Build Dependencies-->Project Dependencies), if so, only VS IDE Build Will recognize their build order while MSBuild command line will lose the relationship about them.
Besides, I wonder if your main project under Release mode references a dependency project under Release mode. If you did these, the build will definitely go wrong.
All of these above is that I think you made some changes to your project.
You could follow these steps:
1) I suggest you could try to use Project Reference and it will add these xml node in ProjectQ.csproj file to strongly specify build relationship:
<ItemGroup>
<ProjectReference Include="..\ProjectD\ProjectD.csproj">
<Project>{26c26cdd-a5e0-40c7-b0c9-4563f969424f}</Project>
<Name>ProjectD</Name>
</ProjectReference>
</ItemGroup>
Also, check if there are any conditions that distinguish between Debug or Release mode when referring to a project like this:
<ProjectReference Include="..\ProjectD\ProjectD.csproj" Condition="'$(Configuration)'=='Debug'">
If so, please remove that condition Condition="'$(Configuration)'=='Debug'" to make sure it is the same between Debug and Release mode.
2) close VS Instance, delete .vs hidden folder under solution folder.
3) check your CI Build and cloud build server and ensure that cloud parameters are consistent with other servers. And examine any of your xxx.csproj files to check if you have any other operations which causes this bahavior.
In addition, if necessary, you can share your xxx.csproj file and the build script with us to troubleshoot it.

Visual Studio - C++ - build and run app as 32bit or 64bit - load DLL from appropripate ProgramFiles dir

I build my app either as x86 or x64. This app uses external DLL.
I have x64 system (Windows 10) with the same DLL library installed for both platforms - x86 and x64. They are placed in same folders inside appropriate Program Files directory. I can manually set path to either one in environment variable PATH and it woks. But it is a little incovinient to rewrite PATH and reset computer when I switch platform and want to test the other one. Is there any solution, how system automatically loads correct DLL from correct Program Files dir?
I have found solution for running apps from Visual Studio: How do I set the path to a DLL file in Visual Studio?
It is working as epxected. If I run app outise Visual Studio, I set PATH variable by myself and is also working.
Copy the DLL in the build location next to the executable, for Visual Studio this is typically \Debug or \Release in a architecture dependent sub directory (i.e. x86 or x64). Just make sure the target / output locations are set correctly in the project settings.
You have to copy only once, or more correctly: each time after you 'clean' the solution. To make this easier, many people use a dll-copy script (use batch, ruby or python) and have it run automatically before building or after cleaning. You can execute the script in a pre-build step or post-build step that can be configured in your Visual Studio solution or project settings.
There is also a more robust way to handle build artifacts and peculiarities: I highly recommend the use of CMake to keep the build matrix organized. It is provides a general cross-platform approach to script pre-build and post-build actions such as tracking dependencies, copying files, packaging installers, deployment, version verification, versioning, etc.. it comes with an easy scripting language so you can build macro's and functions to do your copying. It can be a bit of a learning curve to get it right, but once it's there, it provides a robust dependable way to build out your project build pipeline.

msbuild for visual studio 2008 projects

We have a relatively big C++ application with lots of dependencies. On our build server running msbuild 4.0.30319, we keep getting linker errors and failures when doing incremental builds using msbuild.
After further investigating this, it does appear to be that dependency libraries are not being picked up and built prior to building the application.
The question i have is as follows:
Are there best practices for building such a C++ project with dependencies? if yes, what is the correct way to do incremental builds using MSBUILD for a C++ application built using VS2008 with a fair amount of dependencies.
Just as Thomas comment "build servers should be making full builds, not incremental builds. "
When you create a new build definition on build server by default it does a clean build for you every time. That is to say that between builds all the source from the previous build is deleted along with the compiled outputs and then the source is downloaded fresh, built and you are good to go. There is a good reason for this to be the default – it is the safest option. If your build script messes around with the files in your source directory at all (perhaps baking in a build number into the AssemblyInfo files etc) then you want to get a clean workspace to ensure that you are back to a known good state.
However, if you want to enable the incremental builds in build Server, like TFS, you can refer to the Incremental Builds in Team Foundation Server for more detail.
but i am more interested how msbuild handles build dependencies on an incremental build and if it supports it
Dependencies between projects could result in inconsistent builds. Visual Studio does respect project-to-project references and builds the reference project before the referring project. However, if several projects have a shared reference, that reference is built only once and “cached” for the next times it is referenced. Also, errors and exceptions on one build do not affect the running of other builds, which may depend on the failing build. For the more info, please refer to Managed Incremental Build.

in visual studio solution, how can I use exe built in one project in another project

In Visual Studio, I have a solution that includes multiple projects. One of the projects is a code generator that creates a executable. I want to use that executable in the same solution for another project that will use that executable in a custom build step to generate c++ code.
I thought I could use a reference but I cannot see how to specify the executable in the custom build using that reference. Is a reference really used just for linking against?
Is the only way to use the executable to create a Post-Build step in the one project and move that executable to a known location in the solution and call that out in the other project?

Create Visual Studio "Custom Build Step" with CMake

I would like to run a code generator every time my project is built in Visual Studio, even if no source file in the project was changed. Therefore I would like to have a custom build step set up as explained in Visual Studio: Run C++ project Post-Build Event even if project is up-to-date.
How can I create such a build step with CMake?
I think a custom target is what you are looking for: add_custom_target
From the documentation:
Add a target with no output so it will always be built.
Or if you are generating a code file,
https://cmake.org/cmake/help/v2.8.8/cmake.html#command:add_custom_target
can be run POST_BUILD and generate output.
This is afaik not possible with CMake, and is therefore a missing feature for sure.
The answer from Tarydon in the question you refer to, is about setting up precisely what you want - a "Custom Build Step". This means that you still only have your main target (VS Project), with something that looks like a "Post-Build Event" but technically isn't, since Post-Build Events aren't run if the project is up-to-date.
The answer from tpg2114 works, but has one major drawback; it spams your solution with phony projects. In case you have a hundred projects in a solution, having to add another hundred just as post-build wrappers to the first hundred is of course undesirable.
Depending on your situation, it might sometimes be easier to use Post-Build Events and force a rebuild of at least one source file, for the project to actually build and therefore also run your custom command.