CMAKE - 如何将静态库的头文件正确复制到/usr/include 中? [英] CMAKE - How to properly copy static library's header file into /usr/include?

查看:95
本文介绍了CMAKE - 如何将静态库的头文件正确复制到/usr/include 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 C 来使用 CMAKE,实际上我正在创建两个非常小的静态库.

I'm getting into CMAKE usage with C and actually I'm creating two very small static libraries.

我的目标是:

  1. 库被编译并链接到 *.a 文件中.[这作品]
  2. 然后我希望将 *.a 文件复制到/usr/local/lib [这也有效]
  3. 就我对库的了解(很少),它们使用 -lnameoflib 链接,这是一个编译器标志.行.我已经准备好了我的 CMakeLists.txt,它实际上将 *.a 文件复制到 /usr/local/lib 中.但是,为了能够在程序中使用它们,我还需要将它们的头文件复制到 /usr/local/include 中,然后我可以通过简单的方式包含它们 #include #include <mylibheader.h>.我现在就是这么理解的.
  1. The libraries are compiled and linked into *.a files. [THIS WORKS]
  2. Then I wish to copy that *.a files into /usr/local/lib [THIS ALSO WORKS]
  3. As far as I know about libraries (very little), they are linked using -lnameoflib, which is a compiler flag. OK. I have prepared my CMakeLists.txt and it actually copies *.a files into /usr/local/lib. However, to be able to use them in a program, I also need to copy their header files into /usr/local/include, then I can include them the easy way #include <mylibheader.h>. That's how I understand it now.

我的问题是 - 使用 CMAKE 将头文件复制到/usr/include 文件夹的正确方法是什么?我希望它在执行 make install 时自动复制它们,就像 *.a 文件一样.

And my question is - how is the proper way of copying header files into /usr/include folder with CMAKE? I would like it to copy them automatically when make install is executed, like *.a files are.

对于这两个库,我都有一个类似的 CMakeLists.txt:

For both of the libraries I have a smiliar CMakeLists.txt:

project(programming-network)

add_library(programming-network STATIC
    send_string.c
    recv_line.c
    )

INSTALL(TARGETS programming-network
        DESTINATION "lib"
        )

推荐答案

最新的 cmake 版本更好的方法是使用目标的 PUBLIC_HEADER 属性.

A better way for newest cmake version is to use target's PUBLIC_HEADER properties.

project(myproject)

add_library(mylib some.c another.c)
set_target_properties(mylib PROPERTIES PUBLIC_HEADER "some.h;another.h")
INSTALL(TARGETS mylib 
        LIBRARY DESTINATION some/libpath
        PUBLIC_HEADER DESTINATION some/includepath
)

一些参考:

PUBLIC_HEADER

CMake 安装命令

这篇关于CMAKE - 如何将静态库的头文件正确复制到/usr/include 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆