0

My program includes two headers for external libraries: nifti_io.h and CImg.h (downloaded from here and here, respectively). The compiler (GCC 8.2.0 inside Code::Blocks) returned these strange errors in /usr/include/dirent.h - the build messages in the IDE are:

g++ -Wall -fexceptions -O2 -c /tmp/image_test.cpp -o tmp/obj/Release/image_test.o
g++ -o bin/Release/image_test obj/Release/image_test.o -s ../z/libz.so ../znz/bin/Release/libznz.so ../nifti/bin/Release/libnifti.so
In file included from /usr/include/nifti/nifti1_io.h:18,
             from /tmp/image_test.cpp:4:
/usr/include/dirent.h:99:5: error: expected identifier before numeric constant
     DT_UNKNOWN = 0,
     ^~~~~~~~~~
/usr/include/dirent.h:99:5: error: expected ‘}’ before numeric constant
In file included from /tmp/CImg.h:173,
             from /tmp/image_test.cpp:7:
/usr/include/dirent.h:98:3: note: to match this ‘{’
   {
   ^
In file included from /usr/include/nifti/nifti1_io.h:18,
             from /tmp/image_test.cpp:4:
/usr/include/dirent.h:99:5: error: expected unqualified-id before numeric constant
     DT_UNKNOWN = 0,
     ^~~~~~~~~~
In file included from /usr/include/features.h:424,
             from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,
             from /usr/include/stdio.h:27,
             from /usr/include/nifti/nifti1_io.h:9,
             from /tmp/image_test.cpp:4:
/usr/include/dirent.h:402:1: error: expected declaration before ‘}’ token
 __END_DECLS
 ^~~~~~~~~~~
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 0 warning(s) (0 minute(s), 0 second(s))

After commenting out literally everything (all the program does after including those libraries is int main() { return (0); }) the error still remains.

#include "nifti/nifti1_io.h"
#include "CImg.h"
int main() { return ( 0 ); }

Including only one of them (either nifti1_io.h or CImg.h) compiles without errors or warnings. Including both of them returns the output above.

Does anyone have a clue what causes this and how to solve it? My guess is that it has something to do with compiler directives, specifically the ones involving extern "C". I would like to keep the included header files intact though because they're not mine.

  • I don't know the source of the error. But you can avoid this clash by not including both headers in the same cpp file. Do you need them both in combination, or can you split the code? If not, you have to write a wrapper for one of them. – Martin Fehrs Sep 20 at 8:35
  • Yes I need both files; one is for 3D images and one is for picture functions in general. With a wrapper do you just mean a C++ header file that includes one of the files -- and could you expand a bit on why that would work? Thanks! – alle_meije Sep 20 at 8:48
  • No I mean a cpp/hpp pair. The cpp file includes one of the problematic header files. Now you have to wrap the needed functions and/or classes. For wrapping functions you have do implement a function forwarding the arguments to the original function. Declare it in the new hpp file. For classes you can use the pimpl-Idiom. Now include the new header file in your application and use the wrappers instead of the original functions. – Martin Fehrs Sep 20 at 9:49
  • Do the errors change if you reverse the order of your #include lines? – JaMiT Sep 20 at 15:52
  • Changing the order doesn't work. Strangely enough including the second header from a .hpp file. Does work. If anyone can answer with an explanation I'll put the green thingy on! – alle_meije Sep 21 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.