Issue
Below is the CMakeLists.txt file, that tries to execute terminal command with wildcard character *.txt
. Currently it gives error ls: cannot access '*.txt': No such file or directory
. But, when I try to run without the wildcard it runs as expected, ie it lists the files in the current directory.
Is there any way I could use wild cards in execute_process?
cmake_minimum_required(VERSION 3.8)
project(foo)
execute_process(COMMAND ls *.txt
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE result
OUTPUT_VARIABLE curr_out
ERROR_VARIABLE curr_out
)
message(STATUS "${result} : ${curr_out}")
Solution
Filename expansion one of expansions done by your shell before the command is executed and it causes *.txt
to expand to the list of files using special rules.
You have to run the shell to cause *.txt
to expand.
COMMAND sh -c "ls *.txt"
Using file(GLOB tmp "*.txt")
and passing tmp
would be a more cmake-ish way.
Answered By - KamilCuk Answer Checked By - Katrina (WPSolving Volunteer)