私のCファイルはclock_gettime()
. これが機能するためには、man ページに従って、 が
含まれて<time.h>
おり、 に定義_POSIX_C_SOURCE
されています。(200112L)
SYNOPSIS
#include <time.h>
int clock_getres(clockid_t clk_id, struct timespec *res);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);
Link with -lrt (only for glibc versions before 2.17).
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
clock_getres(), clock_gettime(), clock_settime():
_POSIX_C_SOURCE >= 199309L
次のオプションを使用してコンパイルおよびリンクします。
CFLAGS = -g -Wall -Wextra -Werror -O3 -std=c99 -include $(PROJ_SETTINGS_INC) -lrt
、およびPROJ_SETTINGS_INC
設定を含む h ファイルに設定されます。
これまでのところ、問題ありません。
ここで、設定ファイルを変更して を使用するuint16_t
ので<stdint.h>
、設定 h ファイルに含めます。
clock_gettime()
コンパイラは、それが暗黙の宣言であると不平を言うようになりました。
設定ファイルをint
の代わりに使用するように戻し、uint16_t
インクルードを に削除すると<stdint.h>
、コンパイルが再び機能します。
<stdint.h>
設定に h ファイルを含めると、なぜでのコンパイルが中断されるのclock_gettime()
ですか?
私の最善の推測は、stdint
定義を再定義することですが、ソースの最初の行でインクルードが行われたかのようにディレクティブが機能POSIX
するため、意味がありません。-include
ここに例があります(ジョン・ボリンジャーの答えに照らして、何がうまくいかないのか理解し始めましたが、とにかくこれを書くと思いました)。
bar.h:
#include <stdint.h>
struct s {
uint16_t a;
};
foo.c
#define _POSIX_C_SOURCE (199309L)
#include <stdio.h>
#include <time.h>
int main(void)
{
struct s s;
struct timespec now;
s.a = 42;
clock_gettime(CLOCK_REALTIME, &now);
printf("answer: %d, time: %lld\n", s.a, (long long) now.tv_sec);
return 0;
}
ビルド:
gcc foo.c -include bar.h
奇妙なことに、これは有用な警告を与えます。私の元のアプリケーションでは、暗黙の宣言エラーのみが発生しました。
foo.c:1:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
#define _POSIX_C_SOURCE (199309L)
^
In file included from /usr/include/stdint.h:25:0,
from /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stdint.h:9,
from ./bar.h:1,
from <command-line>:1:
/usr/include/features.h:230:0: note: this is the location of the previous definition
# define _POSIX_C_SOURCE 200809L