繁体   English   中英

将 static 库与 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”是我在 my.cpp 文件中包含 header 文件。

#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' 引起的

但我不明白为什么。

在我的程序中链接 static 库应该怎么做?

我认为您误解了 static 库的创建。 static 库是 object 文件的集合,它没有任何 init/deinit 代码来拉入其他库,如共享库或可执行文件罐。

如果您在构建使用libraryAapplicationX时创建了一个使用libraryBlibraryC代码的libraryA ,则必须手动拉入libraryBlibraryC 对于共享库,这不是必需的。

如果您调查pkg-config ,您可以看到属性Libs.private ,它指定了 static 链接需要的这些额外的私有或内部库。

暂无
暂无

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

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