Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically move *.pb.h and *.pb.h under /include and /src directories?

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?

like image 486
Akira Okumura Avatar asked Oct 01 '14 17:10

Akira Okumura


People also ask

What does include directories do in CMake?

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.

How do I include a .h file in CMake?

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 .

Where do I put CMakeLists txt?

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.

Where does CMake look for include files?

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.

What are PB_Max_required_fields and Pb_field_32bit?

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.

Where do the declarations in a protocol buffer file reside?

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.

How to convert a model from Tensorflow saved model format (PB) to H5?

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.

How do I enable dynamic allocation in the nanopb decoder?

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.


1 Answers

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.

like image 88
ComicSansMS Avatar answered Oct 21 '22 22:10

ComicSansMS