2016-08-19 104 views
1

我嘗試爲我的CLion項目運行一些基本命令,但它不起作用。這是我的CMake設置。CMake找不到自定義命令「ls」

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命令你好」,看看在完成構建窗口或運行窗口中的結果。

+2

問題CMake的代碼是不是正在構建過程中使用。我認爲你有'命令'ls -l hello「'(引用的命令+參數)。另一種猜測是,也許你在Windows上運行命令,並且%PATH%中沒有「ls」可執行文件。 –

+0

謝謝。它在我的%path%中,但它不適用於CMake。所以我嘗試了完整路徑。 – Leonard

回答

1

不知何故ls即使正確設置全局路徑也不起作用。 CMake需要完整路徑。以下工作和解決問題。

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

的問題

的CMake並不保證一個外殼上下文其COMMAND電話和它不會自動查找與COMMAND本身給出的可執行文件。

它主要將給定的命令放入生成的構建環境中,並取決於它在那裏如何處理。

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

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

可能Soltions

我看到三個可能的解決方案。無論是

  1. 提供一個bash外殼上下文

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

參考