CMake header files not being found in include - c++

I have library, AprilTags, that uses cmake top build it.
I have another project AIV, that uses AprilTags. I want to keep the apriltags library inside of ~/aiv/apriltags but have another file, front_back_camera_demo that uses some of the files inside of AprilTags library.
So the file structure looks like
~/aiv/build/
/apriltags/CMakeLists.txt
/apriltags/AprilTags/TagDetector.h
/apriltags/AprilTags/*.h
/front_back_camera_demo.cpp
/CMakeLists.txt
When I run cmake on the top level CMakeLists.txt, it builds the AprilTags library successfully, but then I get a
front_back_camera_demo.cpp:72:35: fatal error: AprilTags/TagDetector.h: No such file or directory
error on the line where I include AprilTags/TagDetector.h
Here are the two relevant CMakeLists.txt:
Top level:
cmake_minimum_required(VERSION 2.6)
project(AIV)
add_subdirectory(apriltags)
add_executable(front_back_camera_demo front_back_camera_demo.cpp
Serial.cpp)
target_link_libraries(front_back_camera_demo apriltags)
Inside apriltags:
cmake_minimum_required(VERSION 2.6)
project(apriltags)
#add_definitions(-pg) #"-fopenmp)
# pull in the pods macros. See cmake/pods.cmake for documentation
set(POD_NAME apriltags)
include(cmake/pods.cmake)
file(GLOB SOURCE_FILES "src/*.cc")
include_directories(AprilTags . /opt/local/include)
add_library(apriltags ${SOURCE_FILES})
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(apriltags ${OpenCV_LIBS}) #-pg) #-fopenmp)
pods_use_pkg_config_packages(apriltags eigen3)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(apriltags -L/opt/local/lib/) # MacPorts
special treatment...
else (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
pods_use_pkg_config_packages(apriltags libv4l2)
endif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
pods_install_libraries(apriltags)
file(GLOB header_files "AprilTags/*.h")
pods_install_headers(${header_files} DESTINATION AprilTags/)
pods_install_pkg_config_file(apriltags
LIBS -lapriltags
REQUIRES eigen3 opencv2
VERSION 1.0.0)
add_subdirectory(example)
What am I doing wrong?

Prefer the target_* commands.
apriltags/CMakeLists.txt:
target_include_directories(apriltags
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/AprilTags"
/opt/local/include)
That says, everything that uses the apriltags target will be able to access any includes in ".", that apriltags can also use "." and only apriltags itself uses content under the "AprilTags" and "/opt/local/include" directories.
If you really know what you are doing, you can get even more fine-grained by using generator expressions, but that is not necessary here to get it working.

Related

Including opencv with fetchcontent does not work

I'm trying to include opencv in my c++ project. I want CMake to handle this for me.
Currently I'm at the point where I need to include opencv with the tag: #include <opencv2/opencv.hpp>
The files in the _deps/opencv-src directory throw the following error though:
Scanning dependencies of target VisionC
Building CXX object CMakeFiles/VisionC.dir/main.cpp.o
In file included from /Users/koen/Vakken/MotionVision/VisionC/main.cpp:2:
/Users/koen/Vakken/MotionVision/VisionC/cmake-build-debug/_deps/opencv-src/include/opencv2/opencv.hpp:48:10: fatal error: 'opencv2/opencv_modules.hpp' file not found
#include "opencv2/opencv_modules.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
It seems Like the files can't include their own headers
My CMakeLists file is still pretty simple:
cmake_minimum_required(VERSION 3.17)
project(VisionC)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
opencv
GIT_REPOSITORY https://github.com/opencv/opencv.git
GIT_TAG 4.4.0
)
FetchContent_GetProperties(opencv)
if (NOT opencv_POPULATED)
FetchContent_Populate(opencv)
add_subdirectory(${opencv_SOURCE_DIR} ${opencv_BINARY_DIR})
include_directories(${opencv_SOURCE_DIR}/include) # "/include" should be deleted somehow...
endif ()
FetchContent_MakeAvailable(opencv)
add_executable(VisionC main.cpp)
target_link_libraries(VisionC opencv_lib)
I think the "/include" in the include_directories line hints that the library is included in a directory to "high" or so... I'm not sure how I should change this. If I delete this line I have to include opencv like #include <include/opencv2/opencv.hpp>
I found the solution, this is my cmakelists now:
cmake_minimum_required(VERSION 3.17)
project(VisionC)
set(CMAKE_CXX_STANDARD 20)
# Fetch from git
include(FetchContent)
FetchContent_Declare(
opencv
GIT_REPOSITORY https://github.com/opencv/opencv.git
GIT_TAG 4.4.0
)
FetchContent_GetProperties(opencv)
if (NOT opencv_POPULATED)
FetchContent_Populate(opencv)
endif ()
FetchContent_MakeAvailable(opencv)
# Find on pc
set(OpenCV_DIR ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(OpenCV REQUIRED)
# Link
add_executable(VisionC main.cpp)
target_link_libraries(VisionC ${OpenCV_LIBS})
The solution provided by #Typhaon is not a solution at all because it uses find_package alongside FetchContent and only uses the result of the find_package ignoring the fetched content. In, other words if you remove FetchContent usages it will work. If anyone is wondering what is the proper integration of OpenCV with find_package, take a look at the example provided by OpenCV https://github.com/opencv/opencv/blob/4.x/samples/cpp/example_cmake/CMakeLists.txt.
So, what about fetch_content or even using add_subdriectory?
That kind of usage is currently unsupported
https://github.com/opencv/opencv/issues/20548
But, I currently found a workaround that works for me with the current release. So the problem is that the targets of the OpenCV modules do not include in them the header include dir so when you are trying to link with them you can but you also need to specify the include directory manually. Currently, there are OPENCV_MODULE_opencv_{module_name}_LOCATION variables that point to the source directory of the module and they can be used for obtaining the include dir. For example
target_include_directories(my_target PRIVATE
${OPENCV_MODULE_opencv_core_LOCATION}/include
${OPENCV_MODULE_opencv_highgui_LOCATION}/include
)
target_link_libraries(my_target opencv_core opencv_highgui)
After this, there is an error like this
52 | #include "opencv2/opencv_modules.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
And this is because for some reason the opencv_modules.hpp is being generated in the root of the build directory. But fortunately, there is a variable that is pointing to that directory OPENCV_CONFIG_FILE_INCLUDE_DIR. So the final CMake list file looks like the following
cmake_minimum_required(VERSION 3.23)
project(my_project)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
opencv
GIT_REPOSITORY https://github.com/opencv/opencv.git
GIT_TAG 4.6.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(opencv)
add_executable(my_target main.cpp)
target_include_directories(my_target PRIVATE
${OPENCV_CONFIG_FILE_INCLUDE_DIR}
${OPENCV_MODULE_opencv_core_LOCATION}/include
${OPENCV_MODULE_opencv_highgui_LOCATION}/include
)
target_link_libraries(my_target opencv_core opencv_highgui)
The downside of this workaround is that the mentioned variables can change over the releases, and this currently works for 4.6.0.

Create CMake for NetCDF C++

I am trying to create a CMake file for my project which uses NetCDF. I am very new at CMake (this is my first try), so I apologize if some of this stuff is evident.
To install NetCDF I followed the steps in the git guide shown here
I believe I did the steps correctly. I am trying to use the NetCDF C++ library, but I also had to install the C library for compilation purposes. I did this by using:
sudo apt install libnetcdf-dev
When I run the following command, I receive appropriate output (according to the git guide):
nc-config --has-nc4
So far so good... I hope. Here is my CMakeLists.Txt:
cmake_minimum_required(VERSION 3.16)
project(orbit LANGUAGES CXX)
# ##############################################################################
# Conan Packages
# ##############################################################################
set(CONAN_EXTRA_REQUIRES "") set(CONAN_EXTRA_OPTIONS "")
list(APPEND CONAN_EXTRA_REQUIRES boost/1.73.0) list(APPEND CONAN_EXTRA_REQUIRES eigen/3.3.7)
if(BUILD_SHARED_LIBS) list(APPEND CONAN_EXTRA_OPTIONS "Pkg:shared=True") endif()
include(cmake/Conan.cmake) run_conan()
# ##############################################################################
find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)
find_package (NetCDF REQUIRED) include_directories(${NETCDF_INCLUDES})
# set(SOURCE test.cpp player.cpp player.hpp)
# include_directories(include)
# Search all .cpp files
file(GLOB_RECURSE SOURCE_FILES "*.cpp")
add_executable(test ${SOURCE_FILES})
target_link_libraries(test
PRIVATE
CONAN_PKG::boost
CONAN_PKG::eigen
${NETCDF_LIBRARIES_CXX})
And here is the error output:
CMake Error at CMakeLists.txt:24 (find_package):
By not providing "FindNetCDF.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "NetCDF", but
CMake did not find one.
Could not find a package configuration file provided by "NetCDF" with any
of the following names:
NetCDFConfig.cmake
netcdf-config.cmake
Add the installation prefix of "NetCDF" to CMAKE_PREFIX_PATH or set
"NetCDF_DIR" to a directory containing one of the above files. If "NetCDF"
provides a separate development package or SDK, be sure it has been
installed.
My understanding is that this error comes from me not doing the following line from the git guide correctly:
Make sure that either nc-config is in your PATH, or that the location of netCDFConfig.cmake is in CMAKE_PREFIX_PATH.
I have tried adding the nc-config to my PATH, by running:
export PATH="nc-config:$PATH"
But this does not resolve the issue...
Additionally, I have tried to find a netCDFConfig.cmake file in my computer, however it does not seem to exist. I do have a netCDFCxxConfig.cmake, but I am not sure if this is the same, or how I could go about using it here.
Does anyone have any experience with this issue, and know how to resolve it? Any help would be greatly appreciated, and I do apologize if such a solution has been provided in the past.
Edit:
I was able to get CMake to finally find the file, but now it gets lost looking for another one... Here is the file it was not finding in my original post:
# NetCDF CXX Configuration Summary
####### Expanded from #PACKAGE_INIT# by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was netCDFCxxConfig.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
macro(set_and_check _var _file)
set(${_var} "${_file}")
if(NOT EXISTS "${_file}")
message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
endif()
endmacro()
macro(check_required_components _NAME)
foreach(comp ${${_NAME}_FIND_COMPONENTS})
if(NOT ${_NAME}_${comp}_FOUND)
if(${_NAME}_FIND_REQUIRED_${comp})
set(${_NAME}_FOUND FALSE)
endif()
endif()
endforeach()
endmacro()
####################################################################################
include(CMakeFindDependencyMacro)
if (1)
if(EXISTS "")
set(netCDF_ROOT "")
endif()
if(EXISTS "/usr/lib/x86_64-linux-gnu/cmake/netCDF")
set(netCDF_DIR "/usr/lib/x86_64-linux-gnu/cmake/netCDF")
endif()
find_dependency(netCDF)
set(NETCDF_C_LIBRARY ${netCDF_LIBRARIES})
set(NETCDF_C_INCLUDE_DIR ${netCDF_INCLUDE_DIR})
else()
set(NETCDF_C_LIBRARY "netcdf")
set(NETCDF_C_INCLUDE_DIR "/usr/include")
endif()
if (NOT TARGET netCDF::netcdf)
add_library(netCDF::netcdf UNKNOWN IMPORTED)
set_target_properties(netCDF::netcdf PROPERTIES
IMPORTED_LOCATION "${NETCDF_C_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_C_INCLUDE_DIR}"
)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/netcdf-cxx4Targets.cmake")
This last line is where it all sort of goes wrong. Here is where it has issues finding the netcdf-cxx4Targets.cmake.
The file I have just posted is in the directory: netcd-cxx4/build
netcdf-cxx4Targets.cmake is in the directory:
netcd-cxx4/build/CMakeFiles/Export/lib/cmake/netCDF
So the first should be able to find the other?
The netCDFCxxConfig.cmake file is what you want to use. From quickly looking at the GitHub repository, there is a template for this file (netCDFCxxConfig.cmake.in), but not one for netCDFConfig.cmake.in. Therefore, my conclusion is the maintainers probably changed the config file name at some point, and forgot to update their README documentation to netCDFCxxConfig.cmake.
You can add the location of the netCDFCxxConfig.cmake file to the CMAKE_PREFIX_PATH list variable in your CMake file. Then, link to the imported target netCDF::netcdf defined in the netCDFCxxConfig.cmake file.
...
# ##############################################################################
# Don't do this.
find_file(CMAKE_PREFIX_PATH netCDFCxxConfig.cmake)
# Instead, append the path to the config file using the 'list' command.
list(APPEND CMAKE_PREFIX_PATH "/path/to/installed/netcdf")
# Look for the 'netCDFCxx' package, since that is what the config file is called.
find_package (netCDFCxx REQUIRED)
# Probably don't need this if we use the imported target below.
include_directories(${NETCDF_INCLUDES})
# set(SOURCE test.cpp player.cpp player.hpp)
# include_directories(include)
# Search all .cpp files
file(GLOB_RECURSE SOURCE_FILES "*.cpp")
add_executable(test ${SOURCE_FILES})
# Link to the imported target 'netCDF::netcdf' here instead.
target_link_libraries(test
PRIVATE
CONAN_PKG::boost
CONAN_PKG::eigen
netCDF::netcdf)

How do I include the Eigen library in a CMakelist.txt on windows

I am trying to include the Eigen library to my CMakelist.txt. I have followed the CMake instructions on the Eigen Docs but I am using Jetbrain's Clion and not CMake directly. So I do not know how to use the Cmake commands provided. I have researched around but I don't have have a very good understanding of CMake to write Cmakelists, so I haven't been able to get anything to work yet.
this is what I have been using just to test the serup of the library:
cmake_minimum_required(VERSION 3.17)
project(Eigen_Test)
set(CMAKE_CXX_STANDARD 20)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
add_executable (example example.cpp)
target_link_libraries (example eigen)
add_executable(Eigen_Test main.cpp)
this is the error I have been receiving:
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a package configuration file provided by "Eigen3" (requested
version 3.3) with any of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been installed.
I Have researched many ways to include the library but most methods use command lines which I am unfamiliar with. Also I do not have an Eigen3Config.cmake the only file I have Eigen3Config.cmake.in. I assume there is some install trick that I must not be aware of. If anyone has a way to include clion strictly using a CMakelist.txt, I would be greatly appreciative.
Here a working example with CMake on Windows using the MinGW environment with mingw32-make.exe and g++.exe compiler.
CMakeLists.txt :
# The following lines depends on your project :
cmake_minimum_required(VERSION 3.19)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)
# You have to set these variables as Windows environment variables:
# EIGEN3_INCLUDE_DIR <- %EIGEN3_ROOT%
# EIGEN3_DIR <- %EIGEN3_ROOT%\cmake
#
# EIGEN3_INCLUDE_DIR: variable needed for file %EIGEN3_ROOT%/cmake/FindEigen3.cmake
#
# CMAKE_MODULE_PATH: Search path for the module Eigen3 to be loaded by find_package
#
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
SET( CMAKE_MODULE_PATH "$ENV{EIGEN3_DIR}" )
find_package( Eigen3 3.3 REQUIRED )
# include_directories is needed for the compiler to know where looking for Eigen3 header files to be included
include_directories( ${EIGEN3_INCLUDE_DIR} )
add_executable(PROJECT_NAME FILES...)
You can then call the Eigen3 libraries, such as:
#include <Eigen/Core>
Eigen is a header only library, so you don't have to add it to target_link_library, and you don't need a CMake Macro to detect it.
Instead just add the header file into your include path and you should be set.

Cmake file for C++/CUDA project

I'm having trouble compiling my application using cmake and make. The source files of the project are organized as follows:
SOURCE/
CMakeLists.txt
myApp.cc
include/
classA.hh
classB.hh
src/
classA.cc
classB.cc
classB.cu
My CMakeLists.txt file is as follows:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myApp)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES gdcmCommon vtkgdcm)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)
SET(CUDA_TOOLKIT_ROOT_DIR="/Developer/NVIDIA/CUDA-7.5/")
FIND_PACKAGE(CUDA REQUIRED)
SET(CUDA_PROPAGATE_HOST_FLAGS ON)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
SET(LIB_TYPE SHARED)
SET(CUDA_SEPARABLE_COMPILATION ON)
LINK_DIRECTORIES(/lib/FFTW/INSTALL/lib)
INCLUDE_DIRECTORIES(/lib/FFTW/INSTALL/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
FILE(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
FILE(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
FILE(GLOB cudafile ${PROJECT_SOURCE_DIR}/src/*.cu)
CUDA_ADD_EXECUTABLE(myApp myApp ${headers} ${sources} ${cudafile})
TARGET_LINK_LIBRARIES(myApp ${VTK_LIBRARIES} ${GDCM_LIBRARIES} fftw3)
When I try to compile the project using make (after successfully running cmake) I get:
nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified
CMake Error at myApp_generated_classB.cu.o.cmake:207 (message):
Error generating
../BUILD/CMakeFiles/myApp.dir/src/./myApp_generated_classB.cu.o
Is breaking up the source file of a class into .cc and .cu files problematic?
This is not a comprehensive explanation of what exactly causes the problem stated in the question; nevertheless it solves the problem in a fairly satisfactory way.
First, apparently there is a conflict between using FIND_PACKAGE(VTK) (and hence FIND_PACKAGE(GDCM) which seems to require VTK CMake files for vtkgdcm) and nvcc. This has been recently reported on Mantis. To avoid this conflict, I use:
LINK_DIRECTORIES( {VTK_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({VTK_Directory}/INSTALL/include/vtk-6.2)
LINK_DIRECTORIES( {GDCM_Directory}/INSTALL/lib)
INCLUDE_DIRECTORIES({GDCM_Directory}/INSTALL/include/gdcm-2.4)
instead of,
FIND_PACKAGE(VTK REQUIRED)
FIND_PACKAGE(GDCM REQUIRED)
Second, as for the CUDA part of the project, I put everything into a .cu file and use CUDA_COMPILE(cuda_o myCUDAstudff.cu) to create an object file. Then I use the native C++ compiler to create an executable as usual using ADD_EXECUTABLE( ... ${cuda_o}). Since I am using the native C++ compiler as opposed to nvcc, I need to include the following header files in my kernel (myCUDAstudff.cu) file:
#include <cuda.h>
#include <cuda_runtime.h>
and also link to libcudart in TARGET_LINK_LIBRARIES(), for which I used the shared library. I couldn't figure out a way to do the same with libcudart_static.a though.
Alternatively, one can use CUDA_ADD_EXECUTABLE(... myCUDAstudff.cu) instead of all the above steps (i.e., CUDA_COMPILE(), ADD_EXECUTABLE(),...).

CMake with Google Protocol Buffers

I'm trying to use cmake to build my little project using protocol buffers.
There's a root directory with a number of subdirectories with a number of libraries and executables. My first thought was to have my .proto-files in a subdirectory, but when I read this answer I made a library out of it instead. But when I try to include a messages header in my executable it can't find it.
Error message:
fatal error: msgs.pb.h: No such file or directory
#include "msgs.pb.h"
^
compilation terminated.
I'm running it by creating a dir "build" and then "cmake .. && make" from inside it.
I've looked and it seems the generated files get put in build/messages, so I could do include_directories(build/messages) but that doesn't seem...proper. Is there a proper way of doing this with protobuf? The reason I want the messages file in their own folder is they they'll be used in a lot of different small executables.
Any other general tips for improvements to my CMake-structure is also appreciated :)
Directories:
root
messages
core
server
root/CMakeLists.txt:
project(lillebror)
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0015 NEW)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS date_time log thread system)
find_package(Protobuf REQUIRED)
if(Boost_FOUND)
add_definitions(-std=c++11)
add_subdirectory(messages)
add_subdirectory(core)
add_subdirectory(server)
add_subdirectory(testserver)
endif()
messages/CMakeLists.txt:
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
add_library(messages STATIC ${ProtoSources} ${ProtoHeaders})
target_link_libraries(messages ${Boost_LIBRARIES} ${PROTOBUF_LIBRARY})
core/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
add_library(core STATIC ${SRC_LIST})
target_link_libraries(core messages ${Boost_LIBRARIES})
server/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
include_directories(../messages) <---- I thought this would sove my problem
include_directories(../core/src)
link_directories(../core/build)
add_executable(server ${SRC_LIST})
target_link_libraries(server core ${Boost_LIBRARIES})
server/main.cpp:
#include "msgs.pb.h"
int main()
{
return 0;
}
I think the problem here is that the PROTOBUF_GENERATE_CPP function sets up the .pb.h and .pb.cc files to exist in the build tree, not in the source tree.
This is good practice (not polluting the source tree), but it means that your call include_directories(../messages) is adding the wrong value to the search paths. This is adding the source directory "root/messages", whereas you want "[build root]/messages".
You could probably just replace that line with:
include_directories(${CMAKE_BINARY_DIR}/messages)
However, a more robust, maintainable way might be to set the required include path inside the messages/CMakeLists.txt. To expose this value to the parent scope, this would need to either use set(... PARENT_SCOPE) or:
set(ProtobufIncludePath ${CMAKE_CURRENT_BINARY_DIR}
CACHE INTERNAL "Path to generated protobuf files.")
Then in the top-level CMakeLists.txt, you can do:
include_directories(${ProtobufIncludePath})
If your messages library itself needs to #include the generated protobuf files (this would be normal), then it too should have a similar include_directories call.
Having said all that, if you can specify CMake v2.8.12 as the minimum, you can use the target_include_directories command instead.
In messages/CMakeLists.txt after the add_library call, you'd simply do:
target_include_directories(messages PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Then any other target which depends on messages automatically has the appropriate "messages" include dirs added to its own - you don't need to explicitly call include_directories at all.