How to use ftruncate in c99 without warning

梦想与她 提交于 2019-12-23 09:57:34

问题


I want to use ftruncate function in my code. I have to compile with option std=c99. I get warning:

In function ‘test’:
warning: implicit declaration of function ‘ftruncate’ [-Wimplicit-function-declaration]

I tied to find on the Internet any solution which can solve this problem but I don't succeeded in.

I use ftrucnate because I want to clear content of an opened file after I get lock (flock).


回答1:


Since ftruncate() isn't a standard C function, and you've asked for standards enforcement, you need to define the appropriate feature test macros (see feature_test_macros(7)).

From the ftruncate(2) manpage:

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

In other words, to expose the ftruncate() function you must define one of these macros, for example:

gcc -c -std=c99 -D_XOPEN_SOURCE=500 myfile.c



回答2:


FatalError's answer did not work for me. Mostly all you have to do for it to work is:

#include <unistd.h>


来源:https://stackoverflow.com/questions/26806764/how-to-use-ftruncate-in-c99-without-warning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!