I am trying to write a C++ application using the OpenCV libraries. As per the recommendation from the OpenCV maintainers, I am using CMake to generate the Makefile. My platform is Windows 7 (64 bit). My compiler is MinGW (so I am using the 'mingw32-make' tool to build the application).
Right now I am trying to make sure my setup is correct so I can move forward with the code. I have verified that the OpenCV libraries have been built correctly. I then tried to verify that I could use CMake to include the OpenCV libraries in my build. I am using the following sample code from an OpenCV tutorial:
DisplayImage.cpp
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if (argc != 2 || !image.data)
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
CMakeLists.txt
cmake_minimum_required( VERSION 2.8 )
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
I used cmake-gui to generate the Makefile, then used mingw32-make to build the application. Everything works fine and the program compiles with no errors. However, when I try to run it, Windows stops and complains that it needs libopencv_core231.dll. I have checked and this dll is in my (OpenCV Build location)/bin directory. How can I get CMake/MinGW to include this when compiling?
copy opencv dlls into your executable folder.
MACRO (COPY_DLL trgt libname)
ADD_CUSTOM_COMMAND (
TARGET ${trgt}
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy "${OPENCVDIR/bin}/${libname}.dll" ${CMAKE_CURRENT_BINARY_DIR}
)
ENDMACRO (COPY_DLL)
In my case, I looked for something working using visual Studio (which supports multiple configurations in the build tree), the command would be slightly modified.
the command works in parallel of find_package( OpenCV REQUIRED )
first: get output dll (build as the unique opencv_world lib)
get_target_property(__dll_dbg opencv_world IMPORTED_LOCATION_DEBUG)
get_target_property(__dll_release opencv_world IMPORTED_LOCATION_RELEASE)
Then you could add a command:
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD # Adds a post-build event the TARGET
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"$<$<CONFIG:debug>:${__dll_dbg}>$<$<CONFIG:release>:${__dll_release}>" # <--this is in-file
$<TARGET_FILE_DIR:${TARGET_NAME}> # <--this is out-file path
# another dll copy if needed here
COMMENT " [1718_34_ATK] copy dlls realsense2 and opencv_world")
I use configure_file(...)
which copies specified .dll
files when they change (rarely) for the right compiler using ${_OpenCV_LIB_PATH}
.
find_package(OpenCV REQUIRED)
configure_file("${_OpenCV_LIB_PATH}/opencv_world451d.dll" ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With