5

I am trying to solve this problem for 2 days now with no luck. I've read an endless number of threads allover the web and tried a lot of suggestions but so far with no luck.

I'm doing this on Windows 10 with VS2017 and most recent VS Code installed. I installed protobuf with vcpkg install protobuf:

The package protobuf:x64-windows provides CMake targets:

find_package(protobuf CONFIG REQUIRED)
target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

I downloaded Google's example code and extracted it on my drive. The .PROTO file compiles without a problem:

d:\protobuf-3.12.2\examples>protoc -I=d:\protobuf-3.12.2\examples --cpp_out=d:\protobuf-3.12.2\examples d:\protobuf-3.12.2\examples\addressbook.proto

and creates the two files "addressbook.pb.cc" and "addressbook.pb.h" as expected.

Now when I try to compile the project in Visual Studio Code it constantly fails no matter how I modify the CMakeLists.txt file. As mentioned I went through dozens of threads regarding this problem and tried a lot with no luck.


Update 29.05.2020

I checked that protobuf is installed just once and indeed the demo package also included a full protobuf installation. I removed this extra demo package and un-/installed protobuf with vcpgk. I then compiled the .proto file with protoc (which is in my path) and got the two files "addressbook.pb.cc" and "addressbook.pb.h".

Then I tried to compile the project again, this time using the CMakeLists.txt that came with the demo.

The relevant part seems to be right in the beginning:

# Minimum CMake required
cmake_minimum_required(VERSION 2.8.12)
# Project
project(protobuf-examples)
# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)
if(protobuf_VERBOSE)
  message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
endif()
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

Compiling this gives me:

[main] Building folder: examples 
[main] Configuring folder: examples 
[cms-client] Configuring using the "Visual Studio 15 2017" CMake generator with platform "x64" and toolset "host=x64"
[cmake] Selecting Windows SDK version 10.0.17763.0 to target Windows 
...
[cmake] CMake Error at CMakeLists.txt:8 (find_package):
[cmake]   Could not find a package configuration file provided by "protobuf" with any
[cmake]   of the following names:
[cmake] 
[cmake]     protobufConfig.cmake
[cmake]     protobuf-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "protobuf" to CMAKE_PREFIX_PATH or set
[cmake]   "protobuf_DIR" to a directory containing one of the above files.  If
[cmake]   "protobuf" provides a separate development package or SDK, be sure it has
[cmake]   been installed.
[cmake] 
[cmake] 
[cmake] Configuring incomplete, errors occurred!
[cmake] See also "d:/vcpkg/buildtrees/protobuf/src/v3.12.0-8ba83cbbdb/examples/build/CMakeFiles/CMakeOutput.log".
[cms-driver] Error during CMake configure: [cmake-server] Configuration failed.

The file protobuf-config.cmake can be found multiple times in the protobuf folder:

  • D:\vcpkg\buildtrees\protobuf\<BUILDCFG>\share\protobuf\protobuf-config.cmake
  • D:\vcpkg\installed\<BUILDCFG>\share\protobuf\protobuf-config.cmake
  • D:\vcpkg\packages\<BUILDCFG>\share\protobuf\protobuf-config.cmake

What could be the cause that CMake can't locate these files?

Andreas
  • 5,393
  • 9
  • 44
  • 53

3 Answers3

0

Fair warning I am no expert.

I ran in to a similar problem in my own build trying to get Boost working and I think it has to do with your environment variables and how you have your Visual Studio setup. While you are setting crucial things such as

SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")

The actual find_package(protobuf CONFIG REQUIRED) throws these settings out the window. Once it finds that config file it only cares about what the config file is finding, I think this is the cause of how your first MESSAGE has the right one, and then your 2nd one doesn't find one.

Are you positive you only have one installation of protobuf on your machine?

  • It appears to be finding this "used as include directory in directory D:/protobuf-3.12.2/examples"
  • yet you are trying to find "D:/vcpkg/packages/protobuf_x64-windows" no?

Try adding a "-DCMAKE_PREFIX_PATH="d:/vcpkg/packages/protobuf_x64-windows" to your CMake options in Visual Studio

Good luck and sorry if this doesn't help, I'm relatively new to programming but its worth a try.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Austin
  • 174
  • 2
  • 1
    This is really just environment variable issues within your compilation environment. For context with my boost files Installing boost put them at: "C:/users/name/folder/x64" This is what I set my CMAKE_PREFIX_PATH at. now inside that x64 folder I have a **include** and **lib** folder. Inside my lib folder there is a cmake folder which has all my config.cmake files. your CMAKE_PREFIX_PATH needs to point to that root folder for your installation. It also wouldn't hurt to set your protobuf_DIR to that same root folder in your CMakeLists. – Austin May 29 '20 at 17:01
  • OK I've added CMAKE_PREFIX_PATH to my CMakeLists.txt but now a new problem that is even weirder came up. Please see the updated question. – Andreas Jun 23 '20 at 13:30
  • Could we see the CMakeLists.txt you are using? Make sure the protobuf-config.cmake matches the one found here [link](https://github.com/protocolbuffers/protobuf/blob/master/cmake/protobuf-config.cmake.in) I doubt they shipped it without it working on their end syntax-wise so this is a very strange error indeed. but if they did just change all the backslashes to forward slashes in that config file. So line 108 would read `if(NOT "${_rel_dir}" MATCHES "^/./.[///].*")` I highly doubt this would work and DO NOT recommend changing these config files, but its worth a shot? – Austin Jun 23 '20 at 14:30
  • Austin, I've updated the question and added the full error log so maybe you want to have another look at it. I also verified that every protobuf-config.cmake found in vcpkg matches the one from your link/GitHub. I pasted my CMakeLists.txt here: https://pastebin.com/raw/nupYaejE. This file came with the Protobuf examples and additionally contains the CMAKE_PREFIX_PATH as you have suggested. – Andreas Jun 24 '20 at 05:27
  • @Andreas I'm really at a loss for what to do next, This doesn't appear to be a CMake compile issue anymore, more of a syntax issue and I've no idea why. Maybe try making a new thread related to it? I'm not sure what that line 108 is really trying to do, it appears as though its trying to match a directory to a potential format in which case my only suggestion is backslashes don't work in cmake parsing \.\.\.\.\ but forward slashes do /././././ – Austin Jun 26 '20 at 14:17
  • Just noticed it throws another error too thats not related to the syntax error but instead that a file is missing, Try removing your entire protobuf directory and grab a fresh install to see if the error remains. (Keep ur cmakelists changes handy) – Austin Jun 26 '20 at 14:28
  • @Andreas What version of CMake do you have? Going to try and keep looking into this. – Austin Jul 02 '20 at 15:38
  • I updated my answer to include some help with the first error. Not sure about the 2nd one yet. – Austin Jul 02 '20 at 16:28
  • My cmake is version 3.18.0-rc2. Anyway, I'm going to drop protobuf and gRPC for now. I'm really thankful for your help as it fixed the initial problem. I'll reword the question and mark your answer as the correct one. – Andreas Jul 03 '20 at 10:23
0

in this thread fraser solved the problem but if you need to develop according to protobuf CMake config and find_package command in CMake for finding protobuf libraries. your protobuf library must be compiled with CMake and do not use configure routine . after compile protobuf with CMake , a config file named protobuf-config.cmake will be generated into the prefix/lib/CMake/protobuf directory.

SajadBlog
  • 514
  • 2
  • 12
-1

If the problem is in finding the protobuf installation then your protobuf path isn't on the Environment Variables try adding that to your path and let me know in case if it is there. Will glad to help you more in case this doesn't work