Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

silly C++ syntax has not been declared

I'm trying to take a Qt C++ project originally written for Windows and cross compile it for embedded Linux. Now this program compiles find and works on Windows, so this problem must be something OS specific (which I didn't think happend with Qt code) or configuration related, but I'm having a hard time tracking it down because I don't fully understand C++ syntax.

my make command:

arm-linux-gnueabihf-g++ -c -march=armv7-a -marm -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 -O2 -O3 -Wall -W -D_REENTRANT -DCHL80Net -DPHASE_TO_NEUTRAL -DCHL80NET -DCANLCD_BUILD -DQT_NO_DEBUG -DQT_XML_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../mkspecs/qws/linux-am335x-g++ -I. -I../include/QtCore -I../include/QtGui -I../include/QtXml -I../include -I. -IApp -IApp/Model/ModelSim -IApp/Model -I. -IPages -IApp/GUI/Widgets -IApp/GUI/Pages -IApp/GUI -IApp/GUI/Widgets -IApp/GUI/Pages/Util -IApp/GUI/Pages -IApp/Log4Qt -I.obj -o .obj/CanInterface.o App/Can/CanInterface.cpp

The error:

App/Can/CanInterface.cpp: In member function ‘void CanInterface::closeConnection()’: App/Can/CanInterface.cpp:68:5: error: ‘::close’ has not been declared make: * [.obj/CanInterface.o]
Error 1

Here's the line of code in question:

void CanInterface::closeConnection()
{
    ::close(m_socket);
    m_socket = -1;

I thought this didn't look like valid code at all at first, but I don't really know C++ so I had to do a little research, it seems like this ::function() syntax is to ensure resolution occurs from the global namespace instead of the local one.

So what I'm trying to find out is what namespace should have declaired this close() function. If my understanding of this code is correct I don't need to look in the CanInterface class for the undeclaired function, but it's parent class?

In the header file for the CanInterface class I found this:

class CanInterface : public QObject 
{
        Q_OBJECT

which I think means that it inharents from a QObject class. So:

  1. Am I on the right track?
  2. How do I know if I need to look at the QObject class for the missing close() function or if I need to keep going up? Does ::close somehow tell me how many levels of nested classes I need to search through?
  3. Any other ideas or tips for further investigating this?
like image 802
Mike Avatar asked Dec 06 '25 17:12

Mike


1 Answers

You are correct, in that ::some_function() calls a function from the global scope.

This includes all C library or system functions. You can lookup global functions on a *nix system with man, e.g.

man close

which gives on my Ubuntu

NAME
       close - close a file descriptor

SYNOPSIS
       #include <unistd.h>

       int close(int fd);

So, to fix your compile error, you must include unistd.h somewhere in your C++ source or in a surrounding header file.

You can get the same information, when you google for man close, e.g. http://linux.die.net/man/2/close

like image 187
Olaf Dietsche Avatar answered Dec 10 '25 04:12

Olaf Dietsche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!