简体   繁体   English

在批处理文件中指定 VS 宏以在构建前或构建后运行

[英]Specify VS macros in a batch file to run pre or post build

I can use the VS macros like $(ProjectDir) in my pre & post build events.我可以在构建前和构建后事件中使用像 $(ProjectDir) 这样的 VS 宏。 But is there any way I can specify them in a batch file & run the batch file as my pre & post build event?但是有什么方法可以在批处理文件中指定它们并将批处理文件作为我的构建前和构建后事件运行?

eg例如

Before

Post-Build event构建后事件

copy $(ProjectDir)foo.txt $(ProjectDir)\out\foo.txt

After

Post-Build event构建后事件

CopyFoo.cmd

where CopyFoo.cmd contains其中CopyFoo.cmd包含

copy $(ProjectDir)foo.txt $(ProjectDir)\out\foo.txt

I want to do this to make my build events list more user-friendly to edit/update.我想这样做是为了使我的构建事件列表更易于编辑/更新。 Editing a batch file is much easier than editing the build events box in VS.编辑批处理文件比在 VS 中编辑构建事件框容易得多。

Not sure if you can access them or not (becuase $ has a different meaning inside batch file), but one way would be to pass them as command line arguments to the batch file.不确定您是否可以访问它们(因为$在批处理文件中具有不同的含义),但一种方法是将它们作为命令行 arguments传递给批处理文件。 You can access them inside the batch file as %0 - %9.您可以在批处理文件中以 %0 - %9 的身份访问它们。

Post-Build event构建后事件

CopyFoo.cmd $(ProjectDir)

Batch file批处理文件

copy %1foo.txt %1\out\foo.txt

Compared to the most accepted result, I here provide with a more intuitive way, especially when you have many macro wanted to use.与最接受的结果相比,我在这里提供了一种更直观的方法,尤其是当您有很多宏要使用时。

Post-Build event构建后事件

set ENV_ProjectDir=$(ProjectDir)
call CopyFoo.cmd

CopyFoo.cmd CopyFoo.cmd

copy %ENV_ProjectDir%\foo.txt %ENV_ProjectDir%\out\foo.txt

Side notes:旁注:

  • (VS2022) I tried to use "set ProjectDir=$(ProjectDir)", but it results in some strange build error for my C# project. (VS2022) 我尝试使用“set ProjectDir=$(ProjectDir)”,但它导致我的 C# 项目出现一些奇怪的构建错误。 Better avoid this naming最好避免这种命名
  • (VS2022) Pre-Build event and Post-Build event env var are seperated. (VS2022) Pre-Build 事件和 Post-Build 事件 env var 是分开的。 It seems you cannot define in pre-build and use it in post-build似乎您无法在预构建中定义并在构建后使用它

have you tried to just write the commands in an external editor and copy the result to the pre/post build event boxes.您是否尝试过仅在外部编辑器中编写命令并将结果复制到构建前/构建后的事件框。 Creating.cmd files and passing around parameters sounds more complicated.创建.cmd 文件并传递参数听起来更复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM