简体   繁体   English

创建在Linux中使用opencv的C ++动态库

[英]creating C++ dynamic library which uses opencv in linux

I am trying to create a shared library in linux fro a program which uses opencv and tesseract with dynamic linking 我正在尝试通过使用opencv和tesseract与动态链接的程序在linux中创建共享库

I followed link My code is as follows 点击了链接我的代码如下

g++ -c Serial_Key.cpp -fPIC -o cdserial `pkg-config --cflags --libs opencv` -llept -ltesseract
g++ -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 cdserial
ln -sf libctest.so.1.0 libctest.so
ln -sf libctest.so.1.0 libctest.so.1
g++ -c Test.cpp -fPIC -o cprog -lctest `pkg-config --cflags --libs opencv` -llept -ltesseract

Here Test.cpp is a simple file as follows 这里的Test.cpp是一个简单的文件,如下所示

#include <stdio.h>
int Serial_key();
int main(){
int x=Serial_key();
printf("Success");
return 0;}

somehow its giving error for ./cprog as ./cprog: cannot execute binary file: Exec format error 以某种方式将其作为./cprog的给定错误为./cprog:无法执行二进制文件:Exec格式错误

I feel that i am doing some fundamental mistake at 2nd line (g++ -shared) Please guide 我觉得我在第二行(g ++ -shared)犯了一些基本错误,请指导

After a little bit more head crunching, i found several silly mistakes in my above mentioned problem 经过更多的头部仰卧起坐后,我发现上述问题中存在一些愚蠢的错误

Here is the corrected flow for others who may get stuck in similar problem 这是针对可能陷入类似问题的其他人的更正流程

  1. First compile 第一次编译

    g++ -c Serial_Key.cpp -fPIC -o cdserial g ++ -c Serial_Key.cpp -fPIC -o cdserial

  2. Create Shared library by mentioning libraries and their path with soname 通过使用soname提及库及其路径来创建共享库

    g++ -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 cdserial -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_imgcodecs -lopencv_videoio -llept -ltesseract -L/usr/local/lib g ++ -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 cdserial -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_imgcodecs -lopencv_videoio -llept -ltesseract -L / usr / local / lib

  3. Link soname with library 将soname与库链接

    ln -sf libctest.so.1.0 libctest.so ln -sf libctest.so.1.0 libctest.so.1 ln -sf libctest.so.1.0 libctest.so ln -sf libctest.so.1.0 libctest.so.1

  4. Compile and create object for Test File 编译并创建测试文件的对象

    g++ Test.cpp -fPIC -o cprog -lctest -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_imgcodecs -lopencv_videoio -llept -ltesseract -L/usr/local/lib g ++ Test.cpp -fPIC -o cprog -lctest -lopencv_highgui -lopencv_imgproc -lopencv_core -lopencv_imgcodecs -lopencv_videoio -llept -ltesseract -L / usr / local / lib

  5. Copy share library files to local lib 将共享库文件复制到本地库

    cp libctest.so /usr/local/lib cp libctest.so.1 /usr/local/lib cp libctest.so.1.0 /usr/local/lib cp libctest.so / usr / local / lib cp libctest.so.1 / usr / local / lib cp libctest.so.1.0 / usr / local / lib

  6. Ensure $LD_LIBRARY_PATH is pointing to shared library path 确保$ LD_LIBRARY_PATH指向共享库路径

    export LD_LIBRARY_PATH=/usr/local/lib 导出LD_LIBRARY_PATH = / usr / local / lib

  7. Run

    ./cprog ./cprog

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

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