How to link shared object in cmake? - c++

How can I link shared object to my own code in cmake ?
I'd like to link OpenCV, I mean libopencv_***.so.3.1 or something like that to my own software package.
In that case, I believe the following lines are the answer of this question.
...
find_package(OpenCV 3.1)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
...
target_link_libraries(my_node ${OpenCV_LIBRARIES})
But, here's this problem.
I've installed OpenCV 2.4.8 under /usr/include and /usr/lib/x86_64-linux-gnu before, and now I'd like to use OpenCV 3.1.0 installed in local directory "~/Libs/opencv" from code.
So when compiling the codes, OpenCV 2.4.8 takes priority and is used.
In this case, how should I describe CMakeLists.txt ?
Currently I'm writing as the follow.
...
set(CMAKE_PREFIX_PATH ${HOME}/Libs/opencv)
include(${HOME}/Libs/opencv/share/OpenCV/OpenCVConfig.cmake)
find_package(OpenCV 3.1)
set(OpenCV_INCLUDE_DIRS ${HOME}/Libs/opencv/include)
include_directories(${OpenCV_INCLUDE_DIRS)
set(OpenCV_LIBRARY_DIRS ${HOME}/Libs/opencv/lib)
link_directories(${OpenCV_LIBRARY_DIRS})
...
The above CMakeLists.txt kind of helped me.
If executable file is copied to the same directory where libopencv_***.so.3.1 is, the above CMakeLists.txt works.
Otherwise, "make" fails.
But I'd like to make it independent on the directory which contains executable file.
How should I do for this problem ?
Thanks in advance.

See cmake's doc of find_library.
Some predefined paths are used to search for libs. (Platform dependent). It is possible to disable this by options.
Try
find_package(OpenCV 3.1 PATHS ~/Libs/opencv NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH)

Related

Using OpenCV on windows and Clion

So I'm trying to use OpenCV on windows (my ubunut is broken) and I can't configure my Clion to compile my openCV code.
first; i'm completely new to OpenCV so this question might be an easy one but I did all I could to find the problem to no avail.
I used MinGW and Clion and my codes run with no problem if I don't use OpenCv which indicates there's no problem with MinGW installation.
I downloaded OpenCv files and extracted them, then added them to System path.
after that I configured my Cmakelists.txt (my first time modifying it, so I was an amateur) like this.
After that I got this error:
Error:By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this
project has asked CMake to find a package configuration file provided
by "OpenCV", but CMake did not find one. Could not find a package
configuration file provided by "OpenCV" with any of the following
names: OpenCVConfig.cmake opencv-config.cmake Add the installation
prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a
directory containing one of the above files. If "OpenCV" provides a
separate development package or SDK, be sure it has been installed.
I know I'm doing something wrong or missing a step but I can't figure out why. is there any guide or tutorial for my scenario or can anyone point me towards a correct direction?
thanks in advance and any help appreciated.
I managed to make those errors go away with changing my
CMakeLists.txt to:
cmake_minimum_required(VERSION 3.5)
project(something)
set(OpenCV_DIR "E:/Programs/programming/opencv/build2") find_package( OpenCV REQUIRED )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp) add_executable(something ${SOURCE_FILES})
add_executable( {SOURCE_FILES} DisplayImage ) target_link_libraries( ${OpenCV_LIBS} DisplayImage )
and building the libraries on my own by minGW and Cmake.

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl

Two OpenCV's are installed (with different versions) and I can't delete all traces of the past one

I am new to Linux systems but I really need this for my project, so I really need your help.
Here is what happened: I have had OpenCV 2.4 on a linux system and I also installed another library called libv4l. For some reason after this install, cmake was unable to find OpenCv. So I installed OpenCV 3.0 this time, hoping that it was a compatibility problem or something.
However, OpenCV 2.4 was installed at /usr/lib by default (I used cmake, make, make install) and OpenCV 3.0 was installed at /usr/local/lib.
Now when I try to cmake, I get this warning:
Cannot generate a safe runtime search path for target <projectname>
because files in some directories may conflict with libraries in
implicit directories:
...list of files that exist both in usr/lib and usr/local/lib
and when I ignore the warning and proceed with make, I get the following error:
No rule to make target <an opencv file>
my CMakeLists is as follows:
cmake_minimum_required (version 2.8)
project(camera_test)
find_package(OpenCV 3.0.0) #also tried without 3.0.0
set(CMAKE_MODULE_PATH "usr/local/lib/cmake/${CMAKE_MODULE_PATH}")
find_package(raspicam REQUIRED)
target_link_libraries (camera_test ${raspicam_LIBS})
target_link_libraries (camera_test ${opencv_LIBS})
I think my problem is the same with the following pages:
Removing all installed OpenCV libs
http://answers.opencv.org/question/52921/how-to-uninstall-opencv249-completely-in-ubuntu-1404/
However here is the final catch:
I did "make uninstall" for openCV 3.0, then used
sudo find / -name "*opencv*" -exec rm {} \;
and re installed openCV 3.0
but it did not fix my problem. It still thinks there are files at usr/lib although I cant find any files there anymore.
Please suggest me a solution, and I would really appreciate if you can tell briefly me the logic of this thing. I mean, How does it know there was an installed opencv at usr/lib, what trace did I leave? What are the essential files/commands I need to be looking for/executing in such cases.
Thanks a lot, I really appreciate your help.
Edit: Here is the complete CMakeLists file:
cmake_minimum_required (VERSION 2.8)
project (raspicam_test)
find_package(OpenCV 3.0.0)
set(CMAKE_MODULE_PATH “/usr/local/lib/cmake/${CMAKE_MODULE_PATH}”)
find_package(raspicam REQUIRED)
IF ( OpenCV_FOUND AND raspicam_CV_FOUND )
MESSAGE(STATUS “COMPILING OPENCV TESTS”)
add_executable (simpletest_raspicam_cv simpletest_raspicam_cv.cpp)
target_link_libraries (simpletest_raspicam_cv ${raspicam_CV_LIBS} )
target_link_libraries (simpletest_raspicam_cv ${OpenCV_LIBS})
ELSE()
MESSAGE(FATAL_ERROR “OPENCV NOT FOUND IN YOUR SYSTEM”)
ENDIF()
Possible cause:
sudo find / -name "*opencv*" -exec rm {} \;
won't delete directories. But there are some cmake files in
/usr/share/opencv
which would therefore not be deleted, containing deprecated infos to your previous OpenCV install.
Can you check if these files exist in your filesystem?

ROS hydro opencv2 linking error during 'catkin_make'

I have little problem with my ROS hydro distro. I need to use it for one project so can't change to Indiko.
Problem is that I have used catkin_create_pkg packname opencv2 ... for creating my project package. But when I try to invoke catkin_make I get following errors:
CMake Error at /opt/ros/hydro/share/catkin/cmake/catkinConfig.cmake:75
(find_package): Could not find a package configuration file provided
by "opencv2" with any of the following names:
opencv2Config.cmake
opencv2-config.cmake
Add the installation prefix of "opencv2" to CMAKE_PREFIX_PATH or set
"opencv2_DIR" to a directory containing one of the above files. If
"opencv2" provides a separate development package or SDK, be sure it
has been installed.
I know that opencv2 is installed, because I can find it with rospack find opencv2 and I can use it in other projects not related to ROS.
I have following lines in CMakeLists.txt and package.xml
CMakeLists.txt:
find_package(catkin REQUIRED COMPONENTS opencv2)
include_directories(
${catkin_INCLUDE_DIRS}
${opencv2_INCLUDE_DIRS} )
target_link_libraries(BasicObstDetect_node
${catkin_LIBRARIES}
${opencv2_LIBRARIES} )
package.xml:
<build_depend>opencv2</build_depend>
<run_depend>opencv2</run_depend>
I have tried to use OpenCV instead of opencv2 but that didn't make any difference. Any advices?
I'm not sure, but this might work: edit your CMakeLists to add
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
and
target_link_libraries(follower ${OpenCV_LIBRARIES})

linking opencv libraries included as an external project via cmake [duplicate]

This question already has answers here:
Configuring an c++ OpenCV project with Cmake
(2 answers)
Closed 8 years ago.
I'm relatively new to cmake and after days of struggling couldn't figure out the following thing:
I have a project that depends on opencv (which is a cmake project on its own) and I want to statically link opencv libraries. What I'm doing is I have a copy of opencv source code in my project and include it into my CMakeLists.txt via
ExternalProject_Add(my_copy_of_opencv_project
CMAKE_ARGS -D BUILD_SHARED_LIBS=NO ...
CMAKE_INSTALL_PREFIX=${MY_OPENCV_INSTALL_DIR}
SOURCE_DIR ${PATH_TO_OPENCV_SRCS}
)
All built nicely and where I have problem is that I cannot reliably determine where the opencv libraries will be. E.g. on linux/mac there are in ${MY_OPENCV_INSTALL_DIR}/lib and are named as libopencv_core.a whereas on 32-bit Windows with VS 2012 installed the libs are in ${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticlib and for the Debug configuration the libs named like opencv_core247d.lib.
So the question is can I somehow obtain a list of all libraries produced by the OpenCV build (and the root lib folder) and link them via something like target_link_libraries(mylib opencv_core ...)?
Maybe I'm doing something wrong or overcomplicated. So what I basically want is to compile my embedded opencv source tree statically and link against its libraries in a "cross-plaformish" way.
Any pointers are highly appreciated! Thanks!
The best solution to use cmake with OpenCV project is:
Compile OpenCV as shared / static libraries with OpenCV's cmake build system.
In your project's CMakeLists.txt
For example (CMakeLists.txt):
cmake_minimum_required(VERSION 2.8.12)
project(test_project)
# OpenCV_DIR could also be read from system environment variable.
if(WIN32)
set(OpenCV_DIR "d:/libs/opencv-2.4.8/build")
else()
set(OpenCV_DIR "/usr/lib/opencv")
endif()
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui)
include_directories(${OpenCV_INCLUDE_DIRS}) # not needed for opencv>=4.0
add_executable(test main.cpp)
target_link_libraries(test ${OpenCV_LIBS})
It works in cross-platforms.