25

I'm trying to build an OpenCV-based project using CMake, running on Linux. So far my CMakeLists.txt files looks something like

FIND_PACKAGE (OpenCV REQUIRED)
...
TARGET_LINK_LIBRARIES (my-executable ${OpenCV_LIBS})

but this results in dynamically linked libraries. How do I link with static libraries?

Al.G.
  • 4,327
  • 6
  • 31
  • 56
agnul
  • 12,608
  • 14
  • 63
  • 85

7 Answers7

13

You build static OpenCV libraries by just setting the BUILD_SHARED_LIBS flag to false in CMake. Then all you need to do to build your own application with those static libraries is to add a dependency on OpenCV in your CMakeLists.txt:

FIND_PACKAGE (OpenCV REQUIRED)
...
TARGET_LINK_LIBRARIES (your-application ${OpenCV_LIBS})

and CMake will take care of everything.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
agnul
  • 12,608
  • 14
  • 63
  • 85
  • 3
    It's `BUILD_SHARED_LIBS`, not `_LIBRARIES`. :-) – Ela782 Dec 31 '15 at 21:36
  • I corrected BUILD_SHARED_LIBRARIES to BUILD_SHARED_LIBS. – CODE-REaD Sep 10 '16 at 18:28
  • At least with CLion under Windows (CLion 2016.3 EAP) which comes bundled with CMake 3.6.1, when building OpenCV 3.1.0, this was the only way of those listed here which caused CMake to build OpenCV as static rather than shared libraries. Note that attempts to enable this option by editing CMakeLists.txt failed; only by editing the CMake cache via CLion's Cache window and clicking the *Apply Changes and Reload Project* icon was I able to get my CLion/CMake combination to accept `BUILD_SHARED_LIBS`. Not sure if this is a problem with CLion that should be documented. – CODE-REaD Sep 10 '16 at 20:25
  • 3
    This only works if you're building opencv as well. Not in the case where you're using preinstalled opencv libraries. – simplename Feb 06 '19 at 20:34
12

Actually this issue seems to have already been fixed in the OpenCVConfig.cmake that comes with OpenCV. All you have to do is define OpenCV_STATIC in your CMakeLists.txt. I.e.

set(OpenCV_STATIC ON)
find_package(OpenCV REQUIRED)
Fred
  • 3,324
  • 1
  • 19
  • 29
bcook
  • 137
  • 1
  • 3
6

To link everything statically, I believe you're looking for CMAKE_EXE_LINKER_FLAGS (add -static).

Are you using the 'simple method' of OpenCVConfig.cmake? or the older FindOpenCV.cmake?

jkerian
  • 16,497
  • 3
  • 46
  • 59
5

AFAIK that's a bit tricky, because CMake, more precisely the find_library command, prefers shared libs and finds those if both shared and static are available.

I'm still looking for a good solution myself to be able to compile binaries "as static as possible", but I've found no elegant solution yet. The only way it would surely work is to implement everything through custom FindXXXX modules.

pszilard
  • 1,942
  • 1
  • 15
  • 18
  • 5
    The only way I now is to change searched library suffixes: `set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})` before `find_library()`. FindBoost and has special option to force the use of static libraries (`Boost_USE_STATIC_LIBS`), but it also works internally by changing `CMAKE_FIND_LIBRARY_SUFFIXES`. – marcin Sep 03 '13 at 13:09
3

on the add_library line specify static. See https://cmake.org/cmake/help/latest/command/add_library.html

Correction since you are looking to link against a static library I would look into the CMAKE_FIND_LIBRARY_SUFFIXES property

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
RobertJMaynard
  • 2,183
  • 14
  • 13
  • I think the add_library command is to generate an output binary. In this case, specifying STATIC causes it to generate .a. – jkerian Sep 20 '10 at 22:52
  • The static keyword for add_library specifies what type of library it generates, it is not for when generating the binary. – RobertJMaynard Sep 20 '10 at 23:11
  • Perhaps I misunderstood, but I thought the OP was trying to link with a static library, not generate one. – jkerian Sep 20 '10 at 23:36
  • yep, I'm not creating a library, I want to link (statically) to the OpenCV libraries. – agnul Sep 21 '10 at 08:47
1

Note that gcc refuses to link if you pass the -static option, but you have dynamic libs in the link arguments - which you will if you just simply use FindOpenCV.cmake and this picks up the dynamic libs (I don't know how OpenCVConfig.cmake behaves though)...

pszilard
  • 1,942
  • 1
  • 15
  • 18
-1
SET (CMAKE_EXE_LINKER_FLAGS "-static")