编译器找不到jpeg和png库



我已经下载并安装了这个用于绘图和可视化的C++包https://github.com/alandefreitas/matplotplusplus.我已经在我的CMake文件中成功地链接到了它,并且在包含#include <matplot/matplot.h>时没有链接错误。然而,每当我使用matplot.h库中的任何函数时,在编译时都会出现以下错误。

/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::_cimg_jpeg_error_exit(jpeg_common_struct*)':
common.cpp:(.text._ZN12cimg_library4CImgIhE21_cimg_jpeg_error_exitEP18jpeg_common_struct[_ZN12cimg_library4CImgIhE21_cimg_jpeg_error_exitEP18jpeg_common_struct]+0x1e): undefined reference to `jpeg_destroy'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::_save_jpeg(_IO_FILE*, char const*, unsigned int) const':
common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x12b): undefined reference to `jpeg_std_error'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x152): undefined reference to `jpeg_CreateCompress'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x16d): undefined reference to `jpeg_stdio_dest'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x198): undefined reference to `jpeg_set_defaults'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x1b2): undefined reference to `jpeg_set_quality'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x1c6): undefined reference to `jpeg_start_compress'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x2cf): undefined reference to `jpeg_write_scanlines'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x2ed): undefined reference to `jpeg_finish_compress'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE10_save_jpegEP8_IO_FILEPKcj]+0x302): undefined reference to `jpeg_destroy_compress'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::_save_png(_IO_FILE*, char const*, unsigned int) const':
common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x2f4): undefined reference to `png_create_write_struct'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x309): undefined reference to `png_create_info_struct'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x32b): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x34f): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x409): undefined reference to `png_init_io'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x474): undefined reference to `png_set_IHDR'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x485): undefined reference to `png_write_info'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0xc36): undefined reference to `png_write_image'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0xc43): undefined reference to `png_write_end'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0xc50): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: common.cpp:(.text._ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj[_ZNK12cimg_library4CImgIhE9_save_pngEP8_IO_FILEPKcj]+0x1830): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::save_cimg(char const*, bool) const':
common.cpp:(.text._ZNK12cimg_library4CImgIhE9save_cimgEPKcb[_ZNK12cimg_library4CImgIhE9save_cimgEPKcb]+0x3bb): undefined reference to `compress'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::_load_png(_IO_FILE*, char const*, unsigned int*)':
common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x88): undefined reference to `png_sig_cmp'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0xa2): undefined reference to `png_create_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0xb7): undefined reference to `png_create_info_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0xcd): undefined reference to `png_create_info_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0xef): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x14a): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x1c9): undefined reference to `png_init_io'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x1d7): undefined reference to `png_set_sig_bytes'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x1e4): undefined reference to `png_read_info'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x20e): undefined reference to `png_get_IHDR'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x252): undefined reference to `png_get_valid'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x280): undefined reference to `png_set_filler'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x28d): undefined reference to `png_read_update_info'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x2c2): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x369): undefined reference to `png_set_expand_gray_1_2_4_to_8'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x389): undefined reference to `png_set_gray_to_rgb'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x3a9): undefined reference to `png_set_tRNS_to_alpha'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x41f): undefined reference to `png_read_image'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x42c): undefined reference to `png_read_end'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x461): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x4f1): undefined reference to `png_set_palette_to_rgb'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x683): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x84a): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj[_ZN12cimg_library4CImgIhE9_load_pngEP8_IO_FILEPKcPj]+0x9d3): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImgList<unsigned char>::_load_cimg(_IO_FILE*, char const*)':
common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0x404): undefined reference to `uncompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0x70c): undefined reference to `uncompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0x93b): undefined reference to `uncompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0xce5): undefined reference to `uncompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0x10f7): undefined reference to `uncompress'
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o):common.cpp:(.text._ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc[_ZN12cimg_library8CImgListIhE10_load_cimgEP8_IO_FILEPKc]+0x1506): more undefined references to `uncompress' follow
/usr/bin/ld: /usr/local/lib/libmatplot.a(common.cpp.o): in function `cimg_library::CImg<unsigned char>::_load_jpeg(_IO_FILE*, char const*)':
common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0x67): undefined reference to `jpeg_std_error'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0xb3): undefined reference to `jpeg_CreateDecompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0xc5): undefined reference to `jpeg_stdio_src'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0xd2): undefined reference to `jpeg_read_header'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0xda): undefined reference to `jpeg_start_decompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0x1f8): undefined reference to `jpeg_read_scanlines'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0x278): undefined reference to `jpeg_finish_decompress'
/usr/bin/ld: common.cpp:(.text._ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc[_ZN12cimg_library4CImgIhE10_load_jpegEP8_IO_FILEPKc]+0x284): undefined reference to `jpeg_destroy_decompress'
/usr/bin/ld: /usr/local/lib/libmatplot.a(network.cpp.o): in function `matplot::network::process_force_layout()':
network.cpp:(.text+0x304e): undefined reference to `nodesoup::size_radiuses(std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, double, double)'
/usr/bin/ld: network.cpp:(.text+0x312a): undefined reference to `nodesoup::fruchterman_reingold(std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, unsigned int, unsigned int, unsigned int, double, std::function<void (std::vector<nodesoup::Point2D, std::allocator<nodesoup::Point2D> > const&, int)>)'
/usr/bin/ld: /usr/local/lib/libmatplot.a(network.cpp.o): in function `matplot::network::process_kawai_layout()':
network.cpp:(.text+0x3842): undefined reference to `nodesoup::size_radiuses(std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, double, double)'
/usr/bin/ld: network.cpp:(.text+0x390c): undefined reference to `nodesoup::kamada_kawai(std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, unsigned int, unsigned int, double, double)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/example.dir/build.make:84: example] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

错误很多,但除了undefined reference to 'uncompress'之外,它们都是undefined reference to png_...undefined reference to jpeg_...的形式。CMake似乎找不到png和jpeg。我确实安装了libjpeg和libpng。为什么会出现这些错误?我需要在CMakeLists.txt中添加行来查找jpeg和png吗?我运行的是Ubuntu 20.04、g++9.3.0和cmake 3.16.3。

下面是一个非常简单的例子,它会引发错误。这是cmake文件

cmake_minimum_required(VERSION 3.10)
project(example)
set(CMAKE_CXX_COMPILER "c++")
find_package(Matplot++)
add_executable(example example.cpp)
target_link_libraries(example PUBLIC matplot)

下面是一个示例c++代码(直接从链接站点上的示例中提取(。

#include <cmath>
#include <matplot/matplot.h>
int main() {
using namespace matplot;
std::vector<double> x = linspace(0, 10, 150);
std::vector<double> y = transform(x, [](auto x) { return cos(5 * x); });
plot(x, y)->color({0, 0.7, 0.9});
title("2-D Line Plot");
xlabel("x");
ylabel("cos(5x)");
show();
return 0;
}

我也遇到了同样的问题(使用Windows MinGW(。感谢@drescherjm提示(添加选项-DBUILD_SHARED_LIBS=ON(,以下是我用来解决问题的步骤:

git clone http://github.com/alandefreitas/matplotplusplus
cd matplotplusplus/
mkdir compil-release && cd compil-release 
rm CMakeCache.txt # if already exists
cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX="${YOUR_LIB_PATH}/matplot1.0.1" 
make -j4 && make install

然后,我想这是一个错误,我不得不手动将.dll复制到安装路径

cp source/matplot/libmatplot.dll ${YOUR_LIB_PATH}/matplot1.0.1/lib/

希望这对你有用。

最新更新