CMake, Exe cant find DLL

I'm trying to equip OpenCV 1.0.0 with CMake support and meet the very similar situation, i.e. in my own solution, there is a shared library(.dll) and a executable(.exe) built from my source and header files, and when executing that .exe file, how could we ensure .exe can find .dll?

As @drescherjm commented, the solution is: in root CMakeLists.txt, before add_subdirectory(), add these two lines:

set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}")

I had a similar problem whem trying to use cmocka lib to create tests.

Even thought CMake found your library with a find_library command like

find_library(<SOME_VAR> NAMES lib_name PATHS "where/to/search")

you'll still run into this problem.

Windows execution will not be able to find the .dll. You can solve this problem by adding this library stored in right next to your executable.

So if you have something like

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

in your CMakeLists.txt file, you'd only have to add

file(COPY ${SOME_VAR}
    DESTINATION ${EXECUTABLE_OUTPUT_PATH})

That's it.