編輯問題是:「‘strnlen’的功能隱式聲明」:我怎麼刪除警告/EDIT 編譯(特殊切下的測試只有一個的#include)gcc的警告當方言C99或C11使用
#include <string.h>
void DeleteMe(){
const char* pC = "ABC";
int nLen = strnlen(pC, 255);
char buffer[256];
strncpy(buffer, pC, nLen);
}
由於沒有方言,它編譯沒有任何警告的
Building file: ../EzyThread.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"EzyThread.d" -MT"EzyThread.o" -o "EzyThread.o" "../EzyThread.c"
Finished building: ../EzyThread.c
使方言C99在警告
Building file: ../EzyThread.c
Invoking: GCC C Compiler
gcc -std=c99 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"EzyThread.d" -MT"EzyThread.o" -o "EzyThread.o" "../EzyThread.c"
../EzyThread.c: In function ‘DeleteMe’:
../EzyThread.c:4:13: warning: implicit declaration of function ‘strnlen’ [-Wimplicit-function-declaration]
int nLen = strnlen(pC, 255);
^
Finished building: ../EzyThread.c
使方言C11(我的首選)在警告
Building file: ../EzyThread.c
Invoking: GCC C Compiler
gcc -std=c11 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"EzyThread.d" -MT"EzyThread.o" -o "EzyThread.o" "../EzyThread.c"
../EzyThread.c: In function ‘DeleteMe’:
../EzyThread.c:4:13: warning: implicit declaration of function ‘strnlen’ [-Wimplicit-function-declaration]
int nLen = strnlen(pC, 255);
^
Finished building: ../EzyThread.c
額外的信息:
- 項目
Oher部分失敗下C90編譯,所以沒有信息可以
在Ubuntu 16.04下運行,這是14.04的升級
使用
Eclipse IDE中的C/C++開發人員
版本:Neon.3版本(4.6.3) 版本ID:20170314-1500
人strnlen
給出
STRNLEN(3) Linux Programmer's Manual STRNLEN(3)
NAME
strnlen - determine the length of a fixed-size string
SYNOPSIS
#include <string.h>
size_t strnlen(const char *s, size_t maxlen);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strnlen():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
你的問題是什麼?您向編譯器要求c99或c11合規性,並且這兩個版本都沒有在string.h頭文件中提供'strnlen()'函數。 – nos
使用gnu99,而不是c99。作爲替代,手冊頁告訴你定義_XOPEN_SOURCE ... –
@nos在其他地方在stackoverflow中搜索導致我明白strnlen在string.h中,就像strncpy – brewmanz