How to make CMake find google protobuf on windows?

匿名 (未验证) 提交于 2019-12-03 08:35:02

问题:

I am using Google Protobuf with CMake. On Linux the Protobuf 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.

回答1:

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



回答2:

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



回答3:

Newest protobuf v3 (now in beta) 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.



回答4:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!