Compiling core files with the project without installing opencv!

I am working on a project which requires to deploy custom algorithm on microcontrollers.
I used some basic functions from opnecv especially cv::Mat.

I want to compile the header files under the /core/ with rest of the cpp and hpp without installing opencv or using -lopencv_core flags.

Some idea of the hardware is that the memory limit is 1mb, currently compiled code is 12 mb which is using imgproc, highgui [This will be removed], videoio libraries.

Thanks, Please I would love to hear ideas and thoughts.

you could try to build / link a static version of opencv_core,
so you dont have to deploy a seperate so/dll,
by adding -DBUILD_SHARED_LIBS=OFF to the cmake cmdline
also: -DBUILD_LIST=core,imgproc` to avoid building other (unwanted) modules

however,

with static libs, you dont need to install/deploy the so’s (assuming you cross-compile)
but you still need -lopencv_imgproc -lopencv_core -littnotify -lrt -ldl -lz -lpthread to link your own prog

and just for comparison - libopencv_core.so is 10.3mb here, but a (stripped) prog, statically linked against core only is 2.7mb :wink:

1 Like

I am not clear. I already have opencv installed on the system.

After your comment I am not sure which case should I proceed with. Case 1, run the cmake as it is but with suggested flags from your response. Case 2, where I should copy the header files I need, and then run the cmake.

Case 1 :

|-Dir
|--main.hpp
|--main.cpp


Case 2 : 

|-Dir
|-custom_cv
|  |-core.hpp
|  |-mat.hpp
|  |-mat.inl.hpp
|  etc
|--main.hpp
|--main.cpp

So, I understand your comment better now. I am still having difficulty since lack of my experience in Cmake
I am using opencv3.4.19, and this is the error

/usr/bin/ld: attempted static link of dynamic object `/usr/local/lib/libopencv_highgui.so.3.4.19’
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/csrt.dir/build.make:179: csrt] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/csrt.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

Dynamic linking is working as expected, but not with static. Thanks again.

cmake_minimum_required(VERSION 3.0)

project(csrt)

# Set C++ standard
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find OpenCV package
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV REQUIRED core imgproc videoio)

# Add executable
file(GLOB SRCS *.cpp)
file(GLOB HEADERS *.hpp)
add_executable(csrt ${SRCS} ${HEADERS})

# Link OpenCV libraries statically
# set(OpenCV_STATIC ON)
# target_link_libraries(csrt PRIVATE ${OpenCV_LIBS} -static -littnotify -lrt -ldl -lz -lpthread)

set(OpenCV_STATIC ON)
set(OPENCV_LIBS "/usr/local/lib/")
target_link_libraries(csrt PRIVATE ${OPENCV_LIBS}/libopencv_imgproc.a ${OPENCV_LIBS}/libopencv_videoio.a ${OPENCV_LIBS}/libopencv_highgui.a ${OPENCV_LIBS}/libopencv_core.a -static -ldl -lz -lpthread)


# Set compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -O0 -g3 -Wall")

# Install executable
install(TARGETS csrt DESTINATION bin)

Update :

Currently buidling opencv library statically.
Error I am getting.

/usr/bin/ld: cannot find -littnotify
/usr/bin/ld: /home/nile649/opencv_build/opencv/build/lib/libopencv_core.a(opencl_core.cpp.o): in function `opencl_check_fn(int)':
opencl_core.cpp:(.text._ZL15opencl_check_fni+0xe1): warning: Using ‘dlopen’ in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/csrt.dir/build.make:178: csrt] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/csrt.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

Above issues resolved, I am updating the cmake file

The current errors

  1. Errors regarding window_gtk.cpp
    window_gtk.cpp:(.text.cvResizeWindow+0x10a): undefined reference to `gtk_window_resize’
  2. Errors regarding the ffmpeg
    cap_ffmpeg.cpp:(.text.ZN23InputMediaStream_FFMPEG4openEPKcPiS2_S2_S2+0x9a): undefined reference to `avformat_network_init’
  3. Errors regarding the cap_gstreamer.cpp
    cap_gstreamer.cpp:(.text._ZN2cv23CvVideoWriter_GStreamerD2Ev[_ZN2cv23CvVideoWriter_GStreamerD5Ev]+0x4d): undefined reference to `gst_object_unref’
Errors regarding window_gtk.cpp
window_gtk.cpp:(.text.cvResizeWindow+0x10a): undefined reference to `gtk_window_resize’
Errors regarding the ffmpeg
cap_ffmpeg.cpp:(.text.ZN23InputMediaStream_FFMPEG4openEPKcPiS2_S2_S2+0x9a): undefined reference to `avformat_network_init’
Errors regarding the cap_gstreamer.cpp
cap_gstreamer.cpp:(.text._ZN2cv23CvVideoWriter_GStreamerD2Ev[_ZN2cv23CvVideoWriter_GStreamerD5Ev]+0x4d): undefined reference to `gst_object_unref’

Unable to understand source of this error. I will appreciate any helps.
Thanks

wait, wait…

what kind of hardware are you trying with, exactly ??
what kind of os is there (if at all !) ?
do you really expect to connect it to a monitor & run windowed programs / gui ?
where would your (image) input come from ?
do you actually need video(file / url) handling (i doubt so !) ?

cut down the requirements to a bare minimum, before proceeding !

if you thought, you could just ‘steal’ some files, and add those to your prog – wont work ever, forget that idea, immediately

1 Like

The final hardware is MUC, but currently testing it on ubuntu 20.04.
Will try and get back to you.

Thanks

`
cmake_minimum_required(VERSION 3.6)

project(csrt)

# Set C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(OpenCV_SHARED OFF)
# Find OpenCV package
set(BUILD_SHARED_LIBS OFF)
find_package(OpenCV REQUIRED imgproc core)

# Add executable
file(GLOB SRCS *.cpp)
file(GLOB HEADERS *.hpp)
add_executable(csrt ${SRCS} ${HEADERS})

# Link OpenCV and ITP libraries statically
set(OpenCV_STATIC ON)
set(ITP_LIB "/usr/local/share/OpenCV/3rdparty/lib/libittnotify.a")
set(OPENCV_LIBS "/usr/local/lib/")
target_link_libraries(csrt PRIVATE ${OPENCV_LIBS}/libopencv_imgproc.a ${OPENCV_LIBS}/libopencv_core.a ${ITP_LIB} -static -ldl -lz -lpthread)

# Set compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -O0 -g3 -Wall")

# Install executable
install(TARGETS csrt DESTINATION bin)

`
above is the cmake config, after over conversation on this thread I now understand how to compile and get the static library work. I am really thankful.

Issue, I am trying to get the CSRT tracker from the contrib to get working on the microcontroller. The size went upto 15mb.

Any idea what could help?

After running strip --strip-unneeded csrt we do get ~10mb file.
Any suggestion would be appreciated.