[TOC]

add_custom_command

add_custom_command(OUTPUT output1 [output2 ...]  COMMANDcommand1 [ARGS] [args1...]  [COMMANDcommand2 [ARGS] [args2...] . ]  [MAIN_DEPENDENCY depend] [DEPENDS [depends...]] [BYPRODUCTS [files...]] [IMPLICIT_DEPENDS <lang1> depend1 [<lang2> depend2] ...]  [WORKING_DIRECTORY dir] [COMMENT comment] [DEPFILE depfile] [VERBATIM] [APPEND] [USES_TERMINAL])Copy the code

Why doesn’t my add_custom_command execute?

example

LIST(APPEND SOURCE app_head.pb.c)
The OUTPUT must be the same as the source in add_executable, or it can be the absolute path and the file name below
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/${SOURCE}
    COMMAND ls -al
    COMMENT "h -----------------------"
    )

Add_custom_command (); / / add_custom_command (); / / add_custom_command ()
add_executable(ttt ${CMAKE_SOURCE_DIR}/${SOURCE}) 
Copy the code

The following are the two consistent and inconsistent printed information

Scanning dependencies of target TTT71%] h ----------------------- Total usage48
drwxr-xr-x 3 txz txz  4096 8month26 20:16 .
drwxr-xr-x 3 txz txz  4096 8month24 14:38. -rw-r--r--1 txz txz 11609 8month26 20:16 CMakeCache.txt
drwxr-xr-x 8 txz txz  4096 8month26 20:16 CMakeFiles
-rw-r--r-- 1 txz txz  1371 8month26 20:16 cmake_install.cmake
-rw-r--r-- 1 txz txz  7879 8month26 20:16 Makefile
-rwxr-xr-x 1 txz txz  8808 8month26 20:16 t5
[ 85%] Building C object CMakeFiles/ttt.dir/app_head.pb.c.o cc: error: /home/txz/test/testCMake/t5/app_head.pb.c: Cc: Fatal error: Add_custom_command for short path, SOURCE for absolute path -- Configuring done CMake Error at cmakelists.txt30 (add_executable):
  Cannot find source file:

    /home/txz/test/testCMake/t5/app_head.pb.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx


CMake Error: CMake can not determine linker language for target: ttt
CMake Error: Cannot determine link language for target "ttt".
-- Generating done
-- Build files have been written to: /home/txz/test/testCMake/t5/build
Copy the code

case

Why does my method report an error of Unknown type of argument

 LIST(append SOURCE app_head.pb.c)
 Note that output is lowercase
 add_custom_command(output ${CMAKE_SOURCE_DIR}/${SOURCE}
     COMMAND ls -al
     COMMENT "h -----------------------"
     )
 add_executable(ttt ${SOURCE})
Copy the code

For the record here, the method name is case-insensitive, but reserved words inside the method must be uppercase as instructed by the document

Otherwise, an error will be reported

CMake Error at CMakeLists.txt:26 (add_custom_command):
  add_custom_command Wrong syntax.  Unknown type of argument.


-- Configuring incomplete, errors occurred!
See also "/home/txz/test/testCMake/t5/build/CMakeFiles/CMakeOutput.log".
Copy the code

file

Why file(READ) does not output each line using foreach

  # string (string, string, string, string, string, string
 file(STRINGS tmp.txt out)
 
 foreach(item ${out})
     message(STATUS "xxx" ${item})
 endforeach()
 
 file(READ tmp.txt out_read)
 foreach(item ${out_read})
     message(STATUS "-- -- -- -- --" ${item})
 endforeach(a)Copy the code

Here is the output

 xxxcpp/com/txz/music_manager/music_manager.pb.h
-- xxxtest
-- xxxtest1
-- -----cpp/com/txz/music_manager/music_manager.pb.h
test
test1
Copy the code

Let’s do another example

 execute_process(COMMAND find ${CMAKE_SOURCE_DIR}  -name *.h
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     OUTPUT_FILE tmp1.txt
     )
 file(STRINGS tmp1.txt PROTO_HEADERS)
 foreach(item ${PROTO_HEADERS})
     message(STATUS "> > > > > > > > > > > >" ${item})
 endforeach()
 file(READ tmp1.txt read_file_content)
 foreach(item ${read_file_content})
     message(STATUS "< < < < < < < < < < <" ${item})
 endforeach(a)Copy the code

Here is the output

-- >>>>>>>>>>>>/home/txz/test/testCMake/t5/b.h
-- >>>>>>>>>>>>/home/txz/test/testCMake/t5/a.h
-- <<<<<<<<<<</home/txz/test/testCMake/t5/b.h
/home/txz/test/testCMake/t5/a.h

Copy the code

How to print each line of multiline text output by an instruction

 execute_process(COMMAND find ${CMAKE_SOURCE_DIR}  -name *.h
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
     OUTPUT_VARIABLE output_content
     )
 
 foreach(item ${output_content})
     message(STATUS "+ + + + + + + +" ${item})
 endforeach(a)Copy the code

Here is the output [to prove that it is printed all at once, not line by line]

-- ++++++++/home/txz/test/testCMake/t5/b.h
/home/txz/test/testCMake/t5/a.h

Copy the code

Solution: Print it to a file and read each line through the file, as shown in the previous example.

If you look at my Github, it has all of my code on it: my Github

Don’t go, follow the public account