<small id='5YAiw'></small><noframes id='5YAiw'>

  • <legend id='5YAiw'><style id='5YAiw'><dir id='5YAiw'><q id='5YAiw'></q></dir></style></legend>

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

      1. <tfoot id='5YAiw'></tfoot>
          <bdo id='5YAiw'></bdo><ul id='5YAiw'></ul>

        如何使用 cmake 创建共享库?

        How to create a shared library with cmake?(如何使用 cmake 创建共享库?)

        1. <legend id='SovSg'><style id='SovSg'><dir id='SovSg'><q id='SovSg'></q></dir></style></legend>

                <bdo id='SovSg'></bdo><ul id='SovSg'></ul>
                  <tbody id='SovSg'></tbody>

              • <small id='SovSg'></small><noframes id='SovSg'>

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

                  本文介绍了如何使用 cmake 创建共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经写了一个库,我曾经使用自己编写的 Makefile 进行编译,但现在我想切换到 cmake.树看起来像这样(我删除了所有不相关的文件):

                  <预><代码>.├── 包括│ ├── animation.h│ ├── buffers.h│ ├── ...│ ├── vertex.h│ └── world.h└── src├──动画.cpp├──缓冲区.cpp├── ...├── 顶点.cpp└── world.cpp

                  所以我想要做的只是将源代码编译成共享库,然后与头文件一起安装.

                  我发现的大多数示例都使用一些共享库编译可执行文件,但绝不仅仅是一个普通的共享库.如果有人能告诉我一个使用 cmake 的非常简单的库也会很有帮助,所以我可以以此为例.

                  解决方案

                  始终指定 cmake

                  的最低要求版本

                  cmake_minimum_required(VERSION 3.9)

                  您应该声明一个项目.cmake 说这是强制性的,它将定义方便的变量 PROJECT_NAMEPROJECT_VERSIONPROJECT_DESCRIPTION(后一个变量需要 cmake3.9):

                  project(mylib 版本 1.0.1 描述mylib 描述")

                  声明一个新的库目标.请避免使用file(GLOB ...).此功能不提供编译过程的有人参与的掌握.如果你懒惰,复制粘贴 ls -1 sources/*.cpp 的输出:

                  add_library(mylib 共享来源/动画.cpp源/缓冲区.cpp[...])

                  设置 VERSION 属性(可选,但这是一个好习惯):

                  set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION})

                  您还可以将SOVERSION 设置为VERSION 的主要编号.所以 libmylib.so.1 将是 libmylib.so.1.0.0 的符号链接.

                  set_target_properties(mylib PROPERTIES SOVERSION 1)

                  声明您的库的公共 API.将为第三方应用程序安装此 API.将它隔离在您的项目树中是一个很好的做法(比如将它放在 include/ 目录中).请注意,不应安装私有头文件,我强烈建议将它们与源文件放在一起.

                  set_target_properties(mylib PROPERTIES PUBLIC_HEADER include/mylib.h)

                  如果您使用子目录,则包含诸如 ../include/mylib.h" 之类的相对路径不是很方便.因此,在包含的目录中传递一个顶级目录:

                  target_include_directories(mylib PRIVATE .)

                  target_include_directories(mylib PRIVATE include)target_include_directories(mylib 私有源代码)

                  为您的库创建安装规则.我建议使用 GNUInstallDirs 中定义的变量 CMAKE_INSTALL_*DIR:

                  include(GNUInstallDirs)

                  并声明要安装的文件:

                  安装(目标 mylib图书馆目的地 ${CMAKE_INSTALL_LIBDIR}PUBLIC_HEADER 目的地 ${CMAKE_INSTALL_INCLUDEDIR})

                  您也可以导出 pkg-config 文件.此文件允许第三方应用程序轻松导入您的库:

                  • 使用 Makefile,参见 pkg-config
                  • 对于 Autotools,请参阅 PKG_CHECK_MODULES
                  • 使用 cmake,请参阅 pkg_check_modules

                  创建一个名为 mylib.pc.in 的模板文件(参见 pc(5) 手册页了解更多信息):

                  prefix=@CMAKE_INSTALL_PREFIX@exec_prefix=@CMAKE_INSTALL_PREFIX@libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@includeir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@姓名:@PROJECT_NAME@描述:@PROJECT_DESCRIPTION@版本:@PROJECT_VERSION@要求:库:-L${libdir} -lmylibCflags: -I${includedir}

                  在你的 CMakeLists.txt 中,添加一个规则来扩展 @ 宏(@ONLY 要求 cmake 不扩展表单的变量${VAR}):

                  configure_file(mylib.pc.in mylib.pc @ONLY)

                  最后,安装生成的文件:

                  install(FILES ${CMAKE_BINARY_DIR}/mylib.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

                  您也可以使用 cmake <代码>导出功能.不过这个功能只兼容cmake,我觉得很难用.

                  最后整个 CMakeLists.txt 应该看起来像:

                  cmake_minimum_required(VERSION 3.9)项目(mylib 版本 1.0.1 描述mylib 描述")包括(GNUInstallDirs)add_library(mylib 共享 src/mylib.c)set_target_properties(mylib 属性版本 ${PROJECT_VERSION}SOVERSION 1PUBLIC_HEADER api/mylib.h)配置文件(mylib.pc.in mylib.pc @ONLY)target_include_directories(mylib PRIVATE .)安装(目标 mylib图书馆目的地 ${CMAKE_INSTALL_LIBDIR}PUBLIC_HEADER 目的地 ${CMAKE_INSTALL_INCLUDEDIR})安装(文件 ${CMAKE_BINARY_DIR}/mylib.pc目的地 ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

                  编辑

                  正如评论中提到的,为了符合标准,您应该能够生成静态库和共享库.这个过程有点复杂,与最初的问题不符.但值得一提的是,它在这里有很好的解释.

                  I have written a library that I used to compile using a self-written Makefile, but now I want to switch to cmake. The tree looks like this (I removed all the irrelevant files):

                  .
                  ├── include
                  │ ├── animation.h
                  │ ├── buffers.h
                  │ ├── ...
                  │ ├── vertex.h
                  │ └── world.h
                  └── src
                      ├── animation.cpp
                      ├── buffers.cpp
                      ├── ...
                      ├── vertex.cpp
                      └── world.cpp
                  

                  So what I am trying to do is just to compile the source into a shared library and then install it with the header files.

                  Most examples that I have found compile executables with some shared libraries but never just a plain shared library. It would also be helpful if someone could just tell me a very simple library that uses cmake, so I can use this as an example.

                  解决方案

                  Always specify the minimum required version of cmake

                  cmake_minimum_required(VERSION 3.9)
                  

                  You should declare a project. cmake says it is mandatory and it will define convenient variables PROJECT_NAME, PROJECT_VERSION and PROJECT_DESCRIPTION (this latter variable necessitate cmake 3.9):

                  project(mylib VERSION 1.0.1 DESCRIPTION "mylib description")
                  

                  Declare a new library target. Please avoid the use of file(GLOB ...). This feature does not provide attended mastery of the compilation process. If you are lazy, copy-paste output of ls -1 sources/*.cpp :

                  add_library(mylib SHARED
                      sources/animation.cpp
                      sources/buffers.cpp
                      [...]
                  )
                  

                  Set VERSION property (optional but it is a good practice):

                  set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION})
                  

                  You can also set SOVERSION to a major number of VERSION. So libmylib.so.1 will be a symlink to libmylib.so.1.0.0.

                  set_target_properties(mylib PROPERTIES SOVERSION 1)
                  

                  Declare public API of your library. This API will be installed for the third-party application. It is a good practice to isolate it in your project tree (like placing it include/ directory). Notice that, private headers should not be installed and I strongly suggest to place them with the source files.

                  set_target_properties(mylib PROPERTIES PUBLIC_HEADER include/mylib.h)
                  

                  If you work with subdirectories, it is not very convenient to include relative paths like "../include/mylib.h". So, pass a top directory in included directories:

                  target_include_directories(mylib PRIVATE .)
                  

                  or

                  target_include_directories(mylib PRIVATE include)
                  target_include_directories(mylib PRIVATE src)
                  

                  Create an install rule for your library. I suggest to use variables CMAKE_INSTALL_*DIR defined in GNUInstallDirs:

                  include(GNUInstallDirs)
                  

                  And declare files to install:

                  install(TARGETS mylib
                      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                      PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
                  

                  You may also export a pkg-config file. This file allows a third-party application to easily import your library:

                  • with Makefile, see pkg-config
                  • with Autotools, see PKG_CHECK_MODULES
                  • with cmake, see pkg_check_modules

                  Create a template file named mylib.pc.in (see pc(5) manpage for more information):

                  prefix=@CMAKE_INSTALL_PREFIX@
                  exec_prefix=@CMAKE_INSTALL_PREFIX@
                  libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
                  includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
                  
                  Name: @PROJECT_NAME@
                  Description: @PROJECT_DESCRIPTION@
                  Version: @PROJECT_VERSION@
                  
                  Requires:
                  Libs: -L${libdir} -lmylib
                  Cflags: -I${includedir}
                  

                  In your CMakeLists.txt, add a rule to expand @ macros (@ONLY ask to cmake to not expand variables of the form ${VAR}):

                  configure_file(mylib.pc.in mylib.pc @ONLY)
                  

                  And finally, install generated file:

                  install(FILES ${CMAKE_BINARY_DIR}/mylib.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
                  

                  You may also use cmake EXPORT feature. However, this feature is only compatible with cmake and I find it difficult to use.

                  Finally the entire CMakeLists.txt should looks like:

                  cmake_minimum_required(VERSION 3.9)
                  project(mylib VERSION 1.0.1 DESCRIPTION "mylib description")
                  include(GNUInstallDirs)
                  add_library(mylib SHARED src/mylib.c)
                  set_target_properties(mylib PROPERTIES
                      VERSION ${PROJECT_VERSION}
                      SOVERSION 1
                      PUBLIC_HEADER api/mylib.h)
                  configure_file(mylib.pc.in mylib.pc @ONLY)
                  target_include_directories(mylib PRIVATE .)
                  install(TARGETS mylib
                      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                      PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
                  install(FILES ${CMAKE_BINARY_DIR}/mylib.pc
                      DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
                  

                  EDIT

                  As mentioned in comments, to comply with standards you should be able to generate a static library as well as a shared library. The process is bit more complex and does not match with the initial question. But it worths to mention that it is greatly explained here.

                  这篇关于如何使用 cmake 创建共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  <legend id='i9J1x'><style id='i9J1x'><dir id='i9J1x'><q id='i9J1x'></q></dir></style></legend>
                  <tfoot id='i9J1x'></tfoot>
                  • <bdo id='i9J1x'></bdo><ul id='i9J1x'></ul>

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

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

                              <tbody id='i9J1x'></tbody>