如何让CMake在Windows上找到google protobuf?

Joh*_*n W 15 cmake protocol-buffers

我正在使用Google Protobuf和CMake.在Linux上,Protobuf库可以通过以下方式找到:

find_package( Protobuf REQUIRED ) 
Run Code Online (Sandbox Code Playgroud)

CMake知道在哪里寻找图书馆.我怎么能在Windows中使用它?我应该创建一个环境变量,例如PROTOBUF_LIB?我已经查看FindProtobuf.cmake但无法解决所需的问题.

Jub*_*bei 8

我也在努力解决这个问题.更清楚.

在Windows上(7,类似于旧版Windows):开始 - >控制面板 - >系统 - >高级系统设置 - >环境变量

然后在顶部面板或底部面板上(如果您希望它应用于其他用户在底部执行),创建两个新变量.第一个是

  • CMAKE_INCLUDE_PATH指向包含路径的底部(应包含"google"文件夹)
  • CMAKE_LIBRARY_PATH它应该包含"libprotobuf""libprotobuf - 精简版""liteprotoc" .lib文件.

创建变量后,按"确定",然后重新启动cmake(或清理缓存).


hal*_*hal 5

Newest protobuf v3 have CMake support out of the box.

You could use protobuf repository as submodule and just use

add_subdiretory("third-party/protobuf/cmake")
Run Code Online (Sandbox Code Playgroud)

to get all protobuf targets. Then you can add dependency to protobuf with

target_link_libraries(YourLibrary libprotobuf libprotobuf-lite libprotoc)
Run Code Online (Sandbox Code Playgroud)

Another possible way is available. With protobuf's CMake configuration you can build and install protobuf binaries once and use them across several projects in your development:

git clone https://github.com/google/protobuf.git
mkdir protobuf\tmp
cd protobuf\tmp
cmake ..\cmake
cmake --build .
cmake --build . --target install
Run Code Online (Sandbox Code Playgroud)

Then you can use find_package with hint paths like

find_package(protobuf REQUIRED
    HINTS
       "C:/Program Files/protobuf"
        "C:/Program Files (x86)/protobuf")
if (NOT PROTOBUF_FOUND)
    message("protobuf not found")
    return()
endif()
Run Code Online (Sandbox Code Playgroud)

Hope this helps.


Rob*_*ard 4

Windows 上的 Protobuf 调用 find_library 它将搜索您的 PATH 和 LIB 变量。

  • 谢谢,我发现 find_library 还搜索 CMAKE_INCLUDE_PATH、CMAKE_INCLUDE_PATH 和 CMAKE_LIBRARY_PATH。 (2认同)