How can I add compiler warnings in CMake that exclude Protobuf files?
Asked Answered
T

1

0

I'm adding warnings (and treating them as errors) to my C++ project which uses CMake for builds. Initially I used target_compile_options. But as I've added more warnings I've found that they're being triggered by the generated protobuf files.

The build looks like this:

protobuf_generate_cpp(MYAPP_PROTO_SRCS MYAPP_PROTO_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/myapp.proto)

set(MYAPP_SRCS ${MYAPP_PROTO_SRCS} myapp.cpp)

add_executable(myapp ${MYAPP_SRCS})

target_compile_options(myapp PRIVATE -Wall -Werror)

What I want to do is to be able to set warnings for all source files except the generated protobuf file (in this case myapp.pb.cc). Is there any standard way of doing this in CMake?

Tolyl answered 29/4, 2022 at 5:29 Comment(4)
A possible work-around: Separate the protobuf files into a separate sub.directory where you build a (static) library. You can then easily use one set of options for the library source files, and one for the main application source files.Kipkipling
No need for them to be in a different directory, just a separate target will do. If you use an object library then it won't even generate a separate static libraryAbeabeam
Command set_source_files_properties is the proper way for set per-file compiler flags. Not sure why do you treat is a "HACK" or "a bit clunky".Daimon
@Daimon because it only takes the flags as a string which seems more old-fashioned.Tolyl
T
0

I found I can use set_source_files_properties like this:

protobuf_generate_cpp(MYAPP_PROTO_SRCS MYAPP_PROTO_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/myapp.proto)

set(MYAPP_SRCS myapp.cpp)

set_source_files_properties${MYAPP_SRCS} PROPERTIES COMPILE_OPTIONS "-Wall;-Werror")

add_executable(myapp ${MYAPP_SRCS} ${MYAPP_PROTO_SRCS})
Tolyl answered 29/4, 2022 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.