-2
  • Protobuf Version : 4.22.2
  • CMake Version : 3.27.0
  • C++ Complier version : MSVC 19.36.32537.0
  • My English is poor, so I hope you will be patient to read it, please.
  • I compile protobuf to static library and link it to my project, but always come out this error:

link error information

  • this is my project main CMakeLists.txt code :

    project(test CXX C)
    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
    set(CMAKE_CXX_EXTENSIONS OFF)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_BUILD_TYPE release CACHE STRING "Build Type" FORCE)
    # 可加
    # MSVC runtime library flags are selected by an abstraction.
    # New in CMake 3.15: https://cmake.org/cmake/help/latest/policy/CMP0091.html
    if(POLICY CMP0091)
    cmake_policy(SET CMP0091 NEW)
    endif()

    # option() honors normal variables
    # New in CMake 3.13: https://cmake.org/cmake/help/latest/policy/CMP0077.html
    if (POLICY CMP0077)
    cmake_policy(SET CMP0077 NEW)
    endif (POLICY CMP0077)
     # Suppress linker warnings about files with no symbols defined.
    string(APPEND CMAKE_STATIC_LINKER_FLAGS " /ignore:4221")

    # use English language (0x409) in resource compiler
    string(APPEND CMAKE_RC_FLAGS " -l0x409")

    # help other modules find protobuf
    set(Protobuf_INCLUDE_DIR
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        CACHE INTERNAL "Path to include folder for Protobuf")
    # Find the protoc Executable which need to be cache
    set(Protobuf_PROTOC_EXECUTABLE
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/WIN64/Release/protoc.exe
        CACHE INTERNAL "Path to find the protoc Executable")
    # FindProtobuf.cmake make Protobuf_LIBRARIES equal Protobuf_LIBRARY
    # set Protobuf_LIBRARY for definding target protobuf::libprotobuf
    if(APPLE)
    set(Protobuf_LIBRARY
        ${CMAKE_CURRENT_SOURCE_DIR}/libprotobuf.a
        CACHE INTERNAL "Path to libprotobuf.a")
    elseif(WIN32)
    set(Protobuf_LIBRARY
        ${CMAKE_CURRENT_SOURCE_DIR}/lib/WIN64/Release/libprotobuf.lib
        CACHE INTERNAL "Path to libprotobuf.lib")
    endif()

    find_package(Protobuf REQUIRED)
    protobuf_generate_cpp(GENERATED_SRC GENERATED_HEADER
                        message.proto)
    link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib/WIN64/Release/absl)
    add_executable(main main.cpp
                        ${GENERATED_SRC} ${GENERATED_HEADER})
    target_link_libraries(main PRIVATE protobuf::libprotobuf)
    target_include_directories(main PRIVATE ${Protobuf_INCLUDE_DIR}
                                    ${CMAKE_CURRENT_BINARY_DIR})
    # 【显式设置】release模式下的编译定义(重点是这里)
    target_compile_definitions(main PRIVATE _ITERATOR_DEBUG_LEVEL=0)
    # 【显式设置】release模式下的编译选项(重点是这里)
    target_compile_options(main PRIVATE /MD)
  • and this is my project tree : project tree
  • If anyone can help me, thank you very much!!

I have tried link the absl libraries and add Release Build Type but it still comes out this error.

  • please don't post images of error messages, post the text directly. If possible also translate the errors into english – Alan Birtles Aug 17 '23 at 07:17
  • At a guess I would say that your manual configuration of various Protobuf variables has confused the FindProtobuf.cmake file and it does not include absl in its public link interface. At the same time, you never link your program with absl explicitly. – Botje Aug 17 '23 at 07:49
  • I am sorry that I have not post the text directly. And I found this question can be solved according to https://github.com/protocolbuffers/protobuf/issues/12292. – user20941337 Aug 21 '23 at 01:20

0 Answers0