Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. warn_unused_result | warning: ignoring return value of ‘int system(const char*)’

warn_unused_result | warning: ignoring return value of ‘int system(const char*)’

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JacobNovitsky
    wrote on 15 Oct 2023, 00:53 last edited by
    #1

    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

    J 1 Reply Last reply 15 Oct 2023, 03:16
    0
    • J JacobNovitsky
      15 Oct 2023, 00:53

      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

      J Offline
      J Offline
      JacobNovitsky
      wrote on 15 Oct 2023, 03:16 last edited by
      #2

      @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?

      C 1 Reply Last reply 15 Oct 2023, 03:42
      0
      • P Offline
        P Offline
        Paul Colby
        wrote on 15 Oct 2023, 03:16 last edited by
        #3

        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 (...) {
          ...
        }
        
        J 1 Reply Last reply 15 Oct 2023, 03:24
        2
        • P Paul Colby
          15 Oct 2023, 03:16

          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 (...) {
            ...
          }
          
          J Offline
          J Offline
          JacobNovitsky
          wrote on 15 Oct 2023, 03:24 last edited by
          #4

          @Paul-Colby

          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 &");

          J 1 Reply Last reply 15 Oct 2023, 03:33
          0
          • J JacobNovitsky
            15 Oct 2023, 03:24

            @Paul-Colby

            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 &");

            J Offline
            J Offline
            JacobNovitsky
            wrote on 15 Oct 2023, 03:33 last edited by
            #5

            @JacobNovitsky
            compiler says after I input

            QMAKE_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

            1 Reply Last reply
            0
            • J JacobNovitsky
              15 Oct 2023, 03:16

              @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?

              C Offline
              C Offline
              ChrisW67
              wrote on 15 Oct 2023, 03:42 last edited by ChrisW67
              #6

              @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.

              J 1 Reply Last reply 15 Oct 2023, 06:33
              3
              • C ChrisW67
                15 Oct 2023, 03:42

                @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.

                J Offline
                J Offline
                JacobNovitsky
                wrote on 15 Oct 2023, 06:33 last edited by JacobNovitsky
                #7

                @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

                C 1 Reply Last reply 15 Oct 2023, 07:13
                0
                • J JacobNovitsky
                  15 Oct 2023, 06:33

                  @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

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 15 Oct 2023, 07:13 last edited by
                  #8

                  @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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  C 1 Reply Last reply 15 Oct 2023, 07:35
                  0
                  • C Christian Ehrlicher
                    15 Oct 2023, 07:13

                    @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.

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on 15 Oct 2023, 07:35 last edited by
                    #9

                    @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, ...);
                    
                    1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      JonB
                      wrote on 15 Oct 2023, 07:47 last edited by
                      #10

                      I don't know why we/the OP is even debating this. system() could fail for a number of (possibly nasty) reasons. So why would you want to write code which didn't check its return result anyway?

                      J 1 Reply Last reply 15 Oct 2023, 09:11
                      0
                      • J JonB
                        15 Oct 2023, 07:47

                        I don't know why we/the OP is even debating this. system() could fail for a number of (possibly nasty) reasons. So why would you want to write code which didn't check its return result anyway?

                        J Offline
                        J Offline
                        JacobNovitsky
                        wrote on 15 Oct 2023, 09:11 last edited by
                        #11

                        @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

                        J 1 Reply Last reply 15 Oct 2023, 09:34
                        0
                        • J JacobNovitsky
                          15 Oct 2023, 09:11

                          @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

                          J Offline
                          J Offline
                          JonB
                          wrote on 15 Oct 2023, 09:34 last edited by JonB
                          #12

                          @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 function warn_unused_result. If nothing else maybe do something like qDebug() the result when non-0. Rather than setting a compiler option to suppress warnings on any/all functions with warn_unused_result just for the sake of one system() call somewhere.

                          1 Reply Last reply
                          3

                          9/12

                          15 Oct 2023, 07:35

                          • Login

                          • Login or register to search.
                          9 out of 12
                          • First post
                            9/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved