How to use external build system for Visual C++ 20

2020-06-17 15:09发布

问题:

Is it possible to use an external build system for VC++ 2013? I want Visual Studio do nothing but build by invoking my build tools.

I am thinking about something like this:

  • Put all build command in batches.
  • Invoke a project-level build batch by right clicking the project and choose build.
  • Invoke the a solution-level build batch by right clicking the solution and choose build.

    Is there some walk-through tutorial? I searched a lot but no luck.

ADD 1 - Some progress...

After briefly reading about the MSBuild process, I tried as below.

First, I edit the *.vcxproj project file. I change the DefaultTargets from Build to MyTarget.

<Project DefaultTargets="MyTarget" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Then I add a new target named MyTarget:

  <Target Name="MyTarget">
    <Message Text="Hello, Bitch!" />
  </Target>

I hope this can bypass the VS2013 built-in built process and only carry out my own batch.

It works well on command prompt:

But in Visual Studio, when I right click the project and choose build command, it gives me a lot of link errors.

How to avoid these link errors? Since my batch can take care of all the build process, I don't need Visual Studio to do the link for me.

ADD 2

It seems these link errors show up because I include the *.c files with the ClCompile tag as below.

<ItemGroup>
   <ClCompile Include="z:\MyProject1\source1.c" />
<ItemGroup>

Since I don't want VS2013 to invoke the compiler, I change it to <ClInclude> tag, the link errors disappeared, but the symbol resolution is not working... Seems I shouldn't change the tag.

ADD 3

Here's another way to compile without linking.

Is it possible for Visual Studio C++ to compile objects without linking

Seems it doesn't have the symbol resolution issue. But I still cannot invoke an external batch by click build/rebuild/clean.

回答1:

You might want to look into Visual Studio's makefile projects (in the Visual C++/General project templates category).

You get to specify what commands to execute for each type of build (clean, build, rebuild). The command can invoke a make, run a batch file, or invoke some other build tool. It just executes a command. The commands can contain various macros that VS provides (similar to environment variables) so the command can be parametrized for things like making a target directory based on the solution or project name or type (debug vs. release).



回答2:

(Michael Burr's reply pointed out a better direction, i.e. a better VC++ project template. You can combine my answer and his.)

Finally, I solved this issue!

The trick is the so-called target overriding. The Visual Studio context menu items Build\Rebuild\Clean correspond to MSBuild targets named Build\Rebuild\Clean, respectively. We just need to override them in the *.vcxproj file.

Such as this:

DO REMEMBER that:

The last target seen by MSBuild is the one that is used — this is why we put the at the end of the existing *.vcxproj file.

And in the override.proj, do whatever you like as below:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Build">
    <Message Text="Build override!" />
    <Exec Command="kickass.bat" />
  </Target>
</Project>

The following 2 links are good reference:

  1. Hack the build

  2. Hijacking the Visual Studio Build Process

Note that:

  • The 1st link take a CSharp project as example, but ALSO works with VC++ project.

  • The 2nd link doesn't work for VC++ project but the rational is similar. If you didn't include the Microsoft.Cpp.targets, you will see the following error when loading the project:

ADD 1

As I tried, we don't need another overrride.proj file. We can just place the specific target at the end of the *.vcxprj file. Such as below:

ADD 2

With target overriding mentioned above, I can run my customized bat file with project's Build/Rebuild/Clean commands. But I noticed that when I run solution's Build/Rebuild/Clean commands, I think it is just following some kind of project dependency order to build each project respectively, which is not exactly equivalent to what I want for an overall build in my scenario.

My current workaround is to create a dummy project and use it to trigger a batch for my overall build.