【发布时间】:2016-08-29 22:01:15
【问题描述】:
在我的程序中包含 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 和 watcher 的实现(不包括在内,因为这是一个链接问题):
#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 标头的一些问题有关。 https://lkml.org/lkml/2008/9/16/98
在 Centos 6 上运行的 Linux 2.6 和在 Centos 7 上运行的 Linux 4.0 也会出现同样的问题。
关于导致此错误的原因以及如何成功包含 inotify 的任何想法?
【问题讨论】:
-
你能给我们复制问题所需的最少代码吗?
-
我将使用 inotify 更新源文件中所有相关的#include。但是,如果我知道与产生问题相关的最少代码,我可能能够弄清楚——这是一个艰难的过程,数百个源文件中的任何一个或多个都可以参与其中。
-
那你指望我们如何在没有访问这些文件的情况下解决这个问题?
-
我怀疑您可能遇到过类似的问题。问题是通过 sys/inotify 间接调用的 fcntl 版本没有与内核正确链接,因此 __BEGIN_DECLS 无法识别。但是,ObjectManager.cc 包含的文件中还包含 fcntl.h,直到我添加了 inotify 才出现问题。希望这些症状能为某人敲响警钟。
标签: c++ linux cmake inotify fcntl