1

I try to complier a file on ubuntu64-16.04. In CMakeLists.txt, I set

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

But there is an error in the standard file both in "sys/stat.h" and "sys/fcntl.h". I do not know what happened. The error message is listed below.

When I try to not include "stat.h" the same error occurred in "fcntl.h". So I think as the compilation order, it should compile "stat.h" firstly and then "fcntl.h", so I think the error occurred in "Serial.h" for I only include both the file in "Serial.h". I find a similar question here. But I really have the file "sys/cedfs.h".

-rw-r--r-- 1 root root 15412 Feb  6 04:04 sys/cdefs.h
#ifndef SERIAL_H
#define SERIAL_H

#include "Header.h"
#include <sys/cdefs.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/ioctl.h>

class Serialport
{
public:
        Serialport(string port);//定义Serialport类的成员函数,
        Serialport();
        ~ Serialport();

        int open_port(string port);
        int set_opt(int nSpeed = 115200 , int nBits = 8, char nEvent ='N', int nStop = 1);
        bool send(char *str);
        bool sendAngle(float angleYaw,float anglePitch,float Dis, bool big, bool insight ,bool get);

        void readMode(int &carMode);
        uint8_t readAngle(CarData &cardata);

private:
         int fd ;
         char tmpchar[19];
         const char *buffer;
         unsigned char rData[255];
         char guapichar[4];
};

#endif
Scanning dependencies of target SentryVision
[ 16%] Building CXX object bin/CMakeFiles/SentryVision.dir/main.cpp.o
In file included from /usr/include/features.h:367:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:482,
                 from /usr/include/c++/5/string:38,
                 from /home/wang/Documents/copy_from_zhou/src/../include/Header.h:5,
                 from /home/wang/Documents/copy_from_zhou/src/main.cpp:1:
/usr/include/x86_64-linux-gnu/sys/stat.h:102:1: error: expected unqualified-id before string constant
 __BEGIN_DECLS
 ^
bin/CMakeFiles/SentryVision.dir/build.make:62: recipe for target 'bin/CMakeFiles/SentryVision.dir/main.cpp.o' failed
make[2]: *** [bin/CMakeFiles/SentryVision.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:85: recipe for target 'bin/CMakeFiles/SentryVision.dir/all' failed
make[1]: *** [bin/CMakeFiles/SentryVision.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

For the main is so long, I show the declaration:

#include "../include/Header.h"
#include "../include/ArmorDetector.h"
#include "../include/AngleCalculate.h"
#include "../include/Serial.hpp"
#include "../include/RMVideoCapture.hpp"
#include "../include/WatchDog.hpp"
#include "../include/DxImageProc.h"
#include "../include/GxIAPI.h"
#include <thread>
#define MEMORY_ALLOT_ERROR -1

Mat m_image;
bool is_implemented = false;
int64_t m_pixel_color = 0;
char *m_rgb_image = NULL;
GX_DEV_HANDLE g_device = NULL;    //设备句柄
GX_FRAME_DATA g_frame_data = {0}; //采集图像参数
pthread_t g_acquire_thread = 0;   //采集线程ID
thread t;
Mat ImgDown;
Serialport stm32("/dev/ttyUSB0");
ArmorDetector armorDown;
ArmorData armorDataDown;
CarData carData;


bool g_get_image = false;         //采集线程是否结束的标志
//获取图像大小并申请图像数据空间
int PreForImage();
//释放资源
int UnPreForImage();
//采集线程函数
void *ProcGetImage(void *param);
//获取错误信息描述
void GetErrorString(GX_STATUS error_status);
//开起相机设备
bool OpenDevice();
//在得到图片之后寻找目标
void findTarget(unsigned int &lost);

And the Header.h:

#ifndef HEADER_H
#define HEADER_H

#include "config.h"
#include <string>
#include <iostream>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;

typedef struct __ArmorData__
{
    int Cam;
    bool isGet;
    bool shoot;
    float angleYaw;
    float anglePitch;
    float x;
    float y;
    float z;
    float atocDistance;
    bool isBig;
    int picWidth;
    __ArmorData__()
    {
        Cam = picWidth = 0;
        isBig = isGet = shoot = false;
        atocDistance = anglePitch = angleYaw = x = y = z = 0;
    }
}ArmorData;

typedef struct __CarData__
{
    float absAngleYaw;
    float absAnglePitch;
    __CarData__()
    {
        absAnglePitch = absAngleYaw = 0;
    }
}CarData;


#endif

config.h:

#ifndef CONFIG_H
#define CONFIG_H

//#define watchdog

//#define RED
#define BLUE

#define DEBUG0
#define DEBUG1
// #define DEBUG2
// #define DEBUG3

#endif

My directory tree is:

.
├── CMakeLists.txt
├── autoDog.sh
├── autoSentry.sh
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.5.1
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   └── a.out
│   │   │   └── CompilerIdCXX
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── a.out
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeError.log
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile.cmake
│   │   ├── Makefile2
│   │   ├── Progress
│   │   │   ├── 1
│   │   │   └── count.txt
│   │   ├── TargetDirectories.txt
│   │   ├── cmake.check_cache
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.c
│   │   ├── feature_tests.cxx
│   │   └── progress.marks
│   ├── Makefile
│   ├── bin
│   │   ├── CMakeFiles
│   │   │   ├── CMakeDirectoryInformation.cmake
│   │   │   ├── SentryVision.dir
│   │   │   │   ├── CXX.includecache
│   │   │   │   ├── DependInfo.cmake
│   │   │   │   ├── build.make
│   │   │   │   ├── cmake_clean.cmake
│   │   │   │   ├── depend.internal
│   │   │   │   ├── depend.make
│   │   │   │   ├── flags.make
│   │   │   │   ├── link.txt
│   │   │   │   └── progress.make
│   │   │   └── progress.marks
│   │   ├── Makefile
│   │   └── cmake_install.cmake
│   └── cmake_install.cmake
├── include
│   ├── AngleCalculate.h
│   ├── ArmorDetector.h
│   ├── DxImageProc.h
│   ├── GxIAPI.h
│   ├── Header.h
│   ├── RMVideoCapture.hpp
│   ├── Serial.hpp
│   ├── WatchDog.hpp
│   └── config.h
├── src
│   ├── AngleCalculate.cpp
│   ├── ArmorDetector.cpp
│   ├── CMakeLists.txt
│   ├── RMVideoCapture.cpp
│   ├── Serial.cpp
│   └── main.cpp
└── watchdog
    ├── CMakeLists.txt
    └── src
        ├── CMakeLists.txt
        └── main.cpp

14 directories, 59 files
  • Usually you include your own headers after standard/library headers. What happens if you move the `#include "Header.h"` after the other includes? The error appears from within `Header.h`, before you even include the standard headers. There might be something wrong in `Header.h`. Also, which file is the code you showed? Probably not `main.cpp`? There is no mention of `Serial.h` on the compiler error... – Erlkoenig Jul 19 '19 at 04:41
  • The reason I doubt "Serial.h" is that when I do not include "sys/stat.h", the error occurred in "fcntl.h". As the compilation order, firstly compile "sys/stat.h" and then compile "fcntl.h", so I think it should be in the "Serial.h". The code in header is listed below: ```c++ #ifndef HEADER_H #define HEADER_H #include "config.h" #include #include #include #include #include #include using namespace std; using namespace cv; #endif ``` – Harry Potter Jul 19 '19 at 05:16
  • That really sounds like an error prior to the include. Please show your main.cpp and the Header.h (edit the question text, not as a comment), as those are the only files mentioned in the error - your Serial.h isn't mentioned at all and therefore probably not part of the problem. Also, "using namespace" is discouraged and a cause for all kinds of problems. – Erlkoenig Jul 19 '19 at 05:25
  • Try including cdefs.h first, inside Header.h. This should actually not be necessary. There might be something wrong with your C or C++ library installation. Perhaps headers from different toolchains are mixed. Also, names like `__ArmorData__` are forbidden and reserved for the standard library. – Erlkoenig Jul 19 '19 at 05:44
  • Thanks, I changed struct name and include the header. But also error. – Harry Potter Jul 19 '19 at 05:48
  • Try compiling with `-H` to see whether `cdefs.h` is correctly included, and whether it defines `__BEGIN_DECLS` properly. – Erlkoenig Jul 19 '19 at 06:22
  • Sorry, could you please show me the code. I have no idea with what you say. I do not find -H in make opinions. I am new with the complier – Harry Potter Jul 19 '19 at 08:42
  • It's probably easiest to just call the compiler manually for this test and pass `-H` and all the other necessary flags directly. – Erlkoenig Jul 19 '19 at 08:44

0 Answers0