简体   繁体   中英

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

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

#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

I feel that i am doing some fundamental mistake at 2nd line (g++ -shared) Please guide

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

  2. Create Shared library by mentioning libraries and their path with 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

  3. Link soname with library

    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

  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

  6. Ensure $LD_LIBRARY_PATH is pointing to shared library path

    export LD_LIBRARY_PATH=/usr/local/lib

  7. Run

    ./cprog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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