1

I have a question about how to configure the CMakeLists.txt file so that I can compile a program on macOS without using shared library, means it can be run on another macOS computer without requiring the installation of OpenCV.

I have added the set(BUILD_SHARED_LIBS=OFF) to the CMakeLists.txt file, but when I check the compiled program by typing otool -L ./MyProgram on Terminal, it show me the following:

/usr/local/opt/opencv/lib/libopencv_gpu.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_contrib.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_legacy.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_objdetect.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_calib3d.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_features2d.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_video.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_highgui.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_ml.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_imgproc.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_flann.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/usr/local/opt/opencv/lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.13)
/opt/X11/lib/libSM.6.dylib (compatibility version 7.0.0, current version 7.1.0)
/opt/X11/lib/libICE.6.dylib (compatibility version 10.0.0, current version 10.0.0)
/opt/X11/lib/libX11.6.dylib (compatibility version 10.0.0, current version 10.0.0)
/opt/X11/lib/libXext.6.dylib (compatibility version 11.0.0, current version 11.0.0)
/usr/local/opt/libpng/lib/libpng16.16.dylib (compatibility version 43.0.0, current version 43.0.0)
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libsqlite3.dylib (compatibility version 9.0.0, current version 253.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)

I guess that it was not compiled with a static OpenCV library. Here is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.6)
project(MyProgram)

include(../dlib_19_3/dlib/cmake)

set(SOURCE_FILES
        main.cpp
        SupportFunction.cpp
        SupportFunction.h)

add_executable(MyProgram ${SOURCE_FILES})
target_link_libraries(MyProgram dlib)

set(USE_FAT_OPENCV TRUE)

set(OpenCV_INCLUDE_DIR "/usr/local/Cellar/opencv/2.4.13.1/include/")
include_directories(${OpenCV_INCLUDE_DIR})
set(OpenCV_LIBS "opencv_gpu;opencv_contrib;opencv_legacy;opencv_objdetect;opencv_calib3d;opencv_features2d;opencv_video;opencv_highgui;opencv_ml;opencv_imgproc;opencv_flann;opencv_core")
link_directories(/usr/local/Cellar/opencv/2.4.13.1/lib)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(BUILD_SHARED_LIBS=OFF)

include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MyProgram ${OpenCV_LIBS})

I hope for the answer from you guys. Thank you for your time.

  • Variable *BUILD_SHARED_LIBS* affects on libraries **built by your project**. If you want to **link** with 3d-party static libraries, you need to enumerate these libraries in `target_link_libraries` call. [That question](http://stackoverflow.com/questions/7583172/opencv-as-a-static-library-cmake-options) describes how to obtain OpenCV *static libraries*. BTW, correct setting of the variable: `set(BUILD_SHARED_LIBS OFF)`. – Tsyvarev Mar 14 '17 at 09:17
  • The solution from the link you give me, it does not work. Can you tell me how to enumerate these libraries in target_link_libraries? – Tinh-Chi Tran Mar 15 '17 at 02:39
  • `Can you tell me how to enumerate these libraries in target_link_libraries?` - Just list static libraries instead of shared ones in `OpenCV_LIBS` variable, like you do now. – Tsyvarev Mar 15 '17 at 08:11
  • You mean I am doing right? If yes, why my compiled program cannot be run on another computer due to lack of Open CV ? – Tinh-Chi Tran Mar 16 '17 at 05:59
  • So, you **have static libraries** under `/usr/local/Cellar/opencv/2.4.13.1/lib`, but cannot force CMake to use them instead of system (shared) ones? Then [this question](http://stackoverflow.com/questions/28536435/how-to-link-a-static-library-to-an-executable-using-cmake) is what you are looking for. – Tsyvarev Mar 16 '17 at 07:44
  • Thanks for your answer. However, my problem is not like that. I would like to compile my problem with the OpenCV library from `/usr/local/Cellar/opencv/2.4.13.1/lib` and it has to be compiled so that the executable file won't take reference to `/usr/local/Cellar/opencv/2.4.13.1/lib` when it is called. It means I can run my program on another computer without `/usr/local/Cellar/opencv/2.4.13.1/lib` – Tinh-Chi Tran Mar 17 '17 at 06:29
  • So, which libraries do you have under `/usr/local/Cellar/opencv/2.4.13.1/lib`, **static** or **shared**? – Tsyvarev Mar 17 '17 at 07:14
  • They all have the postfix of *. dylib thus I think they are dynamic libraries. – Tinh-Chi Tran Mar 17 '17 at 08:03
  • So you need to obtain **static** OpenCV libraries first. You [cannot statically link to a shared library](http://stackoverflow.com/questions/5720205/is-it-possible-to-statically-link-against-a-shared-object). – Tsyvarev Mar 17 '17 at 08:32

0 Answers0