c++ - Linux:使用 inotify 与 fcntl 发生冲突

标签 c++ linux cmake inotify fcntl

在我的程序中包含 inotify 来监视文件系统的更改后,我遇到了一个奇怪的链接问题。该项目包括<fcntl.h>在许多其他源文件中。但是,当我包括<sys/inotify.h>时在进行目录监视的源文件中,我收到此错误:

/usr/include/fcntl.h:30:1: error: expected initializer before ‘extern’ __BEGIN_DECLS

我的项目使用 CMake,尽管这似乎与查找 inotify 无关。据我所知,它正在查找 inotify 声明,因为当我包含 时,它抛出了一个错误,指出 inotify_init() 和我使用的其他函数未定义。 Inotify 包含 fcntl,并且部分构建在其中的一些功能之上,因此我的第一个想法是它导入的 fcntl 版本与我的程序的其余部分不同。

在 ObjectManager.h 中:

#ifndef MANAGE_OBJECT_H
#define MANAGE_OBJECT_H


#include "config.h"

//includes all lua headers under extern 'C'
#include <lua.hpp>

#include <list>
#include <unordered_map>
#include <pthread.h>

class ObjectManager //...

唯一改变的是 ObjectManager.cc,添加了 sys/notify 和观察器的实现(不包括在内,因为这是一个链接问题):

#include "config.h"

#include "ObjectManager.h"
#include "Control.h"

#ifdef OBJECT_MANAGER_ENABLED

#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
#include <unistd.h>
#include <fstream>
#include <sys/inotify.h> 
//... inotify implementation

Control.h 声明 #include <fcntl.h> 的位置。 这是我发现的最接近的问题,与实现用户空间使用的不同 fcntl header 的一些问题有关。 https://lkml.org/lkml/2008/9/16/98

在 Centos 6 上运行的 Linux 2.6 和 Centos 7 上运行的 Linux 4.0 上也会出现同样的问题。

关于导致此错误的原因以及如何成功包含 inotify 有什么想法吗?

最佳答案

解决方案:函数定义在 #endif 之前的 ObjectManager.h 的 END 处缺少分号,并且由此产生的 GCC 错误以复杂的方式传播到下一个包含,从而导致 fcntl.h 中出现奇怪的预处理器错误。

关于c++ - Linux:使用 inotify 与 fcntl 发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215857/

相关文章:

c++ - C++中的文本编辑器?

C++:变量不受 Void 函数影响

php - Apache2.4.11 虚拟主机配置不工作

cmake - 你如何使用 CTEST_CUSTOM_PRE_TEST?

CMake:从子目录(又名库)中将目录包含到父目录中

c++ - 过滤范围、lambda 和 is_sorted

c++ - C++ 中的 "new (&variable) value;"有什么作用?

linux - 如何可移植地扩展使用 mmap() 访问的文件

linux - bash 中的 unix 历史命令 "!$"是什么?

c++ - 混合CPP OpenCV/主程序C