HDUHomework icon indicating copy to clipboard operation
HDUHomework copied to clipboard

Implicit declaration of function 'ftruncate'

Open imba-tjd opened this issue 7 years ago • 1 comments

see this code. I have included unistd.h and sys/types.h, but it doesn't work. However, my program did't report 'bus error' and crash. qq 20180629173139 qq 20180629152812

imba-tjd avatar Jun 29 '18 09:06 imba-tjd

I know this is so old, but there is no issue in answering it now. Since you have enabled all warnings, you will have to compile with:

gcc -D_XOPEN_SOURCE=<version> ...

<version> has to be 500 or more for this specific feature (ftruncate(...)). This is shown in its manual page and in your screenshot.

You can find more explanation in feature_test_macros(7); run the command:

man 7 feature_test_macros

Hope this help! Have a nice day.

alrshdn avatar May 11 '24 03:05 alrshdn

@alrshdn Thank you! I have no experence in developing Linux C program.

You are right. I tested it again and the warning disappers. ~~Accroding to the doc, it's because I'm using a newer glibc now.~~ Well, it seems that /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L means I need to use glibc >= 2.12 and define _POSIX_C_SOURCE >= 200809L. Althouth I tested printf("%ld\n", _POSIX_C_SOURCE); and it actually prints 200809 now.

I also tried to ask ChatGPT. After some instructions, it gives the solution of #define _POSIX_C_SOURCE 200809L, which is bigger than 200112L. Students thesedays should have a better learning experience than I did.

imba-tjd avatar May 26 '24 12:05 imba-tjd