[CMake] add_test and locating dependent DLL's on Windows

Scott Gray sgray at inventa.com
Thu May 14 09:56:54 EDT 2009

Werner Smekal wrote:
> Hi Scott,
>>> There is actually a possibility to tell Visual C++ directly where the
>>> DLL is located (by changing the PATH variable for one configuration
>>> inside VS), but I don't remember right now how and I'm just on the
>>> wrong OS right now to have a look.
>> Obviously I want to avoid having the developer manually editing the
>> project that cmake has produced, so if such a feature exists, I'd
>> like to track down how to make cmake spit it out for me.
> Just for the records: Start Visual C++ and right click on the Project,
> then choose Properties. Go to Configuration Properties->Debugging. In
> the Textbox "Environment" enter something like:
> PATH=%PATH%;$(SolutionDir)\dll

Thanks again Werner.  While that solution would most certainly work, 
what I've been working really hard on is to ensure that a developer 
needs little more than the compiler and subversion installed on his/her 
machine to do our build.

Thanks to David Cole's help, here is what I have working now and it 
*should* be pretty portable. I'm going to try to work it into a 
re-usable function or macro, which I'll post on the list when I'm done 
if anyone is interested:

# In order to run tests we will need to set the approriate environment
# variable so that the test program can locate its dependent DLL's. First
# we want to know what directory our dependent DLL was installed into:
get_target_property(utildir util LOCATION_Debug)
get_filename_component(utildir "${utildir}" PATH)
file(TO_NATIVE_PATH "${utildir}" native_utildir)

# Next, for our directory full of platform-dependent 3rd party DLL's
file(TO_NATIVE_PATH "${THIRDPARTY_DIR}/lib" native_3rdparty_lib)
file(TO_NATIVE_PATH "${THIRDRDPARTY_DIR}/bin" native_3rdparty_bin)

# Determine which environment variable controls the locating of
# DLL's and set that variable.
if (WIN32)
   set (LD_VARNAME "PATH")
   set (LD_PATH 

   # IMPORTANT NOTE: The set_tests_properties(), below, internally
   # stores its name/value pairs with a semicolon delimiter.
   # because of this we must protect the semicolons in the path
   string(REPLACE ";" "\\;" LD_PATH "${LD_PATH}")
else ()
   set (LD_PATH 
endif ()

add_test (SomeTest SomeTest)
set_tests_properties(SomeTest PROPERTIES ENVIRONMENT 

Thanks again,

More information about the CMake mailing list