I'm getting into CMAKE usage with C and actually I'm creating two very small static libraries.
我正在使用C语言,实际上我正在创建两个非常小的静态库。
My goal is:
我的目标是:
- The libraries are compiled and linked into *.a files. [THIS WORKS]
- 这些库被编译并链接到*。一个文件。(这是)
- Then I wish to copy that *.a files into /usr/local/lib [THIS ALSO WORKS]
- 然后我想复制那个*。一个文件进入/usr/local/lib[这也起作用]
- 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/include, then I can include them the easy way#include <mylibheader.h>
. That's how I understand it now. -
就我所知的库(非常少),它们使用-lnameoflib链接,这是一个编译器标志。好的。我已经准备好了我的CMakeLists。txt和它实际上复制*。一个文件到/usr/local/lib.但是,为了能够在程序中使用它们,我还需要将它们的头文件复制到/usr/include中,然后我可以将它们包含在简单的方法中,包括
。这就是我现在的理解。
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.
我的问题是,如何正确地将头文件复制到/usr/include文件夹和CMAKE?我希望在执行安装时自动复制它们,比如*。一个文件。
For both of the libraries I have a smiliar CMakeLists.txt:
对于这两个库,我都有一个smiliar CMakeLists.txt:
project(programming-network)
add_library(programming-network STATIC
send_string.c
recv_line.c
)
INSTALL(TARGETS programming-network
DESTINATION "lib"
)
SOLUTION
Thanks to the help of comments, I have found a solution (very easy). Now my CMakeLists looks like this:
由于有了评论的帮助,我找到了一个解决方案(非常简单)。现在我的CMakeLists看起来是这样的:
project(programming-network)
add_library(programming-network STATIC
send_string.c
recv_line.c
)
INSTALL(TARGETS programming-network
DESTINATION "lib"
)
INSTALL(FILES programming-network.h
DESTINATION "../include"
)
3 个解决方案
#1
15
A better way for newest cmake version is to use target's PUBLIC_HEADER
properties.
最新的cmake版本的更好的方法是使用目标的PUBLIC_HEADER属性。
project(myproject)
add_library(myproject some.c another.c)
set_target_properties(myproject PROPERTIES PUBLIC_HEADER "some.h;another.h")
INSTALL(TARGETS myproject
LIBRARY DESTINATION some/libpath
PUBLIC_HEADER DESTINATION some/includepath
)
Some ref:
一些参考:
PUBLIC_HEADER
CMake安装命令
#2
5
I don't think your solution is the correct one. /usr/include
should be reserved for your vendor to put files in.
我认为你的解决方案不正确。/usr/include应该保留给供应商,以便将文件放入。
The proper thing to do IMO is to install the header in /usr/local/include
and then instruct the user to export CPATH="/usr/local/include:${CPATH}"
.
正确的做法是在/usr/local/include中安装header,然后指示用户导出CPATH="/usr/local/include:${CPATH}"。
It seems /usr/local/lib
was search automatically but if you wish to use another dir export LIBRARY_PATH="/usr/local/lib:${LIBRARY_PATH}"
works similar for the .a binary (but may or may not work good for shared libraries depending on your os).
看起来/usr/local/lib是自动搜索的,但是如果您想要使用另一个dir导出LIBRARY_PATH="/usr/local/lib:${LIBRARY_PATH}"的工作类似于一个二进制文件(但是根据您的操作系统,可以或可能不适合共享库)。
Optionally, but more cumbersome is to add -I /usr/local/include
and -L /usr/local/lib
while compiling.
但更麻烦的是在编译时添加-I /usr/local/include和-L /usr/local/lib。
This is a somewhat subjective answer, but it's been working well for me.
这是一个有点主观的回答,但对我来说已经很好了。
#3
5
In a much better way, will copy all files that match the pattern and will preserve the directory structure.
以一种更好的方式,将复制与模式匹配的所有文件,并保存目录结构。
INSTALL (
DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION include
FILES_MATCHING PATTERN "*.h*")
#1
15
A better way for newest cmake version is to use target's PUBLIC_HEADER
properties.
最新的cmake版本的更好的方法是使用目标的PUBLIC_HEADER属性。
project(myproject)
add_library(myproject some.c another.c)
set_target_properties(myproject PROPERTIES PUBLIC_HEADER "some.h;another.h")
INSTALL(TARGETS myproject
LIBRARY DESTINATION some/libpath
PUBLIC_HEADER DESTINATION some/includepath
)
Some ref:
一些参考:
PUBLIC_HEADER
CMake安装命令
#2
5
I don't think your solution is the correct one. /usr/include
should be reserved for your vendor to put files in.
我认为你的解决方案不正确。/usr/include应该保留给供应商,以便将文件放入。
The proper thing to do IMO is to install the header in /usr/local/include
and then instruct the user to export CPATH="/usr/local/include:${CPATH}"
.
正确的做法是在/usr/local/include中安装header,然后指示用户导出CPATH="/usr/local/include:${CPATH}"。
It seems /usr/local/lib
was search automatically but if you wish to use another dir export LIBRARY_PATH="/usr/local/lib:${LIBRARY_PATH}"
works similar for the .a binary (but may or may not work good for shared libraries depending on your os).
看起来/usr/local/lib是自动搜索的,但是如果您想要使用另一个dir导出LIBRARY_PATH="/usr/local/lib:${LIBRARY_PATH}"的工作类似于一个二进制文件(但是根据您的操作系统,可以或可能不适合共享库)。
Optionally, but more cumbersome is to add -I /usr/local/include
and -L /usr/local/lib
while compiling.
但更麻烦的是在编译时添加-I /usr/local/include和-L /usr/local/lib。
This is a somewhat subjective answer, but it's been working well for me.
这是一个有点主观的回答,但对我来说已经很好了。
#3
5
In a much better way, will copy all files that match the pattern and will preserve the directory structure.
以一种更好的方式,将复制与模式匹配的所有文件,并保存目录结构。
INSTALL (
DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION include
FILES_MATCHING PATTERN "*.h*")