The approach should be correct - godbolt.org/z/aTEmGQ. You should try to create minimal reproducible example and specify compiler version, command line arguments and why it seems does not working.
And it is indeed much safer to use such pragmas in combination with GCC push_options and GCC pop_options to always restore previous behavior so it does not suddenly change depending on included files and the include order.
GCC push_options
andGCC pop_options
to always restore previous behavior so it does not suddenly change depending on included files and the include order.