Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler doesn't recognize lstat even with the #include's

 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>

 ...
 lstat(name, &st);
 ...

I am using CodeBlocks to write a C program. All the other includes work fine. I checked online and lstat requires the three includes listed at the top of the code, but I still get the error message warning: implicit declaration of function 'lstat' when I try to compile. I do not know what is wrong. If I need to include any extra information to get help, please say.

like image 776
fool Avatar asked Apr 04 '14 00:04

fool


2 Answers

According to lstat(2):

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   lstat():
       _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
       || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L

This means you need to define one of these feature test macros to use lstat(2).

So choose one of those feature test macros that makes sense to your code, such as _BSD_SOURCE, and define it in the very beginning (before you include any header file) of your source file, or you could define it on the compiler command line, such as -D_BSD_SOURCE for gcc.

like image 196
Lee Duhem Avatar answered Sep 22 '22 12:09

Lee Duhem


lstat() is not compliant with strict ANSI standard.You should use your compiler flags from -std=c99 to -std=gnu99 . This would include all the Unix based system.

like image 34
Mantosh Kumar Avatar answered Sep 21 '22 12:09

Mantosh Kumar