首页 > 解决方案 > 链接到 .a 库时未定义的引用

问题描述

我正在尝试这个库:

https://github.com/dacap/clip

我已经 git 克隆了它,然后使用以下内容构建它:

cmake .
make

这会生成一个名为libclip.a.

现在,我采用了示例 Hello World 代码,如下所示:

#include "clip/clip.h"

int main() {
  clip::set_text("Hello World");
}

鉴于以上内容位于名为cliptest2.cpp的文件中,我将其编译如下:

g++ -lclip cliptest2.cpp -o cliptest2

每次,我都会得到以下信息:

/usr/bin/ld: /tmp/cc71SQmy.o: 在函数“main”中:cliptest2.cpp:(.text+0x47): 未定义引用 `clip::set_text(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)' collect2: 错误: ld 返回 1 个退出状态

我确实了解链接器的工作原理,并且已经解决了许多其他类似错误的问题。但是,鉴于我正在构建第三方库并将其链接到我的示例程序中,我不明白为什么该set_text()方法没有得到解决。

编辑:我尝试了一些事情,也是由于下面的评论。剪辑库的源代码位于~/git/clip,我正在从主目录本身编译 cliptest2.cpp。因此,将@MathiasSchmid 建议的命令调整到我的环境,我最终得到了这个:

g++ cliptest2.cpp -I git -L git/clip -lclip -lxcb -lpthread -o cliptest2

我在我的 Kubuntu 20.04 LTS 上运行它,并得到以下所有错误:

daniel@orion:~$ g++ cliptest2.cpp -I git -L git/clip -lclip -lxcb -lpthread -o cliptest2   
/usr/bin/ld: git/clip/libclip.a(clip_x11.cpp.o): in function `clip::x11::write_data_fn(png_struct_def*, unsigned char*, unsigned long)':
clip_x11.cpp:(.text+0x4f): undefined reference to `png_get_io_ptr'
/usr/bin/ld: git/clip/libclip.a(clip_x11.cpp.o): in function `clip::x11::write_png(clip::image const&, std::vector<unsigned char, std::allocator<unsigned char> >&)':
clip_x11.cpp:(.text+0x117): undefined reference to `png_create_write_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x13a): undefined reference to `png_create_info_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x158): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x17d): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: clip_x11.cpp:(.text+0x1a5): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x1cb): undefined reference to `png_set_write_fn'
/usr/bin/ld: clip_x11.cpp:(.text+0x232): undefined reference to `png_set_IHDR'
/usr/bin/ld: clip_x11.cpp:(.text+0x249): undefined reference to `png_write_info'
/usr/bin/ld: clip_x11.cpp:(.text+0x255): undefined reference to `png_set_packing'
/usr/bin/ld: clip_x11.cpp:(.text+0x268): undefined reference to `png_get_rowbytes'
/usr/bin/ld: clip_x11.cpp:(.text+0x27a): undefined reference to `png_malloc'
/usr/bin/ld: clip_x11.cpp:(.text+0x3cd): undefined reference to `png_write_rows'
/usr/bin/ld: clip_x11.cpp:(.text+0x3e9): undefined reference to `png_free'
/usr/bin/ld: clip_x11.cpp:(.text+0x3fc): undefined reference to `png_write_end'
/usr/bin/ld: clip_x11.cpp:(.text+0x40f): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: git/clip/libclip.a(clip_x11.cpp.o): in function `clip::x11::read_data_fn(png_struct_def*, unsigned char*, unsigned long)':
clip_x11.cpp:(.text+0x45d): undefined reference to `png_get_io_ptr'
/usr/bin/ld: git/clip/libclip.a(clip_x11.cpp.o): in function `clip::x11::read_png(unsigned char const*, unsigned long, clip::image*, clip::image_spec*)':
clip_x11.cpp:(.text+0x575): undefined reference to `png_create_read_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x5a1): undefined reference to `png_create_info_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x5cd): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x5f5): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: clip_x11.cpp:(.text+0x628): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: clip_x11.cpp:(.text+0x68f): undefined reference to `png_set_read_fn'
/usr/bin/ld: clip_x11.cpp:(.text+0x6a8): undefined reference to `png_read_info'
/usr/bin/ld: clip_x11.cpp:(.text+0x6ea): undefined reference to `png_get_IHDR'
/usr/bin/ld: clip_x11.cpp:(.text+0x747): undefined reference to `png_get_rowbytes'
/usr/bin/ld: clip_x11.cpp:(.text+0x8bf): undefined reference to `png_set_strip_16'
/usr/bin/ld: clip_x11.cpp:(.text+0x8ce): undefined reference to `png_set_packing'
/usr/bin/ld: clip_x11.cpp:(.text+0x8dd): undefined reference to `png_set_expand_gray_1_2_4_to_8'
/usr/bin/ld: clip_x11.cpp:(.text+0x8ec): undefined reference to `png_set_palette_to_rgb'
/usr/bin/ld: clip_x11.cpp:(.text+0x8fb): undefined reference to `png_set_gray_to_rgb'
/usr/bin/ld: clip_x11.cpp:(.text+0x90a): undefined reference to `png_set_tRNS_to_alpha'
/usr/bin/ld: clip_x11.cpp:(.text+0x919): undefined reference to `png_set_interlace_handling'
/usr/bin/ld: clip_x11.cpp:(.text+0x938): undefined reference to `png_read_update_info'
/usr/bin/ld: clip_x11.cpp:(.text+0x95a): undefined reference to `png_malloc'
/usr/bin/ld: clip_x11.cpp:(.text+0x9ab): undefined reference to `png_malloc'
/usr/bin/ld: clip_x11.cpp:(.text+0xa19): undefined reference to `png_read_rows'
/usr/bin/ld: clip_x11.cpp:(.text+0xbe0): undefined reference to `png_free'
/usr/bin/ld: clip_x11.cpp:(.text+0xc05): undefined reference to `png_free'
/usr/bin/ld: clip_x11.cpp:(.text+0xc4b): undefined reference to `png_destroy_read_struct'
collect2: error: ld returned 1 exit status

我仍然不清楚这里的问题是什么。

标签: c++linkerstatic-linking

解决方案


推荐阅读