如何在 C++ 中读取 CMake 变量源代码
我想将我的库的版本号存储在一个地方。所以我在 CMake 文件中定义了这样一个变量:
SET(LIBINTERFACE_VERSION 1 CACHE INTEGER "Version of libInterface")
通过这个定义,我可以根据 Microsoft 的定义生成一个 version.rc 文件,我将其编译到库中,然后在我的 dll 文件的属性窗口中正确显示。
现在我也想在我的 c++ 源代码中使用这个 CMake 变量,但实际上我没有找到可行的解决方案。我尝试过不同的事情,比如这样:
#ifndef VERSION_LIBINTERFACE
# define VERSION_LIBINTERFACE @LIBINTERFACE_VERSION@
#endif
或者这样:
unsigned int getLibInterfaceVersion()
{
return @LIBINTERFACE_VERSION@;
}
但是编译器不会接受任何东西。由于我在 CMake-Documentation 中的研究没有得到任何结果,我希望有人能给我重要的建议。
提前致谢。
I'd like to store the version number of my library in just one place. So I have defined such a variable in the CMake-file:
SET(LIBINTERFACE_VERSION 1 CACHE INTEGER "Version of libInterface")
With this definition I can generate a version.rc file according to Microsoft's definition, which I compile into the library and afterwards shows up correctly in the properties window of my dll-file.
Now I'd like to use this CMake variable in my c++ source code too, but I actually don't get to a working solution. I've tried different things like this:
#ifndef VERSION_LIBINTERFACE
# define VERSION_LIBINTERFACE @LIBINTERFACE_VERSION@
#endif
or this:
unsigned int getLibInterfaceVersion()
{
return @LIBINTERFACE_VERSION@;
}
But the compiler won't accept anything. Since my researches in the CMake-Documentation didn't get any results, I hope that someone could give me the essential advice.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是将 LIBINTERFACE_VERSION 作为定义传递 add_definition:
但是,您也可以创建“头文件模板”并使用 configure_file。这样,CMake 将替换您的@LIBINTERFACE_VERSION@。这也更具可扩展性,因为您可以在此处轻松添加额外的定义或变量...
例如创建一个文件“version_config.h.in”,如下所示:
然后将configure_file行添加到您的cmakelists.txt:
当然,确保源文件中包含正确的 version_config.h。
The easiest way to do this, is to pass the LIBINTERFACE_VERSION as a definition with add_definition:
However, you can also create a "header-file template" and use configure_file. This way, CMake will replace your @LIBINTERFACE_VERSION@. This is also a little more extensible because you can easily add extra defines or variables here...
E.g. create a file "version_config.h.in", looking like this:
Then add a configure_file line to your cmakelists.txt:
And of course, make sure the correct version_config.h is included in your source-files.