簡體   English   中英

CMake找不到自定義命令“ ls”

[英]CMake does not find custom command “ls”

我嘗試為我的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_out時,出現以下錯誤消息。

[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)

問題

CMake不能為其COMMAND調用保證shell上下文,也不會自動搜索COMMAND本身提供的可執行文件。

它主要將給定命令放入生成的構建環境中,並取決於在此環境中的處理方式。

在您的情況下,我假設您/ CLion在MS Windows cmd shell中運行cmakemingw32-make 在這種情況下,您將不得不使用dir而不是ls命令:

add_custom_target(
    run_hello_out
    COMMAND dir $<TARGET_FILE_NAME:hello>
    DEPENDS hello
)

可能的解決方案

我看到了三種可能的解決方案。

  1. 提供bash shell上下文

     include(FindUnixCommands) if (BASH) add_custom_target( run_hello_out COMMAND ${BASH} -c "ls -l hello" DEPENDS hello ) endif() 
  2. 使用CMake的外殼程序抽象cmake -E (僅有限數量的命令,例如,沒有ls等效項)

     add_custom_target( run_hello_out COMMAND ${CMAKE_COMMAND} -E echo $<TARGET_FILE:hello> DEPENDS hello ) 
  3. 使用find_program()搜索可執行文件。

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

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM