V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
norkki
V2EX  ›  OpenCV

[求助]C++ 用到 OpenCV 库 怎么静态编译呢?

  •  
  •   norkki · 146 天前 · 608 次点击
    这是一个创建于 146 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问 c++用怎样正确的静态编译呢? 目的是让编译的程序不在依赖 libopencv_*.so 文件

    测试代码如下:

    cv-test.cc

    #include <opencv2/opencv.hpp>
    #include <iostream>
    
    int main(int argc, char** argv) {
            cv::Mat image = cv::imread("image.jpg");
            if (image.empty()) {
                    std::cout << "Error loading image!" << std::endl;
                    return -1;
            }
            // cv::imshow("Image", image);
            std::cout << "size: "
                    << image.cols << "x" << image.rows
                    << std::endl;
            return 0;
    }
    

    c++ -o cv-test cv-test.cc -I/usr/local/opencv/include/opencv4/ -L/usr/local/opencv/lib64/ -lopencv_core -lopencv_imgcodecs

    正常编译

    加上 -static尝试静态编译 (opencv 是有编译静态库的/usr/local/opencv/lib64/libopencv_core.a)

    c++ -o cv-test cv-test.cc -I/usr/local/opencv/include/opencv4/ -L/usr/local/opencv/lib64/ -lopencv_core -lopencv_imgcodecs -static

    报错:

    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(opencl_core.cpp.o): in function `opencl_check_fn(int)':
    /home/nick/github/opencv/modules/core/src/opencl/runtime/opencl_core.cpp:166: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `ipp::IwiImage::Release()':
    /home/nick/github/opencv/build/3rdparty/ippicv/ippicv_lnx/iw/include/iw++/iw_image.hpp:945: undefined reference to `iwAtomic_AddInt'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `ipp::IwiImage::~IwiImage()':
    /home/nick/github/opencv/build/3rdparty/ippicv/ippicv_lnx/iw/include/iw++/iw_image.hpp:813: undefined reference to `iwAtomic_AddInt'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `ipp::IwiImage::Release()':
    /home/nick/github/opencv/build/3rdparty/ippicv/ippicv_lnx/iw/include/iw++/iw_image.hpp:957: undefined reference to `iwiImage_Release'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `ipp::IwException::IwException(int)':
    /home/nick/github/opencv/build/3rdparty/ippicv/ippicv_lnx/iw/include/iw++/iw_core.hpp:133: undefined reference to `iwGetStatusString'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `cv::transpose(cv::_InputArray const&, cv::_OutputArray const&)':
    /home/nick/github/opencv/modules/core/src/matrix_transform.cpp:228: undefined reference to `ippicviTranspose_32f_C4R'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_core.a(matrix_transform.cpp.o): in function `ipp_transpose':
    /home/nick/github/opencv/modules/core/src/matrix_transform.cpp:228: undefined reference to `ippicviTranspose_32s_C3R'
    /usr/bin/ld: /home/nick/github/opencv/modules/core/src/matrix_transform.cpp:228: undefined reference to `ippicviTranspose_16s_C3R'
    
    ...
    
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_imgcodecs.a(grfmt_webp.cpp.o): in function `std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<unsigned char*, void (*)(void*), std::allocator<void>, void>(unsigned char*, void (*)(void*), std::allocator<void>)':
    /usr/include/c++/13/bits/shared_ptr_base.h:958: undefined reference to `WebPFree'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_imgcodecs.a(grfmt_webp.cpp.o): in function `cv::WebPEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
    /home/nick/github/opencv/modules/imgcodecs/src/grfmt_webp.cpp:286: undefined reference to `WebPEncodeLosslessBGRA'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_imgcodecs.a(grfmt_webp.cpp.o): in function `std::_Sp_ebo_helper<0, void (*)(void*), false>::_Sp_ebo_helper(void (*&&)(void*))':
    /usr/include/c++/13/bits/shared_ptr_base.h:482: undefined reference to `WebPFree'
    /usr/bin/ld: /usr/local/opencv/lib64//libopencv_imgcodecs.a(grfmt_webp.cpp.o): in function `cv::WebPEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
    /home/nick/github/opencv/modules/imgcodecs/src/grfmt_webp.cpp:271: undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
    /usr/bin/ld: /home/nick/github/opencv/modules/imgcodecs/src/grfmt_webp.cpp:293: undefined reference to `WebPEncodeBGR'
    /usr/bin/ld: /home/nick/github/opencv/modules/imgcodecs/src/grfmt_webp.cpp:297: undefined reference to `WebPEncodeBGRA'
    /usr/bin/ld: /home/nick/github/opencv/modules/imgcodecs/src/grfmt_webp.cpp:282: undefined reference to `WebPEncodeLosslessBGR'
    /usr/bin/ld: cv-test: hidden symbol `opj_stream_destroy' isn't defined
    /usr/bin/ld: final link failed: bad value
    collect2: error: ld returned 1 exit status
    
    

    请问大佬有没有办法静态编译

    4 条回复    2024-07-18 14:12:59 +08:00
    AEnjoyable
        1
    AEnjoyable  
       146 天前
    虽然我不是做 c++开发的 但你或许可以试试把 OpenCV 的源码一起拉过去编译?生成的 obj 文件直接和你的程序一起编译
    frayesshi1
        2
    frayesshi1  
       146 天前
    全是未定义 reference ,可以反编译看看点 a 文件有没有这些函数
    Hconk
        3
    Hconk  
       146 天前 via iPhone
    看报错你需要加上链接 ipp 和 webp 的库,如果不需要可以重编 opencv 把这俩关掉
    dbpe
        4
    dbpe  
       114 天前
    楼主,最后解决了么?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1573 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:03 · PVG 01:03 · LAX 09:03 · JFK 12:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.