开发者

How to make CMake find google protobuf on windows?

I am using Google Protobuf with CMake. On Linux the Protob开发者_JAVA技巧uf library is found by:

find_package( Protobuf REQUIRED ) 

CMake knows where to look for the library. How though do I get this to work in Windows? Is there an environment variable I should create, such as PROTOBUF_LIB? I have looked in FindProtobuf.cmake but cannot work out what is required.


I also struggled with this. To be more clear.

On Windows (7, similar on older windows): Start → Control Panel → System → Advanced System Settings → Environment Variables

Then either on the top panel or the bottom panel (if you want it to apply to other users do it on the bottom), create two new variables. The 1st one is

  • CMAKE_INCLUDE_PATH which points at the bottom of your include path (should contain a "google" folder)
  • CMAKE_LIBRARY_PATH which should contain "libprotobuf" "libprotobuf-lite" "liteprotoc" .lib files.

After you create the variables press OK and then re-start cmake (or clean the cache).


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")

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

target_link_libraries(YourLibrary libprotobuf libprotobuf-lite libprotoc)

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

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()

Hope this helps.


Protobuf on windows calls find_library which will search your PATH and LIB variables.


I found the way to use protobuf v2 with cmake on Windows and build it with your project settings. Please, try look to cmake-external-packages project and protobuf-v2 CMakeLists which do the job. In fact, I wrote it because ExternalProject_Add is wrong (because does stuff in build phase instead of generation phase).

This CMakeLists.txt will download protobuf from protobuf's github releases, extract, and emit cmake targets which you should add reference to with target_link_libraries.

Use git-subtree, git-submodule or just copy this repository contents to your repository subfolder.

Then add packages you want to use with add_subdiretory. For protobuf, use:

add_subdirectory(path/to/cmake-external-packages/protobuf-v2)

Protobuf's includes will be copied to path/to/cmake-external-packages/include folder. You can customize its location in your top-level CMakeLists:

set (EXTERNAL_PACKAGES_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/third-party/include
    CACHE STRING "Directory for third-party include files, where include folders will be copied")
include_directories(${EXTERNAL_PACKAGES_INCLUDE_DIR})

Just reference protobuf for your executable:

add_executable(your_exe ${your_exe_sources})
target_link_libraries(your_exe libprotobuf libprotobuf-lite libprotoc)

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜