<i id='a15nz'><tr id='a15nz'><dt id='a15nz'><q id='a15nz'><span id='a15nz'><b id='a15nz'><form id='a15nz'><ins id='a15nz'></ins><ul id='a15nz'></ul><sub id='a15nz'></sub></form><legend id='a15nz'></legend><bdo id='a15nz'><pre id='a15nz'><center id='a15nz'></center></pre></bdo></b><th id='a15nz'></th></span></q></dt></tr></i><div id='a15nz'><tfoot id='a15nz'></tfoot><dl id='a15nz'><fieldset id='a15nz'></fieldset></dl></div>
    1. <tfoot id='a15nz'></tfoot>

      • <bdo id='a15nz'></bdo><ul id='a15nz'></ul>
      1. <legend id='a15nz'><style id='a15nz'><dir id='a15nz'><q id='a15nz'></q></dir></style></legend>

        <small id='a15nz'></small><noframes id='a15nz'>

        如何在 C++ 源代码中读取 CMake 变量

        时间:2023-08-28
        <i id='hWcR1'><tr id='hWcR1'><dt id='hWcR1'><q id='hWcR1'><span id='hWcR1'><b id='hWcR1'><form id='hWcR1'><ins id='hWcR1'></ins><ul id='hWcR1'></ul><sub id='hWcR1'></sub></form><legend id='hWcR1'></legend><bdo id='hWcR1'><pre id='hWcR1'><center id='hWcR1'></center></pre></bdo></b><th id='hWcR1'></th></span></q></dt></tr></i><div id='hWcR1'><tfoot id='hWcR1'></tfoot><dl id='hWcR1'><fieldset id='hWcR1'></fieldset></dl></div>
        <legend id='hWcR1'><style id='hWcR1'><dir id='hWcR1'><q id='hWcR1'></q></dir></style></legend>
        <tfoot id='hWcR1'></tfoot>

            <tbody id='hWcR1'></tbody>

          <small id='hWcR1'></small><noframes id='hWcR1'>

              • <bdo id='hWcR1'></bdo><ul id='hWcR1'></ul>
                  本文介绍了如何在 C++ 源代码中读取 CMake 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将我的库的版本号存储在一个地方.所以我在 CMake 文件中定义了这样一个变量:

                  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")
                  

                  有了这个定义,我可以根据微软的定义生成一个 version.rc 文件,我将其编译到库中,然后在我的 dll 文件的属性窗口中正确显示.

                  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.

                  现在我也想在我的 C++ 源代码中使用这个 CMake 变量,但我实际上没有找到一个可行的解决方案.我尝试过类似这样的不同事情:

                  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
                  

                  或者这个:

                      unsigned int getLibInterfaceVersion()
                      {
                          return @LIBINTERFACE_VERSION@;
                      }
                  

                  但是编译器不会接受任何东西.由于我在 CMake-Documentation 中的研究没有得到任何结果,我希望有人能给我一些重要的建议.

                  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.

                  提前致谢.

                  推荐答案

                  最简单的方法是将 LIBINTERFACE_VERSION 作为定义传递给 add_definition:

                  The easiest way to do this, is to pass the LIBINTERFACE_VERSION as a definition with add_definition:

                  add_definitions( -DVERSION_LIBINTERFACE=${LIBINTERFACE_VERSION} )
                  

                  但是,您也可以创建头文件模板"并使用 configure_file.这样,CMake 将替换您的@LIBINTERFACE_VERSION@.这也更具可扩展性,因为您可以在此处轻松添加额外的定义或变量...

                  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...

                  例如创建一个文件version_config.h.in",如下所示:

                  E.g. create a file "version_config.h.in", looking like this:

                  #ifndef VERSION_CONFIG_H
                  #define VERSION_CONFIG_H
                  
                  // define your version_libinterface
                  #define VERSION_LIBINTERFACE @LIBINTERFACE_VERSION@
                  
                  // alternatively you could add your global method getLibInterfaceVersion here
                  unsigned int getLibInterfaceVersion()
                  {
                      return @LIBINTERFACE_VERSION@;
                  }
                  
                  #endif // VERSION_CONFIG_H
                  

                  然后在您的 cmakelists.txt 中添加一个 configure_file 行:

                  Then add a configure_file line to your cmakelists.txt:

                  configure_file( version_config.h.in ${CMAKE_BINARY_DIR}/generated/version_config.h )
                  include_directories( ${CMAKE_BINARY_DIR}/generated/ ) # Make sure it can be included...
                  

                  当然,请确保您的源文件中包含正确的 version_config.h.

                  And of course, make sure the correct version_config.h is included in your source-files.

                  这篇关于如何在 C++ 源代码中读取 CMake 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CMake:如何设置源、库和 CMakeLists.txt 依赖项? 下一篇:如何在 cmake 中使用 SDL2 和 SDL_image

                  相关文章

                1. <small id='xXVhg'></small><noframes id='xXVhg'>

                2. <legend id='xXVhg'><style id='xXVhg'><dir id='xXVhg'><q id='xXVhg'></q></dir></style></legend>
                    <bdo id='xXVhg'></bdo><ul id='xXVhg'></ul>

                  1. <tfoot id='xXVhg'></tfoot>
                      <i id='xXVhg'><tr id='xXVhg'><dt id='xXVhg'><q id='xXVhg'><span id='xXVhg'><b id='xXVhg'><form id='xXVhg'><ins id='xXVhg'></ins><ul id='xXVhg'></ul><sub id='xXVhg'></sub></form><legend id='xXVhg'></legend><bdo id='xXVhg'><pre id='xXVhg'><center id='xXVhg'></center></pre></bdo></b><th id='xXVhg'></th></span></q></dt></tr></i><div id='xXVhg'><tfoot id='xXVhg'></tfoot><dl id='xXVhg'><fieldset id='xXVhg'></fieldset></dl></div>