0

I'm working on a C++ Project with CMake and VCPKG for Dependencies.

After I installed by dependencies: OpenCV, gRPC, Protobuf and OpenSSL, I get a bunch of warnings and a linker error when I start my "main" Application.

Configuration is executed as

cmake -S . -B build-files/ "-DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake"

This is output of CMake: Pastebin.

Build is executed as

cmake --build build-files/ --target myproject.Core -v

This is its output:

/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/Makefile2 myproject.Core
/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_progress_start /Users/oliverkarger/Desktop/myproject/build-files/CMakeFiles 8
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/Makefile2 src/Core/CMakeFiles/myproject.Core.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/DependInfo.cmake --color=
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.grpc.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/build
make[3]: Nothing to be done for `libraries/Protos/CMakeFiles/myproject.Protos.dir/build'.
[ 62%] Built target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/src/Core /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/src/Core /Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/DependInfo.cmake --color=
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/Core.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Core
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/build
[ 75%] Linking CXX executable /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core
cd /Users/oliverkarger/Desktop/myproject/build-files/src/Core && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_link_script CMakeFiles/myproject.Core.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o CMakeFiles/myproject.Core.dir/Core.cc.o -o /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core   -L/Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib  -L/Users/oliverkarger/Desktop/myproject/build/archive  -lgRPC /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libre2.a /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libcares.a /Users/oliverkarger/Desktop/myproject/build/archive/libmyproject.Protos.a -lresolv -lgRPC -lprotobuf
ld: library not found for -lprotobuf
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core] Error 1
make[2]: *** [src/Core/CMakeFiles/myproject.Core.dir/all] Error 2
make[1]: *** [src/Core/CMakeFiles/myproject.Core.dir/rule] Error 2
make: *** [myproject.Core] Error 2

Project: myproject.Protos is a self-built library dependency of myproject.core

CMake Files:

  • CMakeLists.txt
# Project Name
project("myproject")

# Options
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED 17)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ../../../build/archive)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../../build/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ../../../build/runtime)

# Application Metadata
set (CMAKE_PROJECT_VERSION_MAJOR 1)
set (CMAKE_PROJECT_VERSION_MINOR 0)
set (CMAKE_PROJECT_VERSION_PATCH 0)
set (CMAKE_PROJECT_VERSION_TWEAK 0)

# CMake Required Version
cmake_minimum_required(VERSION 3.24)

# Add VCPKG Toolchain
include (./vcpkg/scripts/buildsystems/vcpkg.cmake)
set (VCPKG_TARGET_TRIPLET x64-osx)

# Packages
find_package(gRPC CONFIG REQUIRED)
find_package(protobuf CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)

# Third Party Includes
include_directories(./vcpkg/installed/x64-osx/include)
include_directories("include")
include_directories(${OpenCV_INCLUDE_DIRS})

# Doxygen
find_package(Doxygen)
if (DOXYGEN_FOUND)
    # set input and output files
    set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
    set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

    # request to configure the file
    configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
    message("Doxygen build started")

    # note the option ALL which allows to build the docs together with the application
    add_custom_target( docs ALL
        COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM )
else (DOXYGEN_FOUND)
  message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)

# Google Test
include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Directories
add_subdirectory(src/Core)
add_subdirectory(src/CoreGUI)
add_subdirectory(src/CameraServer)
# add_subdirectory(src/TestRunner)
add_subdirectory(libraries/Math)
add_subdirectory(libraries/ImageProcessing)
add_subdirectory(libraries/DataStructures)
add_subdirectory(libraries/Protos)
add_subdirectory(libraries/CommonCC)
  • src/myproject.Core/CMakeLists.txt
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.cc *.h)
set(SOURCES ${SOURCES})

add_executable(myproject.Core ${SOURCES})

target_link_libraries(myproject.Core gRPC)
target_link_libraries(myproject.Core re2::re2 c-ares::cares)
target_link_libraries(myproject.Core myproject.Protos)
  • libraries/myproject.Protos/CMakeLists.txt
file(GLOB_RECURSE PROTOS LIST_DIRECTORIES true *.proto)
set(PROTOS ${PROTOS})

add_library(myproject.Protos ${PROTOS})

target_link_libraries(myproject.Protos gRPC)
target_link_libraries(myproject.Protos protobuf)

get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)
protobuf_generate(TARGET myproject.Protos LANGUAGE cpp)
protobuf_generate(TARGET myproject.Protos LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")

As far as I understand the issue is that cmake can't find the library file for protobuf. But I checked with vcpkg (is a submodule of my project) that the file and path both exist. I also tried passing the path to the .a-file directly by using

target_link_library(myproject.(Core|Protos) vcpkg/installed/x64-osx/lib/libprotobuf.a)

but that didn't resolve my issues.

How to link libprotobuf properly?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Oliver Karger
  • 105
  • 1
  • 11
  • 1
    Linking with `protobuf` is a **wrong usage** of the `find_package` results. Correct usage is displayed when you install protobuf via vcpkg (see e.g. the message in [that question](https://stackoverflow.com/questions/64404450/compile-grpc-c-examples-with-vcpkg)): it provides **targets** prefixed with `protobuf::`. E.g. for link with the library `libprotobuf` use target `protobuf::libprotobuf`. – Tsyvarev Nov 20 '22 at 21:54

0 Answers0