Программы на C++. Форум разработчиков
-
Anonymous
Cmake не создавать исходные файлы Protobuf как часть проекта C ++
Сообщение
Anonymous »
Этот вопрос является продолжением этого предыдущего вопроса. br /> Вот моя структура каталогов: < /p>
Код: Выделить всё
protobuf-example/
proto/
CMakeLists.txt
message.proto
CMakeLists.txt
main.cpp
< /code>
[h4]proto/CMakeLists.txt
[/h4]
Код: Выделить всё
set(PROTO_FILES message.proto)
set(GENERATED_PROTO_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
file(MAKE_DIRECTORY ${GENERATED_PROTO_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
add_library(proto_files STATIC ${PROTO_SRCS})
target_include_directories(proto_files PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(proto_files PUBLIC ${Protobuf_LIBRARIES})
< /code>
[h4]message.proto
[/h4]
Код: Выделить всё
syntax = "proto3";
message Person {
string name = 1;
int32 age = 2;
string email = 3;
}
< /code>
[h4]CMakeLists.txt
[/h4]
Код: Выделить всё
cmake_minimum_required(VERSION 3.10)
project(ProtobufExample LANGUAGES CXX)
find_package(Protobuf REQUIRED)
add_executable(protobuf_example main.cpp)
target_link_libraries(protobuf_example PRIVATE proto_files)
< /code>
[h4]main.cpp
[/h4]
Код: Выделить всё
#include
#include
#include "message.pb.h"
void serializePerson(const std::string& filename) {
Person person;
person.set_name("John Doe");
person.set_age(30);
person.set_email("john.doe@example.com");
std::ofstream output(filename, std::ios::binary);
if (!person.SerializeToOstream(&output)) {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79423984/cmake-not-building-protobuf-source-files-as-part-of-a-c-project[/url]
1739054957
Anonymous
Этот вопрос является продолжением этого предыдущего вопроса. br /> Вот моя структура каталогов: < /p>
[code]protobuf-example/
proto/
CMakeLists.txt
message.proto
CMakeLists.txt
main.cpp
< /code>
[h4]proto/CMakeLists.txt[/code] [/h4]
[code]set(PROTO_FILES message.proto)
set(GENERATED_PROTO_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
file(MAKE_DIRECTORY ${GENERATED_PROTO_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})
add_library(proto_files STATIC ${PROTO_SRCS})
target_include_directories(proto_files PUBLIC ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(proto_files PUBLIC ${Protobuf_LIBRARIES})
< /code>
[h4]message.proto[/code] [/h4]
[code]syntax = "proto3";
message Person {
string name = 1;
int32 age = 2;
string email = 3;
}
< /code>
[h4]CMakeLists.txt[/code] [/h4]
[code]cmake_minimum_required(VERSION 3.10)
project(ProtobufExample LANGUAGES CXX)
find_package(Protobuf REQUIRED)
add_executable(protobuf_example main.cpp)
target_link_libraries(protobuf_example PRIVATE proto_files)
< /code>
[h4]main.cpp[/code] [/h4]
[code]#include
#include
#include "message.pb.h"
void serializePerson(const std::string& filename) {
Person person;
person.set_name("John Doe");
person.set_age(30);
person.set_email("john.doe@example.com");
std::ofstream output(filename, std::ios::binary);
if (!person.SerializeToOstream(&output)) {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79423984/cmake-not-building-protobuf-source-files-as-part-of-a-c-project[/url]