How to compile OpenCV statically linked with libstdc++ - opencv

I need to compile OpenCV statically linked with libstdc++ to avoid problems of different dll versions of libstdc++-6.dll needed by Qt5 and OpenCV. Following the steps of this article: http://www.argong.com/docs/how-to-OpenCV-2.2.0.pdf and adding the lines below to CMakeLists.txt i expected to get the OpenCV DLLs statically linked with libstdc++, but the OpenCV continues dependent of the libstdc++-6.dll. What i'm doing wrong to get the OpenCV libraries statically linked with libstdc++?
if (MINGW)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc -s")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++ -s")
endif()

First ensure you're not pulling dependencies on libraries that link on shared system libraries. You can for example enable compilation of bundled source components like zlib, jpeg, etc., with flags BUILD_ZLIB, BUILD_JPEG, etc. You can also disable dependencies on optional components like Vtk (WITH_VTK flag) and others that may be already present in the system as shared libraries. Then, if you are building OpenCV as a shared library, push the setting on the shared linker flags variable (CMAKE_SHARED_LINKER_FLAGS) in the cache with cmake -C command:
set(CMAKE_SHARED_LINKER_FLAGS "-static -static-libgcc -static-libstdc++" CACHE INTERNAL "" FORCE)
Alternatively, if you are compiling OpenCV statically with -DBUILD_SHARED_LIBS=FALSE, you can plug gcc linker flags for static linking on your final shared object or executable.

Related

gtk/gtk.h: No such file or directory

In opencv compilation, we need gtk. I have gtk-2.0 and gtk-3.0 installed.
This command gave me
dpkg -l libgtk2.0-0 libgtk-3-0
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-==============-============-============-=================================
ii libgtk-3-0:amd 3.10.8-0ubun amd64 GTK+ graphical user interface lib
ii libgtk2.0-0:am 2.24.23-0ubu amd64 GTK+ graphical user interface lib
ii libgtk2.0-0:i3 2.24.23-0ubu i386 GTK+ graphical user interface lib
I have gtk-2.0 and gtk-3.0 folder inside /usr/include/.
They exist as /usr/include/gtk-2.0 and /usr/include/gtk-3.0
But my compilation for opencv gave me error as
src/window_gtk.cpp:48:21: fatal error: gtk/gtk.h: No such file or directory
#include "gtk/gtk.h"
^
compilation terminated.
I have gtk/gtk.h inside both gtk-2.0 and gtk-3.0 folders.
What could be wrong?
First, use brackets instead of double quotes searches in your include if that's not a file that belongs to your project, but a third-party one.
We also need to know which version of opencv you're trying to compile. OpenCV 2.x support GTK+ 2 only, whereas OpenCV 3.x support GTK+ 3.
We also need how you call cmake when you configure the OpenCV project, so please add the command line and the logs of that call too.
This will help determine the arguments given to the compiler, to check it uses the right include locations, and also check that it's trying to use the GTK+ backend and not the Qt one for example.

yocto SDK krogoth cmake FindBoost not found

I am trying to find the boost libraries (cmake) inside the Yocto SDK with extended environment on krogoth.
The default cmake Find_
find_package(Boost REQUIRED)
The standard error message
Unable to find the requested Boost libraries.
Unable to find the Boost header files. Please set BOOST_ROOT to the root
directory containing Boost or BOOST_INCLUDEDIR to the directory containing
Boost's headers.
Call Stack (most recent call first):
CMakeLists.txt:3 (find_package)
The following is a snippet from my conf/local.conf
IMAGE_INSTALL_append = " boost-dev"
IMAGE_INSTALL_append = " boost"
IMAGE_INSTALL_append = " kernel-devsrc"
MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "kernel-module-hello"
KERNEL_MODULE_AUTO_lOAD += "hello-md"
LCHAIN_HOST_TASK_append = "${SDK_EXTRA_TOOLS}"
SDK_EXTRA_TOOLS = " nativesdk-cmake
I am using the native cmake
auke#xenialxerus:~/workspace/beaglebone-dev/build$ which cmake
/home/auke/workspace/beaglebone-dev/poky-sdk/tmp/sysroots/x86_64-linux/usr/bin/
since I:
source environment-setup-cortexa8hf-neon-poky-linux-gnueabi
looking for the usual headers in:
find ./tmp/sysroots/beaglebone/usr/include/boost/
..
/tmp/sysroots/beaglebone/usr/include/boost/vmd/list/to_seq.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/list/to_tuple.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/to_list.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/empty.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/is_list.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/size.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/get_type.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/assert_is_identifier.hpp
./tmp/sysroots/beaglebone/usr/include/boost/vmd/is_number.hpp
..
just like the binaries:
./tmp/sysroots/beaglebone/usr/lib/libboost_system-mt.a
./tmp/sysroots/beaglebone/usr/lib/libboost_iostreams.so.1.60.0
./tmp/sysroots/beaglebone/usr/lib/libboost_serialization-mt.a
./tmp/sysroots/beaglebone/usr/lib/libboost_date_time-mt.a
./tmp/sysroots/beaglebone/usr/lib/libboost_date_time.a
./tmp/sysroots/beaglebone/usr/lib/libboost_thread.so
./tmp/sysroots/beaglebone/usr/lib/libboost_signals-mt.a
./tmp/sysroots/beaglebone/usr/lib/libboost_date_time-mt.so
./tmp/sysroots/beaglebone/usr/lib/libboost_graph-mt.a
./tmp/sysroots/beaglebone/usr/lib/libboost_iostreams.so
./tmp/sysroots/beaglebone/usr/lib/libboost_regex.so
./tmp/sysroots/beaglebone/usr/lib/libboost_wserialization.so.1
Is there something that i might have overlooked?
regards Auke
You should use
bitbake -c populate_sdk <image_name> to generate the SDK based on your image;
As an alternative to locating and downloading a toolchain installer,
you can build the toolchain installer one of two ways if you have a
Build Directory:
*Use bitbake meta-toolchain. This method requires you to still install
the target sysroot by installing and extracting it separately. For
information on how to install the sysroot, see the "Extracting the
Root Filesystem" section.
*Use bitbake -c populate_sdk. This method has significant
advantages over the previous method because it results in a toolchain
installer that contains the sysroot that matches your target root
filesystem.
Also, using the variable TOOLCHAIN_HOST_TASK to add more packages.
http://www.yoctoproject.org/docs/1.8/ref-manual/ref-manual.html
This variable lists packages the OpenEmbedded build system uses when
building an SDK, which contains a cross-development environment. The
packages specified by this variable are part of the toolchain set that
runs on the SDKMACHINE, and each package should usually have the
prefix "nativesdk-". When building an SDK using bitbake -c
populate_sdk , a default list of packages is set in this
variable, but you can add additional packages to the list.
e.g.
TOOLCHAIN_HOST_TASK += “nativesdk-libqt5core-dev”

CMake OpenCV 3.0.0 and building executables and libraries in Ubuntu

I have OpenCV 3.0.0 installed in /usr/local/opencv-3.0.0
I am trying to construct a CMakeLists file to build a library against this OpenCV 3.0.0. The CMakeLists.txt is as follows:
cmake_minimum_required(VERSION 2.8)
project(STT_People_Tracker)
cmake_policy(SET CMP0016 NEW)
# compilation mode setup
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
# set OpenCV directories - CHANGE DEPENDING ON SYSTEM
set(OpenCV_PATH "/usr/local/opencv-3.0.0")
set(OpenCV_INCLUDE_DIRS "${OpenCV_PATH}/include")
set(OpenCV_LIBS "${OpenCV_PATH}/lib/")
# set environment variables
set(SOURCES_PATH "${CMAKE_SOURCE_DIR}/Sources")
set(INCLUDES_PATH "${SOURCES_PATH}/include")
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(OUTPUT_PATH "../Debug")
message(STATUS "Compiling in DEBUG mode")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(OUTPUT_PATH "../Release")
message(STATUS "Compiling in RELEASE mode")
endif()
include_directories(${INCLUDES_PATH})
include_directories(${OpenCV_INCLUDE_DIRS})
# compilation of executables
message(STATUS "configuring executables...")
add_executable(${OUTPUT_PATH}/mainTest ${SOURCES_PATH}/mainTest.cpp)
# compilation of libraries
message(STATUS "configuring libraries...")
add_library(${OUTPUT_PATH}/background_substractor ${SOURCES_PATH}/background_substractor.cpp)
# set linker options
link_directories(${OpenCV_LIBS})
target_link_libraries(${OUTPUT_PATH}/mainTest opencv_core opencv_highgui)
target_link_libraries(${OUTPUT_PATH}/background_substractor opencv_core opencv_highgui)
message(STATUS "cmake configuration complete")
It is a fairly simple Cmake file, however, I have the following problems/doubts:
1.-How can I know I am using OpenCV 3, and not other versions of OpenCV present in the system?
2.- When compiling the file background_substractor, its associated header file can not be located, although I have checked the path and it is correctly assigned in the set(INCLUDES_PATH "${SOURCES_PATH}/include"):
/home/alberto/STT_People_Tracking/Sources/background_substractor.cpp:3:36: fatal error: background_substractor.h: No such file or directory
#include "background_substractor.h"
^
compilation terminated.
make[2]: *** [CMakeFiles/../Debug/background_substractor.dir/Sources/background_substractor.cpp.o] Error 1
make[1]: *** [CMakeFiles/../Debug/background_substractor.dir/all] Error 2
make: *** [all] Error 2
3.- Finally, and if I comment the header file, I have problems linking:
Linking CXX static library lib../Debug/background_substractor.a
/usr/bin/ar: lib../Debug/background_substractor.a: No such file or directory
make[2]: *** [lib../Debug/background_substractor.a] Error 1
make[1]: *** [CMakeFiles/../Debug/background_substractor.dir/all] Error 2
make: *** [all] Error 2
I have tried everything: Specifying the include files in the add_executable() and add_library() commands, I have checked paths and they are ok, etc etc.
Could anyone more experienced with CMake, give me a little hand?
Thank you very much in advance,
Alberto
How can I know I am using OpenCV 3, and not other versions of OpenCV present in the system?
Your project should check that.
But usually projects just use find_package command for fill variables related to 3d-party library. This command perform all needed checks. In your case it could be
find_package(OpenCV 3 REQUIRED)
call, which fills OpenCV_LIBS and OpenCV_INCLUDE_DIRS variables automatically. This command, by default, search OpenCV installation in default paths, but you can adjust searching algorithm by using parameters to cmake (so, you needn't to change CMakeLists.txt when you build the project on other machine). E.g., this way
cmake -DOpenCV_DIR=/usr/local/opencv-3.0.0 <source-dir>
you can specify precise installation path of OpenCV.
When compiling the file background_substractor, its associated header file can not be located...
Cannot suggest anything aside from checking file existence
/home/alberto/STT_People_Tracking/Sources/include/background_substractor.h
But this also can be a result of the 3d issue(see below).
Finally, and if I comment the header file, I have problems linking...
Your usage of CMake targets is incorrect. Unlike to make targets, which usually are files, CMake targets are simple names. By default, name of library/executable target determines filename of the library/executable file, produced by this target, but this can be changed. Directory, where resulted file will be located, can be adjusted using CMAKE_<TYPE>_OUTPUT_DIRECTORY variables, where <TYPE> can be ARCHIVE, LIBRARY or RUNTIME depending on target type.
Correct CMake script would be:
cmake_minimum_required(VERSION 2.8)
project(STT_People_Tracker)
cmake_policy(SET CMP0016 NEW)
# compilation mode setup
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
# set OpenCV directories using find_package.
find_package(OpenCV 3 REQUIRED)
# set environment variables
set(SOURCES_PATH "${CMAKE_SOURCE_DIR}/Sources")
set(INCLUDES_PATH "${SOURCES_PATH}/include")
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Debug") # Use absolute path
message(STATUS "Compiling in DEBUG mode")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Release")
message(STATUS "Compiling in RELEASE mode")
endif()
# Set output directory for STATIC libraries and executables
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_PATH})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_PATH})
include_directories(${INCLUDES_PATH})
include_directories(${OpenCV_INCLUDE_DIRS})
# compilation of executables
message(STATUS "configuring executables...")
add_executable(mainTest ${SOURCES_PATH}/mainTest.cpp) # Use simple name as a target
# compilation of libraries
message(STATUS "configuring libraries...")
add_library(background_substractor ${SOURCES_PATH}/background_substractor.cpp) # Use simple name as a target
# set linker options
# Command below is no-op: OpenCV libraries enumerated using absolute paths
# link_directories(${OpenCV_LINK_DIRECTORIES})
target_link_libraries(mainTest ${OpenCV_LIBS}) # Variable OpenCV_LIBS contains OpenCV libraries needed to link with
target_link_libraries(background_substractor ${OpenCV_LIBS})
message(STATUS "cmake configuration complete")

Linker can not find OpenCV libraries when using CMake

I have a copy of OpenCV2.4.0 installed in /usr/local/lib
My program compiled properly but when the linker is evoked, it gave errors such as:
/home/zhouw/moos-ivp-zhouw/trunk/src/pATRTest/mst.cpp:661: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat const&)'
CMakeFiles/pATR.dir/mst.cpp.o:/home/zhouw/moos-ivp-zhouw/trunk/src/pATRTest/mst.cpp:675: more undefined references to `cv::_OutputArray::_OutputArray(cv::Mat const&)'
collect2: ld returned 1 exit status
make[2]: *** [../bin/pATR] Error 1
make[1]: *** [src/pATRTest/CMakeFiles/pATR.dir/all] Error 2
make: *** [all] Error 2
The strange thing is my program uses opencv intensively, if CMake has trouble finding the libraries, it should have complained a lot more undefined references than jsut a few.
I tried adding
LINK_DIRECTORIES("/usr/local/lib") in my cmake file but it didn't help.
There's another library called POCO is also installed under /usr/local/lib. My program also links to the POCO libraries, but CMake seems having no trouble finding them.
If I manually link with -L/usr/local/lib, it would link properly without error.
The CMakeLists.txt looks like this
PROJECT(pATR)
#what files are needed?
SET(SRCS
spline.hpp
utils.hpp utils.cpp
mst.hpp mst.cpp
cluster.hpp cluster.cpp
target.hpp target.cpp
detector.hpp detector.cpp
classifier.hpp classifier.cpp
atr.hpp atr.cpp
MOOSAtr.h MOOSAtr.cpp
main.cpp
)
ADD_EXECUTABLE(pATR ${SRCS})
# indicate how to link
#LINK_DIRECTORIES("/usr/local/lib")
TARGET_LINK_LIBRARIES(pATR opencv_core opencv_highgui opencv_imgproc MOOS)
INSTALL(TARGETS
pATR
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)
Any idea what's going on? Many thanks!
If you have CMake 2.8, I recommend using find_package(OpenCV) to load the libraries.
There is an example at http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html
The CMake file:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
You're after:
find_package(OPENCV COMPONENTS core imgproc highgui REQUIRED)
From the docs:
Packages with components
Some libraries are not monolithic, but come with one or more dependent
libraries or components. A notable example for this is the Qt library,
which ships (among others) with the components QtOpenGL and QtXml. To
use both of these components, use the following the find_package
command:
find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)
also, for more info you may check out the following link.
https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-To-Find-Libraries
I am not sure if it makes sense that CMake can't find the linking libraries. CMake finds your dependencies and generates the Makefile, but it doesn't actually compile and link for you.
Your error are not from CMake, right? They are from make.
I always link manually with this
g++ -o myopencvapp `pkg-config --cflags --libs opencv` myopencvapp.cpp`
when invoking g++.

OpenCV compilation error in eclipse

I have been coding in python before and included the library of openCV without any problem. Now, I want to code in C++ so I downloaded eclipse and openCV libraries and included their path in the includes from eclipse ..
I have simple example of openCV and I am trying to run it, but I get this error -->
**** Build of configuration Debug for project Example ****
make all
Building file: ../Test.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test.d" -MT"Test.d" -o "Test.o" "../Test.cpp"
Finished building: ../Test.cpp
Building target: Example
Invoking: GCC C++ Linker
g++ -o "Example" ./Test.o
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In function _start':
(.text+0x20): undefined reference tomain'
./Test.o: In function gh::main()':
/home/shamma/workspace/Example/Debug/../Test.cpp:16: undefined reference tocvCreateImage'
/home/shamma/workspace/Example/Debug/../Test.cpp:21: undefined reference to cvNamedWindow'
/home/shamma/workspace/Example/Debug/../Test.cpp:22: undefined reference tocvShowImage'
/home/shamma/workspace/Example/Debug/../Test.cpp:23: undefined reference to cvWaitKey'
/home/shamma/workspace/Example/Debug/../Test.cpp:24: undefined reference tocvDestroyWindow'
/home/shamma/workspace/Example/Debug/../Test.cpp:25: undefined reference to `cvReleaseImage'
collect2: ld returned 1 exit status
make: * [Example] Error 1enter code here
any idea what might be the cause of the problem, I have tried lots of things without use
thanks in advance
It seems you have not properly configured OpenCV libraries. I recommend you to follow the OpenCV tutorial to start using it with Eclipse.
In eclipse go to project->properties->settings and under GCC C++ linker-> libraries and assuming you're just testing, add "opencv_core" and "opencv_highgui" there are many more and for each library you need to manually include them so that eclipse can tell the compiler to link these libraries when you run your program.
Also as a note generally "cvE______" is opencv for C where as for C++ it would be cv::E____ , its really confusing that sometime you can get away with both, but not always, so stick the the function in the namespace without cv prefixes.

Resources