warn_unused_result | warning: ignoring return value of ‘int system(const char*)’
-
using some system calls sent from GUI made with Qt headers.
getting warnings above
how to fix those?./GUI/mainwindow.cpp: In member function ‘void MainWindow::on_actionsensors_triggered()’:
../GUI/mainwindow.cpp:52:11: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
52 | system("gnome-terminal -e "bash -c 'sensors; bash;'""); //otherwise we need to use buffer text file, gonna be symbol overflow -
@JacobNovitsky stackoverflow folks say it can be hidden by adding directive to gcc -Wno-unused-result
what should I insert to pro file to get rid of this warnings?
-
Hi @JacobNovitsky,
warning: ignoring return value of ...
how to fix those?Pretty simple: don't ignore the return value :D
For example:
const int result = system(...); if (result == -1) { // handle the error; check errno. } else (...) { ... }
-
how to fix code above
void MainWindow::on_actionMPU_4_triggered() { system("nohup ~/Qt/Tools/QtCreator/bin/qtcreator /home/supernova/Desktop/MPU/MPU.pro &");}
QMAKE_CXXFLAGS_WARN_ON += -Wno-Wunused-result QMAKE_CXXFLAGS += -Wno-Wunused-result
there is not return value
GUI/mainwindow.cpp: In member function ‘void MainWindow::on_actionMPU_4_triggered()’:
../GUI/mainwindow.cpp:204:8: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
204 | system("nohup ~/Qt/Tools/QtCreator/bin/qtcreator /home/supernova/Desktop/MPU/MPU.pro &"); -
@JacobNovitsky
compiler says after I inputQMAKE_CXXFLAGS_WARN_ON += -Wno-Wunused-result QMAKE_CXXFLAGS += -Wno-Wunused-result
At global scope:
cc1plus: note: unrecognized command-line option ‘-Wno-Wunused-result’ may have been intended to silence earlier diagnostics -
@JacobNovitsky said in warn_unused_result | warning: ignoring return value of ‘int system(const char*)’:
what should I insert to pro file to get rid of this warnings?
Nothing, fix your code as @Paul-Colby shows. Compiler warnings are there for a reason.
how to fix code above
Is it really that hard? Read and understand @Paul-Colby's example and then do the same sort of thing in your code.
void MainWindow::on_actionMPU_4_triggered() { const int result = system("nohup ~/Qt/Tools/QtCreator/bin/qtcreator /home/supernova/Desktop/MPU/MPU.pro &"); if (result == -1) { // handle the error; check errno. } else (...) { ... } }
It is good practice to check return values where failure is significant.
Alternatively, explicitly cast the return to void:
static_cast<void>( system(...) ); // or even this C-style cast (void) system(...);
This has the effect of telling the compiler, and any other reader of the code, that you intended to ignore the return value. The compiler will no longer warn you that you may be ignoring a return value by mistake.
I am still marveling at the use of C++ Qt program to launch Qt Creator to edit the source of, what looks for all the world, like its own source code.
-
@ChrisW67 said in warn_unused_result | warning: ignoring return value of ‘int system(const char*)’:
if (result == -1)
cast still leads to warnings
const int result1 = system ("nohup ~/Qt/Tools/QtCreator/bin/qtcreator /home/supernova/Desktop/MPU/MPU.pro &"); if (result == -1 || result1 == -1) cout << "-1" << endl;
does the task
-
@JacobNovitsky said in warn_unused_result | warning: ignoring return value of ‘int system(const char*)’:
cast still leads to warnings
Which?
Your solution is for sure not correct. -
@Christian-Ehrlicher Actually, my GCC still warns on the cast options:
../test/main.cpp: In function ‘int main(int, char**)’: ../test/main.cpp:7:15: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 7 | (void) system("blah"); | ~~~~~~^~~~~~~~ ../test/main.cpp:8:26: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 8 | static_cast<void>( system("blah") ); | ~~~~~~^~~~~~~~
The system() call is explicitly declared with
warn_unused_result
(__wur below) where, for example, printf() is not. The former is typically unwise to ignore the return value, and the latter is almost universally ignored.$ grep 'int system' /usr/include/stdlib.h extern int system (const char *__command) __wur; $ grep 'int printf' /usr/include/stdio.h extern int printf (const char *__restrict __format, ...);
-
@JonB because I see result and dont need in the context numeric result to be returned, anyway. tripled lines but remove warnings, building time 2-3 sec which is better, but I gonna try all possible methods to reduce it to lowest
thanks for assist
-
@JacobNovitsky said in warn_unused_result | warning: ignoring return value of ‘int system(const char*)’:
@JonB because I see result and dont need in the context numeric result to be returned
Of course you do need to look at the result. If it is non-0 you need to investigate and possibly report to user/outside world. For example, if it returns
-1
that means creating the new process for the command to be executed fails. How can you not need to notice that? It is a recipe for having potentially bad behaviour at user runtime yet not indicating anything about it and then neither you nor user knows something has gone wrong. That is precisely why someone has bothered to mark the functionwarn_unused_result
. If nothing else maybe do something likeqDebug()
the result when non-0. Rather than setting a compiler option to suppress warnings on any/all functions withwarn_unused_result
just for the sake of onesystem()
call somewhere.