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

      <legend id='1Lb1t'><style id='1Lb1t'><dir id='1Lb1t'><q id='1Lb1t'></q></dir></style></legend>
      1. <small id='1Lb1t'></small><noframes id='1Lb1t'>

        如何创建一个依赖于外部头文件的 cmake 头文件库?

        时间:2023-08-27
        <tfoot id='hUj3Q'></tfoot>

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

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

                  <tbody id='hUj3Q'></tbody>
                <legend id='hUj3Q'><style id='hUj3Q'><dir id='hUj3Q'><q id='hUj3Q'></q></dir></style></legend>
                1. 本文介绍了如何创建一个依赖于外部头文件的 cmake 头文件库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个具有以下文件结构的项目:

                  I have a project with the following file structure:

                  project
                   |
                   |-------> lib1
                   |           |----> lib1.h
                   |
                   |-------> lib2
                   |           |----> lib2.h
                   |
                   |-------> main.cc
                  

                  lib1lib2 两个库只包含头文件,而 lib2.h 包含 lib1.h,和 main.cc 包括 lib2.h.

                  The two libs lib1 and lib2 only contain header files while lib2.h includes lib1.h, and main.cc includes lib2.h.

                  我现在如何为这个项目编写 cmake 文件?我试图为 创建一个 接口库lib2,但是编译器找不到lib1.h.这是我的 cmake 文件的内容:

                  How do I write the cmake file for this project now? I tried to create an interface library for lib2, but the compiler can't find lib1.h. Here are the contents of my cmake files:

                  lib2 的 CMakeLists.txt:

                  add_library(lib2 INTERFACE)
                  target_sources(lib2 INTERFACE lib2.h)
                  target_include_directories(lib2 INTERFACE ../lib1/lib1.h)
                  

                  整个项目的CMakeLists.txt:

                  add_executable(project main.cc)
                  target_link_libraries(project lib2)
                  

                  cmake 文件有什么问题?

                  What's the problem in the cmake files?

                  推荐答案

                  如评论中所述,target_include_directories 应该给出一个目录的路径,而不是文件的路径.

                  As stated in the comments, target_include_directories should be given a path to a directory, not to a file.

                  此外,如果你想在 lib1 上为 lib2 创建一个依赖,你应该通过 target_link_libraries 来做:一个依赖不仅是关于包含目录,还有关于编译选项、定义、目标属性...

                  Moreover, if you want to create a dependency for lib2 on lib1, you should do it through target_link_libraries: a dependency is not only about include directories, but also about compile options, definitions, target properties...

                  target_sources 不适用于接口库.来自 这个答案,您可以使用没有命令的自定义目标将源与目标相关联,而不会影响构建过程(对于msvc、QtCreator 和其他基于 GUI 的工具,这使得可以通过 IDE 访问源代码;AFAIK 它对其他构建工具没用).

                  target_sources doesn't work with interface libraries. From this answer, You can use a custom target without commands to associate the sources to a target without impacting the build process (for msvc, QtCreator and other GUI-based tools, this makes the sources accessible through the IDE; AFAIK it's useless for other build tools).

                  您的 cmake 可能如下所示:

                  Your cmake may look like this:

                  add_library(lib1 INTERFACE)
                  target_sources(lib1 INTERFACE lib1.h)
                  
                  target_include_directories(lib1 INTERFACE
                      "${PROJECT_SOURCE_DIR}/lib1"
                  )
                  
                  add_library(lib2 INTERFACE)
                  if(MSVC)
                      add_custom_target(lib2.headers SOURCES lib2.h)
                  endif()
                  
                  target_include_directories(lib2 INTERFACE
                      "${PROJECT_SOURCE_DIR}/lib2"
                  )
                  
                  target_link_libraries(lib2 INTERFACE lib1)
                  
                  add_executable(project main.cc)
                  target_link_libraries(project lib2)
                  

                  高级提示:您可以在 target_include_directories 中为构建树和安装树指定不同的目录(请参阅 文档):

                  Advanced tip: you can specify a different directory in target_include_directories for the build tree and the install tree (see documentation):

                  target_include_directories(lib1 INTERFACE
                      $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lib1>
                      $<INSTALL_INTERFACE:${YOUR_INSTALL_DIR}/lib1>
                  )
                  

                  这篇关于如何创建一个依赖于外部头文件的 cmake 头文件库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何让 CMake 创建一个 dll 及其匹配的 lib 文件? 下一篇:CMake FIND_PACKAGE 成功但返回错误的路径

                  相关文章

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

                      <bdo id='I6hgG'></bdo><ul id='I6hgG'></ul>

                    <legend id='I6hgG'><style id='I6hgG'><dir id='I6hgG'><q id='I6hgG'></q></dir></style></legend>

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