[CMake] Use of ADD_CUSTOM_COMMAND

Michael Hertling mhertling at online.de
Wed Apr 20 19:38:54 EDT 2011


On 04/20/2011 11:56 PM, Gib Bogle wrote:
> Quoting Michael Hertling <mhertling at online.de>:
> 
>> On 04/20/2011 05:40 AM, Fraser Hutchison wrote:
>>> Hi Gib,
>>>
>>> Try the following:
>>>
>>> GET_TARGET_PROPERTY(FUBAR_EXE fubar LOCATION)
>>> ADD_CUSTOM_COMMAND(TARGET fubar POST_BUILD COMMAND ${CMAKE_COMMAND} -E
>>> copy ${FUBAR_EXE} somepath)
>>>
>>> Cheers,
>>>
>>> Fraser.
>>
>> Don't use the obsolete LOCATION property since it might have subtle
>> side effects, see [1]. Instead, use the new "generator expressions"
>>
>> ADD_CUSTOM_COMMAND(TARGET fubar POST_BUILD
>>     COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:fubar> somepath)
>>
>> or follow the advice Michael W. has provided in the meantime.
>>
>> Regards,
>>
>> Michael H.
> 
> Hi Michael,
> 
> I wonder if something went amiss in your post.
> This command:
> 
> ADD_CUSTOM_COMMAND(TARGET threshold POST_BUILD COMMAND  
> ${CMAKE_COMMAND} -E copy $<TARGET_FILE:threshold> ../bin)
> 
> gives an error:
> 
> C:\Users\Gib\LN_structure\code\threshold>make
> Linking CXX executable threshold.exe
> Creating library file: libthreshold.dll.a
> Access is denied.
> mingw32-make[2]: *** [threshold.exe] Error 1
> mingw32-make[1]: *** [CMakeFiles/threshold.dir/all] Error 2
> mingw32-make: *** [all] Error 2
> 
> while this one works OK:
> 
> ADD_CUSTOM_COMMAND(TARGET threshold POST_BUILD COMMAND  
> ${CMAKE_COMMAND} -E copy threshold.exe ../bin)
> 
> Any ideas?
> 
> Gib

Which version of CMake do you use? IIRC, the generator expressions for
custom commands have been added quite recently in 2.8.4. Moreover, if
the directory "../bin" doesn't exist at the time the custom command
is run, the executable will be copied to a file named "bin" in the
directory "..", so you need to ensure the directory "../bin" does
exist, cf. FILE(MAKE_DIRECTORY ...) or "cmake -E make_directory".
Besides, don't use the ".." path in that way; use variables like
CMAKE_BINARY_DIR instead, i.e. ${CMAKE_BINARY_DIR}/../bin, e.g.

If the issue still persists, could you check whether the following
CMakeLists.txt file also fails on your system? It works on mine:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.4 FATAL_ERROR)
PROJECT(GENEXP C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
ADD_CUSTOM_COMMAND(TARGET main POST_BUILD COMMAND
${CMAKE_COMMAND} -E copy $<TARGET_FILE:main> ${CMAKE_BINARY_DIR}/bin)

If it fails, please post [C]Make's output for further investigation.

Regards,

Michael

PS: Always reply to the ML, so others may benefit from the discussion.


More information about the CMake mailing list