Protobufs构建,但未使用CMake链接到主程序

时间:2018-10-20 22:48:37

标签: c++ cmake protocol-buffers

我正在尝试构建和执行使用protobufs的程序。我正在使用CMake3构建该项目。问题是当我进行项目时出现此错误

fatal error: 
      'google/protobuf/stubs/common.h' file not found
#include <google/protobuf/stubs/common.h>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

我将发布该项目的目录结构

directory structure image

directory structure expanded

这是我的CMakeLists.txt文件

TestCode /

下的最高级别
cmake_minimum_required(VERSION 3.5)
set (CMAKE_CXX_STANDARD 11)

add_subdirectory(protobufs)
add_subdirectory(main)

CMakeList.txt在main /

cmake_minimum_required(VERSION 3.5)

set (CMAKE_CXX_STANDARD 11)

add_executable(test_exe 
    main.cc 
)
target_link_libraries(protos ${PROTOBUF_LIBRARIES})
propbufs下的

CMakeLists /

cmake_minimum_required(VERSION 3.5)

set (CMAKE_CXX_STANDARD 11)

find_package(Protobuf REQUIRED)

PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS 
    dog.proto
)
add_library(protos ${PROTO_SRCS})

target_link_libraries(protos 
    PUBLIC
    ${PROTOBUF_LIBRARIES}
)

target_include_directories(protos
    PUBLIC
    ${PROTOBUF_INCLUDE_DIRS}
    ${CMAKE_CURRENT_BINARY_DIR}
)

这就是我构建项目的方式

cd build
cmake ..
make -j8

我无法弄清楚为什么找不到主程序的protobuf。如果我只是尝试使用make来成功构建protobuf。任何想法将不胜感激。

编辑:我解决了它。 在main /的CMakeLists.txt文件中,我需要在target_link_libraries中添加test_exe

cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 11)


add_executable(test_exe 
    main.cc 
)
target_link_libraries(test_exe protos)

1 个答案:

答案 0 :(得分:0)

此错误:

  找不到

'google / protobuf / stubs / common.h'文件

是因为您没有告诉编译器您的protobuf头文件在哪里。您可以在计算机上搜索“ google / protobuf / stubs / common.h”吗?如果尚未安装,则需要安装(使用系统软件包管理器)。如果已安装,则需要在CMake中为父目录(包含“ google”的父目录)添加一个包含目录规则。