Windows CMake找不到自定义命令“ls”

Windows CMake找不到自定义命令“ls”,windows,cmake,Windows,Cmake,我尝试为我的CLion项目运行一些基本命令,但它就是不起作用。这是我的CMake设置 cmake_minimum_required(VERSION 3.6) project(hello) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp) add_executable(hello ${SOURCE_FILES}) add_custom_command(OUTPUT hello.out

我尝试为我的CLion项目运行一些基本命令,但它就是不起作用。这是我的CMake设置

cmake_minimum_required(VERSION 3.6)
project(hello)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(hello ${SOURCE_FILES})

add_custom_command(OUTPUT hello.out
        COMMAND ls -l hello
        DEPENDS hello)

add_custom_target(run_hello_out
        DEPENDS hello.out)
在CLion中运行run_hello_时,我收到以下错误消息

[100%] Generating hello.out
process_begin: CreateProcess(NULL, ls -l hello, ...) failed.
make (e=2): The system cannot find the file specified.
mingw32-make.exe[3]: *** [hello.out] Error 2
mingw32-make.exe[2]: *** [CMakeFiles/run_hello_out.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/run_hello_out.dir/rule] Error 2
mingw32-make.exe: *** [run_hello_out] Error 2
CMakeFiles\run_hello_out.dir\build.make:59: recipe for target 'hello.out' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/run_hello_out.dir/all' failed
CMakeFiles\Makefile2:73: recipe for target 'CMakeFiles/run_hello_out.dir/rule' failed
Makefile:117: recipe for target 'run_hello_out' failed
它应该运行ls-l hello并在构建窗口或运行窗口中查看结果。

不知何故,即使我正确设置了全局路径,ls也无法工作。CMake需要完整路径。下面的工作可以解决这个问题

add_custom_command(OUTPUT hello.out
        COMMAND "C:\\FULL PATH HERE\\ls" -l hello
        DEPENDS hello)
不知何故,即使我正确设置了全局路径,ls也不起作用。CMake需要完整路径。下面的工作可以解决这个问题

add_custom_command(OUTPUT hello.out
        COMMAND "C:\\FULL PATH HERE\\ls" -l hello
        DEPENDS hello)
问题

CMake不保证其命令调用的shell上下文,也不会自动搜索命令本身给定的可执行文件

它主要将给定的命令放入生成的构建环境中,并取决于在那里如何处理它

在您的情况下,我假设您/CLion正在MS Windows cmd shell中运行cmake和mingw32 make。在这种情况下,您必须使用dir而不是ls命令:

使用CMake的shell抽象只能使用有限数量的命令,例如,没有等效的ls

add_custom_target(
    run_hello_out
    COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_FILE:hello>
    DEPENDS hello
)
参考文献

问题

CMake不保证其命令调用的shell上下文,也不会自动搜索命令本身给定的可执行文件

它主要将给定的命令放入生成的构建环境中,并取决于在那里如何处理它

在您的情况下,我假设您/CLion正在MS Windows cmd shell中运行cmake和mingw32 make。在这种情况下,您必须使用dir而不是ls命令:

使用CMake的shell抽象只能使用有限数量的命令,例如,没有等效的ls

add_custom_target(
    run_hello_out
    COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_FILE:hello>
    DEPENDS hello
)
参考文献


所讨论的CMake代码不是构建期间使用的代码。我假定您使用的是命令ls-l hello COMMAND+引述的参数。另一个猜测是,可能您正在Windows上运行该命令,而您的%PATH%中没有ls可执行文件。谢谢。它在我的%path%中,但对CMake不起作用。因此,我尝试了完整路径。所讨论的CMake代码不是构建过程中使用的代码。我假定您使用的是命令ls-l hello COMMAND+引述的参数。另一个猜测是,可能您正在Windows上运行该命令,而您的%PATH%中没有ls可执行文件。谢谢。它在我的%path%中,但对CMake不起作用。所以我尝试了完整路径。
find_program(LS ls)

if (LS)
    add_custom_target(
        run_hello_out
        COMMAND ${LS} -l hello
        DEPENDS hello
endif()