[CMake] how to define current working directory of command line tool

Michael Hertling mhertling at online.de
Mon Oct 11 11:39:31 EDT 2010


On 10/11/2010 11:20 AM, Jochen Wilhelmy wrote:
> 
>>> Hi!
>>>      
>>>>>>> Is it possible to define the current working directory of a command line
>>>>>>> tool
>>>>>>> that is built with cmake? since the build is usually out-of-source I have
>>>>>>> to set the current working directory in the ide, e.g visual studio or
>>>>>>> xcode. more convenient would be to set it in cmake.
>>>>>>>
>>>>>>>
>>>>>>>              
>>>>>> Do you mean in add_custom_command()/add_custom_target() ?
>>>>>> Both have an optional WORKING_DIRECTORY argument.
>>>>>>
>>>>>>
>>>>>>            
>>>>> no, i mean add_executable. when I build and start the directory I'd like to set the
>>>>> working directory that the executable is started in.
>>>>>
>>>>> -Jochen
>>>>>
>>>>>          
>>>> I think that you have a misconception with respect to the concept of "working directory".
>>>>
>>>> In a Unix-style environment, the "working directory" is determined by the "shell" command line interpreter. The actual value is determined only at execution-time. It is not something "built-in" to the executable.
>>>>
>>>>        
>>> No, I mean this very unix-style working directory. Of course this is only a debug setting, i.e. it does not influence the
>>> build result. But if I write a command line tool, e.g. a copy, then I have some test files to copy and need some arguments
>>> for the copy tool to tell it which files to copy. Therefore it is possible to set the working directory and command line
>>> arguments in an ide (visual studio or xcode). For xcode there is even the effect that after cmake runs the current
>>> setting is lost which is not the case for visual studio since these settings are stored in a separate file.
>>>
>>> -Jochen
>>>      
>> Are you trying to run the executable with ADD_TEST? In that case the working directory is always CMAKE_CURRENT_BINARY_DIR. If you need to change it, create a wrapper script (you can do so using the CMake language in order to not depend on any other interpreter).
>>    
> no, just add_executable and then starting the target in debug mode 
> inside the ide. but maybe add_test is the solution to my
> problem, i have to look at it. is this for starting an executable with 
> some command line arguments?

You might consider to use ADD_CUSTOM_COMMAND(TARGET ...), e.g.:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CWD C)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "
#include <unistd.h>
#include <stdio.h>
int main(void)
{
    char cwd[1000];
    getcwd(cwd,sizeof cwd);
    printf(\"CWD: %s\\n\",cwd);
    return 0;
}
")
ADD_EXECUTABLE(main main.c)
ADD_CUSTOM_COMMAND(TARGET main
    POST_BUILD
    COMMAND main
    WORKING_DIRECTORY /var/tmp)

This runs the executable "main" each time after it has been (re)built
from within the working directory "/var/tmp" - adapt for non-*nices.

Regards,

Michael


More information about the CMake mailing list