Tuesday, November 16, 2021

[SOLVED] How to add C++ flag -Wno-unused-function on cmake?

Issue

I added on my CMakeLists.txt on Android:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-variable -Wno-unused-function -Wno-error")

but I get multiple errors like this:

 error: unused function 'createParserError' [-Werror,-Wunused-function]

There's nothing that could rewrite CMAKE_CXX_FLAGS.


Solution

With modern cmake, you can use add_compile_options and target_compile_options instead of setting the variable directly

Thus in your context you can write

add_compile_options(" -Wno-unused-variable -Wno-unused-function -Wno-error")

to add the compile options to all targets or simply

target_compile_options(LIBRARY_NAME SCOPE "-Wno-unused-variable -Wno-unused-function -Wno-error")

to set those options for one target. If you need it to be cross platform

if(${PLATFORM_1})
  add_compile_options("WARNING_FLAGS_FOR_PLATFORM1")
elseif(${PLATFORM_2})
  add_compile_options("WARNING_FLAGS_FOR_PLATFORM2")
endif()

replace the variable by your required values



Answered By - Xavier Groleau