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. close(int fd) not recognized in QT both fcntl.h and unistd.h are included
Forum Updated to NodeBB v4.3 + New Features

close(int fd) not recognized in QT both fcntl.h and unistd.h are included

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreator 4.9.1closeqt5.11
18 Posts 5 Posters 2.4k 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.
  • V vicky_mac
    8 Jul 2021, 09:22

    hi i am doing some memory read and write using mmap and open function in my application.

    But when i am going for closing the file descriptor close() functions is not recognized and program densest compile.

    #include"mainwindow.h"
    #include<unistd.h>
    #include<fcntl.h>
    
    MainWindow::ReadMem()
    {
    int fd = open("/dev/mem",O_RDWR|O_SYNC);
    int* add =(int*)mmap(NULL,2048,PROT_READ | PROT_WRITE,MAP_SHARED,dh,0);
    //now some operation as needed
    munmap(add,2048);
    close(fd);/// this line not compiling
    }
    
    

    // Nomatching function to call function close```
    code_text

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 8 Jul 2021, 09:51 last edited by
    #4

    @vicky_mac

    man 2 close
    

    and include the header file mentioned there.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    K 1 Reply Last reply 8 Jul 2021, 09:54
    0
    • J jsulm
      8 Jul 2021, 09:51

      @vicky_mac

      man 2 close
      

      and include the header file mentioned there.

      K Offline
      K Offline
      KroMignon
      wrote on 8 Jul 2021, 09:54 last edited by
      #5

      @jsulm said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

      and include the header file mentioned there.

      According to thread title, unistd.h has been included, so it should not be the problem.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      J J 2 Replies Last reply 8 Jul 2021, 09:55
      0
      • K KroMignon
        8 Jul 2021, 09:54

        @jsulm said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

        and include the header file mentioned there.

        According to thread title, unistd.h has been included, so it should not be the problem.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 8 Jul 2021, 09:55 last edited by
        #6

        @KroMignon Yeah, careful reading is something I should do more often :-)

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • K KroMignon
          8 Jul 2021, 09:54

          @jsulm said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

          and include the header file mentioned there.

          According to thread title, unistd.h has been included, so it should not be the problem.

          J Offline
          J Offline
          JonB
          wrote on 8 Jul 2021, 09:56 last edited by JonB 7 Aug 2021, 09:56
          #7

          @KroMignon
          I had not noticed that information in the title. (Was it added later?! :) )

          @vicky_mac
          What you do not say is where --- inside which class --- your code is located. You are likely to need ::close() as @KroMignon has said, did you at least try it?

          V 1 Reply Last reply 8 Jul 2021, 12:15
          0
          • J JonB
            8 Jul 2021, 09:56

            @KroMignon
            I had not noticed that information in the title. (Was it added later?! :) )

            @vicky_mac
            What you do not say is where --- inside which class --- your code is located. You are likely to need ::close() as @KroMignon has said, did you at least try it?

            V Offline
            V Offline
            vicky_mac
            wrote on 8 Jul 2021, 12:15 last edited by
            #8

            @JonB hi Joh
            I have edited my question with include and function detail. Please advise what exactly needs to be done.

            J 1 Reply Last reply 8 Jul 2021, 13:46
            0
            • V vicky_mac
              8 Jul 2021, 12:15

              @JonB hi Joh
              I have edited my question with include and function detail. Please advise what exactly needs to be done.

              J Offline
              J Offline
              JonB
              wrote on 8 Jul 2021, 13:46 last edited by JonB 7 Aug 2021, 13:47
              #9

              @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

              Please advise what exactly needs to be done.

              @JonB said:

              You are likely to need ::close() as @KroMignon has said, did you at least try it?

              Your plain close() will be MainWinow::close().

              V 1 Reply Last reply 9 Jul 2021, 04:50
              1
              • J JonB
                8 Jul 2021, 13:46

                @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                Please advise what exactly needs to be done.

                @JonB said:

                You are likely to need ::close() as @KroMignon has said, did you at least try it?

                Your plain close() will be MainWinow::close().

                V Offline
                V Offline
                vicky_mac
                wrote on 9 Jul 2021, 04:50 last edited by
                #10

                @JonB
                Hi Jon,

                I am getting the same error even after MainWindow::close(fd)

                J J 2 Replies Last reply 9 Jul 2021, 04:54
                0
                • V vicky_mac
                  9 Jul 2021, 04:50

                  @JonB
                  Hi Jon,

                  I am getting the same error even after MainWindow::close(fd)

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 9 Jul 2021, 04:54 last edited by
                  #11

                  @vicky_mac @JonB did not say you need to use MainWindow::close(fd)! What he means is that your MainWindow probably has its own close() method nd if you write close(...) that one is used.
                  Why don't you simply try what @KroMignon suggested?!

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply 9 Jul 2021, 05:05
                  2
                  • J jsulm
                    9 Jul 2021, 04:54

                    @vicky_mac @JonB did not say you need to use MainWindow::close(fd)! What he means is that your MainWindow probably has its own close() method nd if you write close(...) that one is used.
                    Why don't you simply try what @KroMignon suggested?!

                    V Offline
                    V Offline
                    vicky_mac
                    wrote on 9 Jul 2021, 05:05 last edited by
                    #12

                    @jsulm

                    Hi jsulm,

                    Sorry to bother again but i don't know which namespace to use in this case i tried std:: that also doesn't work.
                    Can you please suggest which namespace to be used. Thank you

                    J K 2 Replies Last reply 9 Jul 2021, 05:07
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 9 Jul 2021, 05:07 last edited by Christian Ehrlicher 7 Sept 2021, 05:07
                      #13

                      @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                      Can you please suggest which namespace to be used. Thank you

                      So hard to read @KroMignon 's answer?

                      You have to specify the namespace :: to use standard open() function:
                      int fd = ::open("/dev/mem",O_RDWR|O_SYNC);

                      Basic c++ stuff ...

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

                      1 Reply Last reply
                      0
                      • V vicky_mac
                        9 Jul 2021, 05:05

                        @jsulm

                        Hi jsulm,

                        Sorry to bother again but i don't know which namespace to use in this case i tried std:: that also doesn't work.
                        Can you please suggest which namespace to be used. Thank you

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 9 Jul 2021, 05:07 last edited by
                        #14

                        @vicky_mac Did you read what @KroMignon suggested?

                        ::close(fd);
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • V vicky_mac
                          9 Jul 2021, 05:05

                          @jsulm

                          Hi jsulm,

                          Sorry to bother again but i don't know which namespace to use in this case i tried std:: that also doesn't work.
                          Can you please suggest which namespace to be used. Thank you

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 9 Jul 2021, 05:22 last edited by
                          #15

                          @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                          Hi jsulm,
                          Sorry to bother again but i don't know which namespace to use in this case i tried std:: that also doesn't work.
                          Can you please suggest which namespace to be used. Thank you

                          In C++, there are namespaces to prevent name conflicts (cf. https://en.cppreference.com/w/cpp/language/namespace).
                          When you are writing code in a C++ class, the default namespace is the current class.
                          So when you are calling a close() function, the compiler first try to find this function in the current namespace.

                          To avoid this, you have to specify full namespace. In case of "standard functions" like close(int), the namespace is empty. So the fullname of standard open(int), in C++ is ::open().

                          This are C++ basics, it is always helpful to learn the basics ;)

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          V 1 Reply Last reply 9 Jul 2021, 05:24
                          3
                          • K KroMignon
                            9 Jul 2021, 05:22

                            @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                            Hi jsulm,
                            Sorry to bother again but i don't know which namespace to use in this case i tried std:: that also doesn't work.
                            Can you please suggest which namespace to be used. Thank you

                            In C++, there are namespaces to prevent name conflicts (cf. https://en.cppreference.com/w/cpp/language/namespace).
                            When you are writing code in a C++ class, the default namespace is the current class.
                            So when you are calling a close() function, the compiler first try to find this function in the current namespace.

                            To avoid this, you have to specify full namespace. In case of "standard functions" like close(int), the namespace is empty. So the fullname of standard open(int), in C++ is ::open().

                            This are C++ basics, it is always helpful to learn the basics ;)

                            V Offline
                            V Offline
                            vicky_mac
                            wrote on 9 Jul 2021, 05:24 last edited by
                            #16

                            @KroMignon
                            Thanx for the reply kromignon.

                            I am familiar with C but not much C++. I think i'll have to read the basics of C++ for using QT

                            K 1 Reply Last reply 9 Jul 2021, 05:28
                            0
                            • V vicky_mac
                              9 Jul 2021, 05:24

                              @KroMignon
                              Thanx for the reply kromignon.

                              I am familiar with C but not much C++. I think i'll have to read the basics of C++ for using QT

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 9 Jul 2021, 05:28 last edited by
                              #17

                              @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                              I think i'll have to read the basics of C++ for using QT

                              Yes, or you will have a very hard time.
                              I know it, because I had the same learning way. I start with C/Assembler for years before programming in C++.
                              Event if they seem identical, they are definitively not.

                              Programming C++ as a C developer is not the right way to do.
                              Take time to learn C++ basics will save you many days later.

                              Qt is a C++ framework, so it is mandatory to understand C++ to be able to use Qt.

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              1 Reply Last reply
                              2
                              • V vicky_mac
                                9 Jul 2021, 04:50

                                @JonB
                                Hi Jon,

                                I am getting the same error even after MainWindow::close(fd)

                                J Offline
                                J Offline
                                JonB
                                wrote on 9 Jul 2021, 07:32 last edited by
                                #18

                                @vicky_mac said in close(int fd) not recognized in QT both fcntl.h and unistd.h are included:

                                I am getting the same error even after MainWindow::close(fd)

                                Both @KroMignon and I said from the start and multiple times to try ::close(fd);. Same with ::open(). How much clearer than that can one be?

                                If you want to be "safe", every single C (not C++) function you want to use should be ::function().

                                1 Reply Last reply
                                0

                                13/18

                                9 Jul 2021, 05:07

                                • Login

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