作为基于 CMake 的程序的配置步骤的一部分,我将程序下载到 CMAKE_BINARY_DIR 的子文件夹中,然后作为 add_custom_command() 的一部分调用它以在构建时生成一些源文件。
但是,在运行make
构建项目时,进入自定义构建步骤时总是会收到此错误消息:
: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
make
使用标志重新运行VERBOSE=1
以使其打印构建命令,我得到以下输出:
cd (directory) && (cmake_binary_dir)/path/to/tool (arguments...)
: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
其中工具的路径是绝对路径CMAKE_BINARY_DIR
将完整的绝对路径复制到该工具并将其直接粘贴到我的终端中,可以使程序毫无问题地启动。
在这一点上,我不知道如何让 CMake 运行我的自定义构建阶段。有什么建议吗?
编辑:我的实际 add_custom_command 调用如下:
# at this point, PROTOC_COMPILER holds the path to protoc (inside the cmake build directory)
# PROTOBUF_FILES is the collection of .proto files I want to generate
message(STATUS "protoc path: ${PROTOC_COMPILER}")
foreach(PROTOFILE ${PROTOBUF_FILES})
get_filename_component(PROTOFILE_DIR ${PROTOFILE} DIRECTORY)
get_filename_component(PROTOFILE_NAME ${PROTOFILE} NAME_WE) # name without extension
get_filename_component(PROTOFILE_ABSOLUTE_PATH ${PROTOFILE} ABSOLUTE)
set(PROTOFILE_OUTPUT_HEADER_NAME "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}/${PROTOFILE_NAME}.pb.h")
set(PROTOFILE_OUTPUT_SRC_NAME "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}/${PROTOFILE_NAME}.pb.cc")
add_custom_command(
OUTPUT "${PROTOFILE_OUTPUT_HEADER_NAME}" "${PROTOFILE_OUTPUT_SRC_NAME}"
COMMENT "Compiling Protobuf file: ${PROTOFILE}"
# make sure the output directory exists.
COMMAND ${CMAKE_COMMAND} -E make_directory "${FULL_OUTPUT_DIR}/${PROTOFILE_DIR}"
# actually generate the files. This is the command that fails.
COMMAND ${PROTOC_COMPILER}
--cpp_out="${FULL_OUTPUT_DIR}" # where to put the output files.
# make sure we can reference includes.
# directory the $PROTOFILE is in. Otherwise protoc complains about it.
-I${CMAKE_CURRENT_SOURCE_DIR}
--grpc_out="${FULL_OUTPUT_DIR}"
--plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}"
"${PROTOFILE_ABSOLUTE_PATH}" # the file to build
DEPENDS "${PROTOFILE_ABSOLUTE_PATH}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
list(APPEND GENERATED_HEADERS ${PROTOFILE_OUTPUT_HEADER_NAME})
list(APPEND GENERATED_SOURCES ${PROTOFILE_OUTPUT_SRC_NAME})
endforeach()
add_library(my-project-protomessages STATIC ${GENERATED_SOURCES} ${GENERATED_HEADERS})
target_include_directories(my-project-protomessages PUBLIC ${FULL_OUTPUT_DIR})