如何读/写入持续存在并在编译之间进行更新的变量?

发布于 2025-02-12 07:30:03 字数 418 浏览 0 评论 0原文

我正在努力将DLL热载添加到C ++应用中。因此,我打开了MSVC:1个实例的2个实例,当我代码并在另一个实例上编译时,运行该应用程序。断点一开始就击中,我可以继续在第二个实例上代码而无需停止应用程序。

为了支持这一点,我需要读写到构建之间的(半)持久性CMAKE变量。我使用该变量来决定如何命名生成的文件。也就是说,每当我编译一些热加载的DLL时,我都需要在名称之间循环以给出要生成的文件,因为以前的文件是通过运行应用程序使用的。

因此,生成的文件将循环为:

  1. path/to/to/some.a.pdb
  2. 路径/to/to/some.b.pdb,

我如何读/写入每个构建/编译的变量,这记住了先前的编译中的值。 CMakeCache对此无效,因为它在配置阶段设置,并且在生成阶段没有更新。也许可以使用发电机表达式来完成。

I'm working on adding DLL hotloading to a C++ app. Because of this, I open 2 instances of MSVC: 1 instance runs the app while I code and compile on the other instance. Breakpoints are hit on the first instance, and I can continue to code on the 2nd instance without stopping the app.

To support this, I need to read and write to a (semi) persistent CMake variable between builds. I use that variable to decide how to name the generated files. That is, every time I compile some hot loadable DLL, I need to cycle between names to give the files being generated, because the previous files are in use by running app.

So, the generated files would cycle as:

  1. path/to/some.a.pdb
  2. path/to/some.b.pdb

How can I read/write to a variable every build/compile, which remembers the value from the previous compile. The CMakeCache doesn't work for this, because it gets set during the configure phase and not updated during the generation phase. Maybe it could be done with Generator Expressions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-02-19 07:30:03

我很确定您可以使用cmake_< config _ _postfix。是的,您需要重新配置,但是它不会遇到写作以打开/映射库的问题。

来自 documentation

configuration < config

当创建非执行目标时,其< config gt; _postfix目标属性会在设置该变量的情况下初始化该变量的值。

这部分 documentation

邮政编码以附加到配置的目标文件名。

使用配置构建时< config gt;该属性的值将附加到磁盘上构建的目标文件名。对于不可执行的目标,此属性由变量cmake_< config gt; _postFix的值初始化,如果创建目标时设置了。此属性在Mac上忽略了框架和应用捆绑包。

您可以在不同的后缀之间循环循环:

$ cmake -B build -DCMAKE_RELEASE_POSTFIX=_a
$ cmake --build build/ --config Release --target MySwappableDll
... start your app, which loads the _a libraries ...
... later, when you want to rebuild and swap out to _b, just reconfigure ...
$ cmake -B build -DCMAKE_RELEASE_POSTFIX=_b
$ cmake --build build/ --config Release --target MySwappableDll
... now the _b targets are incrementally built and you can hot swap ...

您还可以使用relwithdebinfodebug,甚至完全自定义配置(您甚至可以创建“ A”和A“ B”配置具有不同的后缀,因此您无需重新运行配置步骤)。

I'm pretty sure you can use CMAKE_<CONFIG>_POSTFIX. Yes, you need to reconfigure, but it won't run into problems with writing to open/mapped libraries.

From the documentation

Default filename postfix for libraries under configuration <CONFIG>.

When a non-executable target is created its <CONFIG>_POSTFIX target property is initialized with the value of this variable if it is set.

and this piece of documentation:

Postfix to append to the target file name for configuration .

When building with configuration <CONFIG> the value of this property is appended to the target file name built on disk. For non-executable targets, this property is initialized by the value of the variable CMAKE_<CONFIG>_POSTFIX if it is set when a target is created. This property is ignored on the Mac for Frameworks and App Bundles.

You can cycle between different postfixes like so:

$ cmake -B build -DCMAKE_RELEASE_POSTFIX=_a
$ cmake --build build/ --config Release --target MySwappableDll
... start your app, which loads the _a libraries ...
... later, when you want to rebuild and swap out to _b, just reconfigure ...
$ cmake -B build -DCMAKE_RELEASE_POSTFIX=_b
$ cmake --build build/ --config Release --target MySwappableDll
... now the _b targets are incrementally built and you can hot swap ...

You can also use RelWithDebInfo or Debug or even a totally custom config (you could even create an "A" and a "B" config with different postfixes so you wouldn't need to re-run the configure step).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文