-1

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?

Lars
  • 233
  • 2
  • 9
  • 1
    Can you give us the minimal amount of code needed to replicate the problem? – David Schwartz Aug 29 '16 at 22:34
  • I will update all of the relevant #includes in the source file using inotify. However, if I knew the minimal code that was relevant in producing the problem, I would probably be able to figure it out -- this is a tough one and any one or many of the hundreds of source files could be contributing. – Lars Aug 29 '16 at 22:42
  • 1
    Then how do you expect us to figure it out without even having access to those files? – David Schwartz Aug 29 '16 at 22:51
  • I suspect you would need to have had a similar problem. The issue is that the version of fcntl being called indirectly through sys/inotify is not properly linked with the kernel, and therefore __BEGIN_DECLS is unrecognized. However, there is another inclusion of fcntl.h by a file which is included by ObjectManager.cc, which was not giving problems until I added inotify. Hoping these symptoms ring some bells for someone. – Lars Aug 29 '16 at 23:30

1 Answers1

0

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.

Lars
  • 233
  • 2
  • 9