# Compiling core files with the project without installing opencv!

**URL:** <https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994>\
**Category:** C++\
**Tags:** build, core\
**Created:** [February 14, 2023, 8:27am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994 "2023-02-14T08:27:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 14, 2023, 8:27am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/1 "2023-02-14T08:27:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 14, 2023, 8:36am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/2 "2023-02-14T08:36:47Z")

</div>

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,

> [@nile649](#):
>
> without installing opencv or using -lopencv\_core flags.

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 😉

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 14, 2023, 11:24pm UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/3 "2023-02-14T23:24:32Z")

</div>

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 :

```auto
|-Dir
|--main.hpp
|--main.cpp

Case 2 : 

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

```

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 15, 2023, 1:59am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/4 "2023-02-15T01:59:22Z")

</div>

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.

```auto
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’

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 15, 2023, 7:50am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/5 "2023-02-15T07:50:35Z")

</div>

> ```
> 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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 15, 2023, 9:19am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/6 "2023-02-15T09:19:17Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 15, 2023, 9:21am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/7 "2023-02-15T09:21:10Z")

</div>

> [@nile649](#):
>
> `Case 2 : `

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

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 15, 2023, 2:52pm UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/8 "2023-02-15T14:52:43Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 16, 2023, 9:12am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/9 "2023-02-16T09:12:27Z")

</div>

```auto
`
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?

---

<div class="post-metadata">

**Author:** ![nile649](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nile649](https://forum.opencv.org/u/nile649)\
**Post date:** [February 17, 2023, 2:00am UTC](https://forum.opencv.org/t/compiling-core-files-with-the-project-without-installing-opencv/11994/11 "2023-02-17T02:00:49Z")

</div>

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