I am developing a library which uses Google Protocol Buffers (protobuf
) and CMake. The project has the following directory tree.
MyProject/
MyProject/include/myproject/
MyProject/include/myproject/some_classes.h
MyProject/src/
MyProject/src/some_classes.cc
MyProject/src/foo.proto
MyProject/CMakeList.txt
CMakeList.txt
has the following lines to generate protobuf source and header files.
include_directories(${libCHEC_SOURCE_DIR}/include)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SOURCES)
find_package(Protobuf REQUIRED)
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/src/*.proto")
protobuf_generate_cpp(ProtoSources ProtoHeaders ${ProtoFiles})
list(APPEND EXTLIBS ${PROTOBUF_LIBRARIES})
add_library(MyLibrary SHARED ${SOURCES} ${ProtoSources})
target_link_libraries(MyLibrary ${EXTLIBS})
When I execute cmake
, foo.pb.h
and foo.pb.cc
are generated under the build directory (i.e. the directory where I executed cmake
). It looks that this is the default behavior. But I would like to put foo.pb.h
and foo.pb.cc
under include/myproject
and src
directories, respectively.
How can I change the locations of the files generated by protoc
?
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...]) Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.
To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .
When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory. To open a project, you can point CLion to the top-level CMakeLists. txt and choose Open as Project.
cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.
PB_MAX_REQUIRED_FIELDS: Maximum number of proto2 required fields to check for presence. Default value is 64. Compiler warning will tell if you need this. PB_FIELD_32BIT: Add support for field tag numbers over 65535, fields larger than 64 kiB and arrays larger than 65535 entries. Compiler warning will tell if you need this.
All declarations in the file will reside in the foo::bar namespace. Given a simple message declaration: The protocol buffer compiler generates a class called Foo, which publicly derives from google::protobuf::Message. The class is a concrete class; no pure-virtual methods are left unimplemented.
If the model is saved with the name, “best_model”, it can be loaded using the name of the folder, “ best_model “, instead of saved_model.pb Code to convert a model from tensorflow Saved Model Format (pb) to Keras Saved Model Format (h5) is shown below.
The options can be specified in one of two ways: Using the -D switch on the C compiler command line. NOTE: You must have the same compilation options for the nanopb library and all code that includes nanopb headers. PB_ENABLE_MALLOC: Enable dynamic allocation support in the decoder.
I would strictly advise against placing generated files in the source tree.
CMake puts a lot of effort into separating the build and source trees. Forcing it to give up that separation has several disadvantages. Among the most prominent is the fact that version control will then have to deal with unversioned generated files in the source tree, and furthermore it may no longer be possible to have multiple builds targetting different architectures sharing the same source tree.
A better approach is to keep the files in the binary tree and adjust your target_include_directories
accordingly. There is no shame in using generated files from the binary tree as sources, so don't hesitate to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With