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*)’
Forum Updated to NodeBB v4.3 + New Features

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.6k Views 1 Watching
  • 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.
  • Paul ColbyP Offline
    Paul ColbyP Offline
    Paul Colby
    wrote on 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
    2
    • Paul ColbyP Paul Colby

      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 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
      0
      • J JacobNovitsky

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

          @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 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
          3
          • C ChrisW67

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

            Christian EhrlicherC 1 Reply Last reply
            0
            • J JacobNovitsky

              @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

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              0
              • Christian EhrlicherC Christian Ehrlicher

                @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 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
                • JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on 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
                  0
                  • JonBJ JonB

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

                    JonBJ 1 Reply Last reply
                    0
                    • J JacobNovitsky

                      @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

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on 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

                      • Login

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