【问题标题】:cmake: "add_custom_target" + PRE_BUILD didn't take effect while buildingcmake: "add_custom_target" + PRE_BUILD 在构建时没有生效
【发布时间】:2019-02-20 04:20:43
【问题描述】:

我使用 cmake 来构建我的 google rpc 示例代码。我有

 examples.proto
 client.cpp
 server.cpp

我使用 protoc 命令为 protobuf 和 grpc 构建 .cc/.h 文件,CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
add_definitions(-std=c++11)
include_directories(.)

add_custom_target(protoFile
    PRE_BUILD
    COMMAND protoc --cpp_out=./ examples.proto
)
add_custom_target(protoSource
    PRE_BUILD
    COMMAND protoc --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin examples.proto
)
add_dependencies(protoSource protoFile)

add_library(protoCpp SHARED examples.grpc.pb.cc examples.pb.cc)
add_dependencies(protoCpp protoSource)

link_libraries(protoCpp protobuf grpc grpc++)
add_executable(client client.cpp)
add_executable(server server.cpp)

然后:

cmake .
-- Configuring done
CMake Error at CMakeLists.txt:15 (add_library):
Cannot find source file:

    examples.grpc.pb.cc

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: protoCpp
CMake Error: Cannot determine link language for target "protoCpp".
-- Generating done

似乎我的“add_custom_target()”没有执行,并且“ls”没有显示我预期的任何文件。那么如何让他们执行呢?我已经添加了“protoSource”作为“protoCpp”库的依赖项,但是没有用。

如何解决?

【问题讨论】:

  • add_custom_targetadd_custom_command 中指定的命令在构建阶段 执行,但在配置阶段 出现错误。该错误表明 CMake 既无法找到 add_executable 的源 files 也找不到生成它们的命令。你以一种奇怪的方式使用add_custom_commandadd_custom_target。既然你想使用 protobuf,为什么不调用find_package(Protobuf) 并使用它提供的功能呢?
  • 谢谢,这对我有用。我还发现这个示例文件非常好:github.com/jan-alexander/grpc-cpp-helloworld-cmake/blob/master/…

标签: c++ cmake dependencies grpc target


【解决方案1】:

add_executable 之后使用add_custom_target,第一个参数应该是add_executable 创建的目标,在您的示例中为“客户端”或“服务器”。你不能在add_custom_target中使用PRE_BUILD,但我不确定;

另请参阅:add_custom_target 文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多