Search code examples
c++linuxcmakeinotifyfcntl

Linux: Conflicts using inotify with fcntl


I'm having a strange linking issue after I included inotify in my program to monitor changes to a filesystem. The project includes <fcntl.h> in many other source files. However, when I include <sys/inotify.h> in the source file which is doing the directory monitoring, I get this error:

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

My project uses CMake, although that doesn't seem to be relevant for finding inotify. It IS finding the inotify declarations to my knowledge, since when I included , it threw an error that inotify_init() and the other functions I used were not defined. Inotify includes fcntl and is partially built on top of some of the functionality there, so my first thought was that it's importing a different version of fcntl than the rest of my program.

In 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 //...

The only thing that changed was ObjectManager.cc, with the addition of sys/notify and the implementation of the watcher (not included because this is a linking issue):

#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

Where Control.h declares #include <fcntl.h>. This is the closest issue I found, related to some problems in the implementation of different fcntl headers for userspace usage. https://lkml.org/lkml/2008/9/16/98

The same problem occurs on Linux 2.6 running on Centos 6 and Linux 4.0 running on Centos 7.

Any ideas on what is causing this error and how to successfully include inotify?


Solution

  • Resolution: A function definition lacked a semicolon at the END of ObjectManager.h right before a #endif, and the resulting GCC error that propagated through the next includes in a complicated manner, resulting in a strange preprocessor error in fcntl.h.