将静态库与c ++/cmake链接 [英] Linking static libraries with c++/cmake

查看:87
本文介绍了将静态库与c ++/cmake链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用firebreath框架在程序中链接库.

I try to link libraries in my program using firebreath framework.

所以我将此代码添加到projectDef.cmake中.

So I add this code in 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文件中包含头文件.

Why I add 'include_directories' is I include header file in my .cpp file.

#include <giblib.h>
#include <Xlib.h>

执行 make 后,会出现此错误消息.

After execute make, this error message appears.

/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'

但我不明白为什么.

如何在程序中链接静态库?

What should I do for linking static libraries in my program?

推荐答案

我认为您误解了静态库的创建.静态库是目标文件的集合,它没有任何init/deinit代码可插入其他库,例如共享库或可执行文件.

I think you are misunderstanding the creation of a static library. A static library is a collection of object files, it does not have any init/deinit code to pull in additional libraries like a shared library or an executable can.

如果您创建一个 libraryA ,而该 libraryA 使用构建 applicationX 时使用 libraryB libraryC 中的代码em> libraryA ,您必须手动插入 libraryB libraryC .对于共享库,这不是必需的.

If you create a libraryA that uses code from libraryB and libraryC when you build applicationX that uses libraryA you must manually pull in both libraryB and libraryC. With shared libraries this is not necessary.

如果您调查 pkg-config ,则可以看到属性 Libs.private ,该属性指定了这些其他 private 或静态链接的内部库会需要.

If you investigate pkg-config you can see the property Libs.private that specifies these additional private or internal libraries that a static link would require.

这篇关于将静态库与c ++/cmake链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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