用c++/cmake链接静态库
Linking static libraries with c++/cmake
我尝试使用firebreath框架在程序中链接库。
所以我在projectDef.cmake中添加了这段代码。
include_directories(/usr/include/giblib)
include_directories(/usr/include/X11)
add_library(giblib_ptm STATIC IMPORTED)
set_property(TARGET giblib_ptm PROPERTY IMPORTED_LOCATION /usr/lib/libgiblib.a)
add_library(X11_ptm STATIC IMPORTED)
set_property(TARGET X11_ptm PROPERTY IMPORTED_LOCATION /usr/lib/i386-linux-gnu/libX11.a)
add_library(Imlib_ptm STATIC IMPORTED)
set_property(TARGET Imlib_ptm PROPERTY IMPORTED_LOCATION /usr/lib/libImlib2.a)
target_link_libraries(Printmade2 giblib_ptm X11_ptm Imlib_ptm)
为什么我添加'include_directories'是我在我的。cpp文件中包含头文件。
#include <giblib.h>
#include <Xlib.h>
执行make后,出现此错误信息。
/usr/lib/i386-linux-gnu/libX11.a(OpenDis.o): In function `OutOfMemory':
(.text+0x459): undefined reference to `xcb_disconnect'
/usr/lib/i386-linux-gnu/libX11.a(OpenDis.o): In function `XOpenDisplay':
(.text+0x8f5): undefined reference to `xcb_get_setup'
/usr/lib/i386-linux-gnu/libX11.a(OpenDis.o): In function `XOpenDisplay':
(.text+0xedb): undefined reference to `xcb_get_maximum_request_length'
/usr/lib/i386-linux-gnu/libX11.a(xcb_disp.o): In function `_XConnectXCB':
(.text+0x176): undefined reference to `xcb_parse_display'
/usr/lib/i386-linux-gnu/libX11.a(xcb_disp.o): In function `_XConnectXCB':
(.text+0x1d7): undefined reference to `xcb_connect_to_display_with_auth_info'
...
/usr/lib/i386-linux-gnu/libX11.a(xcb_io.o): In function `poll_for_event':
(.text+0x30e): undefined reference to `xcb_poll_for_event'
/usr/lib/i386-linux-gnu/libX11.a(xcb_io.o): In function `poll_for_response':
(.text+0x6b4): undefined reference to `xcb_poll_for_reply'
/usr/lib/i386-linux-gnu/libX11.a(xcb_io.o): In function `_XSend':
(.text+0x85f): undefined reference to `xcb_writev'
/usr/lib/i386-linux-gnu/libX11.a(xcb_io.o): In function `_XReadEvents':
(.text+0xa1f): undefined reference to `xcb_connection_has_error'
....
我认为这个错误是由'add_library'和'set_property',
引起的但我不明白为什么。
我应该如何在我的程序中链接静态库?
我认为您误解了静态库的创建。静态库是目标文件的集合,它没有任何init/deinit代码来引入额外的库,如共享库或可执行库。
如果你创建了一个libraryB和libraryC中的代码的libraryB和libraryC,当你构建applicationX使用libraryB和libraryC时,你必须手动拉入libraryB和libraryC。对于共享库,这是不需要的。
如果你研究pkg-config
,你可以看到属性Libs.private
指定了这些额外的私有或静态链接所需的内部库。
相关文章:
- 基于boost的程序的静态链接——zlib问题
- 使用gcc从静态链接的文件中查找可选符号
- 使用转换器提升 Python 问题 - 静态链接
- C++实用程序::转换在静态链接库中不起作用
- 将源中的 GLEW 与 cmake 静态链接
- 在发布模式下启动使用库的静态链接编译的应用程序时出错
- libavdevice 和 x11 的静态链接库应该以什么顺序链接?
- 静态链接启用 PIC 的库时出现链接器 PIC 错误
- 静态链接库时,收到链接器错误:找不到 -lgcc_s
- 静态链接 glibc & boost_python36 for Python extension
- 我可以在C++中配置静态链接吗?
- 协议缓冲区的静态链接会导致与现有符号冲突
- C++标准是否定义了结构中成员函数的函数内定义是否必须具有静态链接?
- 静态链接 Visual Studio dll 到动态链接的 sfml 项目
- 我可以加载与库静态链接的 dll 吗?
- 静态链接MSVC库,动态链接Qt
- C++:找不到静态链接
- OpenSSL AES仅适用于某些计算机,即使具有静态链接也是如此
- CURL 静态链接未解析的外部符号
- 如何强制Windows.h在Visual Studio中静态链接而不是动态链接?