0

When I try to use sources generated by protobuf '-std=c++11' is added to my compile flags for target that I try to use them in, causing compilation to fail (cause I'm using post C++11 features).
Following a pretty much minimal example. 3 files, all in single directory:

meson.build

project('test', 'cpp',
version : '0.1',
default_options : ['warning_level=3', 'cpp_std=c++17', 'werror=true']
)

protoc = find_program('protoc')
proto_dep = dependency('protobuf')

gen = generator(protoc,
  output    : ['@BASENAME@.pb.cc', '@BASENAME@.pb.h'],
  arguments : ['--proto_path=@CURRENT_SOURCE_DIR@', '--cpp_out=@BUILD_DIR@', '@INPUT@'])

generated = gen.process(['defs.proto'])

test_exe = executable(
  'test_exe',
  'main.cpp',
  generated,
  dependencies : proto_dep
)

main.cpp

#include "defs.pb.h"
#include <chrono>

int main()
{
  using namespace std::chrono_literals;
}

defs.proto

syntax = "proto2";

package messages;

message Person {
  required string node= 1;
  required string payload= 2;
}

After running meson build && ninja this fails with following error:

[1/3] Compiling C++ object 'test_exe@exe/main.cpp.o'.
FAILED: test_exe@exe/main.cpp.o
c++ -Itest_exe@exe -I. -I.. -I/usr/local/include -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -Wextra -Wpedantic -Werror -std=c++17 -g -pthread -g -std=c++11 -DNDEBUG  -MD -MQ 'test_exe@exe/main.cpp.o' -MF 'test_exe@exe/main.cpp.o.d' -o 'test_exe@exe/main.cpp.o' -c ../main.cpp
../main.cpp: In function ‘int main()’:
../main.cpp:6:24: error: ‘chrono_literals’ is not a namespace-name
   using namespace std::chrono_literals;
                        ^~~~~~~~~~~~~~~
../main.cpp:6:39: error: expected namespace-name before ‘;’ token
   using namespace std::chrono_literals;
                                       ^
[2/3] Compiling C++ object 'test...e/meson-generated_defs.pb.cc.o'.
ninja: build stopped: subcommand failed.

The question is: how to fix the meson.build files so that the sources are correctly generated and only std= flag passed is specified by project( default_options?

paler123
  • 976
  • 6
  • 18

2 Answers2

1

You can disable all dependency cxxflags with

proto_dep = dependency('protobuf').partial_dependency(compile_args : false,
    link_args : true, links : true, includes : true, source : true)

If any important flags will be missing you can add them back manually.

Matt
  • 13,674
  • 1
  • 18
  • 27
0

So I found a dirty solution to the problem, you can get it to compile by passing '-std=c++17' in cpp_args for the target depending on protobuf.

proto_interface = declare_dependency(
  sources : generated,
  dependencies : proto_dep
)

test_exe = executable(
  'test_exe',
  'main.cpp',
  dependencies : proto_interface,
  cpp_args : ['-std=c++17']
)

This does not seem to be very clean, as there are 3 -std flags passed for test_exe sources this way, like in the below snippet from compile_commands.json:

 -std=c++17 -g -pthread -g -std=c++11 -DNDEBUG -std=c++17
paler123
  • 976
  • 6
  • 18