简体   繁体   English

使用CMake包含静态库中的头文件

[英]Include header files from static library with CMake

I have a problem building a CMake project with a static library. 我在使用静态库构建CMake项目时遇到问题。 My project strucutre looks like that: 我的项目strucutre看起来像这样:

Foo/
|-- CMakeLists.txt
|-- lib/
    |-- CMakeLists.txt
    |-- libA/
        |-- CMakeLists.txt
        |-- libA.cpp
        |-- libA.h
        |-- libAB.h
|-- src/
    |-- CMakeLists.txt
    |-- main.cpp
    |-- srcDirA/
        |-- CMakeLists.txt
        |-- srcA.h
    |-- srcDirB/
        |-- CMakeLists.txt
        |-- srcB.cpp
        |-- srcB.h

And the */CMakeLists.txt look like that: * / CMakeLists.txt看起来像这样:

Foo/CMakeLists.txt: Foo / CMakeLists.txt:

cmake_minimum_required(VERSION 3.5.1)
project(FOO)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory(lib)
add_subdirectory(src)

Foo/lib/CMakeLists.txt: Foo / lib / CMakeLists.txt:

add_subdirectory(libA)

Foo/lib/libA/CMakeLists.txt: Foo / lib / libA / CMakeLists.txt:

add_library (staticLibA STATIC libA.cpp)

Foo/src/CMakeLists.txt: Foo / src / CMakeLists.txt:

add_subdirectory(srcDirA)
add_subdirectory(srcDirB)
include_directories(".")

add_executable(foo main.cpp)
target_link_libraries(foo LINK_PUBLIC libA)

Foo/src/srcDirA/CMakeLists.txt is empty Foo / src / srcDirA / CMakeLists.txt为空

Foo/src/srcDirB/CMakeLists.txt is empty Foo / src / srcDirB / CMakeLists.txt为空

Now I am trying to include the header from my static library into my main project like this: 现在,我试图将静态库的标头包含在我的主项目中,如下所示:

Foo/src/main.cpp: Foo / src / main.cpp:

#include "srcDirB/srcB.h"
#include "libA/libA.h"  

int main() {
   //...
   return 0;
}

If I try to build this with CMake, libA is generated but I am getting a fatal error: 如果我尝试使用CMake构建此库,则会生成libA,但出现致命错误:

libA/libA.h: No such file or directory. libA / libA.h:没有这样的文件或目录。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗? Do I have to create a ProjectConfig.cmake file ? 我必须创建一个ProjectConfig.cmake文件吗?

You don't need to create any Config files; 您不需要创建任何配置文件; these are for importing 3rd-party projects. 这些用于导入第三方项目。

Since you're using CMake >= 3.5.1, you can esaily specify usage requirements for your libraries. 由于您使用的是CMake> = 3.5.1,因此可以轻松地指定库的使用要求 Usage requirements are things like flags or include directories which clients need to build with the library. 使用需求是诸如标志之类的内容,或者包括客户端需要使用库构建的目录

So in Foo/lib/libA/CMakeLists.txt:, you'll do this: 因此,在Foo / lib / libA / CMakeLists.txt:中,您将执行以下操作:

add_library (staticLibA STATIC libA.cpp)
target_include_directories(staticLibA INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/..)

Or, if you want the same include directory to apply to libA itself (which is likely), use PUBLIC instead of INTERFACE . 或者,如果您希望同一include目录应用于libA本身(可能),请使用PUBLIC而不是INTERFACE

That's all you really need to do. 这就是您真正需要做的。 However, given the modern CMake you're using, you should replace your use of the legacy keyword LINK_PUBLIC with its modern equivalent PUBLIC . 但是,考虑到您正在使用的现代CMake,您应该将对旧关键字LINK_PUBLIC使用替换为现代等效的PUBLIC

Also, since you mention both CMakeLists in .../srcDir* are empty, why have them there in the first place? 另外,由于您提到.../srcDir*中的两个CMakeLists都是空的,为什么首先将它们放在那里? You can easily get rid of both them and the related add_subdirectory calls. 您可以轻松摆脱它们和相关的add_subdirectory调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM