Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCV compilation as static library [Linux]. #5424

Closed
mrcyberfighter opened this issue Oct 1, 2015 · 6 comments
Closed

OpenCV compilation as static library [Linux]. #5424

mrcyberfighter opened this issue Oct 1, 2015 · 6 comments
Labels
question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org

Comments

@mrcyberfighter
Copy link

I've compiled OpenCV as static library by downloading the source from github.

 $ cmake WITH_GTK=ON WITH_GTK3=ON CMAKE_BUILD_TYPE=RELEASE BUILD_SHARED_LIBS=OFF .

so I get all the libraries as static and try to compile my program according following Makefile:

CXX = g++
CXXFLAGS = -std=c++11 

# Better sort the file order in relationship to `pkg-config --libs opencv` linking chain order.
OPENCV_A = libopencv_calib3d.a \  
    libopencv_contrib.a                    \
    libopencv_core.a                        \
    libopencv_features2d.a              \
    libopencv_flann.a                       \
    libopencv_gpu.a                         \
    libopencv_highgui.a                   \
    libopencv_imgproc.a                  \
    libopencv_legacy.a                    \
    libopencv_ml.a                          \
    libopencv_objdetect.a               \
    libopencv_ocl.a                         \
    libopencv_photo.a                    \
    libopencv_stitching.a                \
    libopencv_superres.a               \
    libopencv_ts.a                          \
    libopencv_video.a                    \
    libopencv_videostab.a             

default:
[TAB] $(CXX) $(CXXFLAGS) main.cpp $(OPENCV_A)

But it result in an error traceback: a linking problem (dlsym linking (ld) problem) related to the OpenCV dependencies (libjpeg, libpng, ...).

I find the following solution to but it doesn't take the way of a static linking:

$ cmake WITH_GTK=ON WITH_GTK3=ON CMAKE_BUILD_TYPE=RELEASE BUILD_SHARED_LIBS=OFF
$ make opencv_python2    
$ cd ./lib
$ sudo cp cv2.so /usr/local/lib
$ sudo ldconfig
$ sudo ln -s  /usr/local/lib/cv2.so /usr/local/lib/libcv2.so # It's the python2 shared libary.
$ cd /path/to/my/prg 
$ g++ -std=c++11 main.cpp -lcv2

So I compile a C++ program with the python2 shared library...

Is it correct or it risk to fail if I distribute my program on others system as mine ?

I do it like this because I don't want to pollute the LIBPATH either the INCLUDEPATH from the target host.

@StevenPuttemans
Copy link

Once you start compiling OpenCV as static library, then you will need every single dependency also as a static library provided with it. That can be a pain in the ass believe me, but in some cases, there is indeed no other way. In this case this means you will need to provide a static build of libjpeg and libpng I guess.

@mrcyberfighter
Copy link
Author

Yes i realize it after that i have to link all the dependencies.

So i try to look up the *.pc file to simply add all links: This doesn't work.

I project to compile the OpenCV library from source, do i should have all dependencies installed in this case, or are they contains | downloaded once you launch the make command ?

@alalek
Copy link
Member

alalek commented Oct 4, 2015

There is simplified Makefile sample via pkgconfig:
samples/cpp/example_cmake/Makefile

Does it work for you?

@mrcyberfighter
Copy link
Author

Thanks for the link.

I must retry because i have a solid solution with the goal to not pollute the target-host system (the -lcv solution) and the linked Makefile is only adding --libs to the linking because the --static option from pkg-config does nothing with my configuration.

Simply try the command $(pkg-config --static opencv) and sea what it return.

On my system where i don't have the shared libraries installed and the $(pkg-config --static opencv) command return nothing.

@alalek
Copy link
Member

alalek commented Oct 6, 2015

BTW, pkg-config --static <name> is wrong command (there is another meaning of this flag). Use pkg-config --libs --static <name> and/or pkg-config -cflags --static <name> instead.

Sample makefile works fine for me in this scenario:

cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=install ...
make install
cd <opencv>/samples/cpp/example_cmake
PKG_CONFIG_PATH=<build path>/install/lib/pkgconfig make

@mrcyberfighter
Copy link
Author

OK thanks, I will try again to compile OpenCV as static library as soon as possible (holydays)...

The goal is to get a GTK+3 compatible version of OpenCV because my packages-manager install it with GTK+2 support.

And so you cannot compile a program with GTK+3 as GUI.

And to distribute the program without pollute the target host with a library which will take prior because the the default order from $PATH is /usr/local/lib before /usr/lib...

In the case the target host wants to use the package-manager version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org
Projects
None yet
Development

No branches or pull requests

3 participants