>EDIT 问题是:如何删除警告 /EDIT编译(仅使用一个 #include 的特殊切割测试)
#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
额外信息:
该项目的某些部分无法在 c90 下编译,因此没有可用的信息
在 Ubuntu 16.04 下运行,这是 14.04 的升级
版用
面向 C/C++ 开发人员的 Eclipse IDE
版本: 霓虹灯.3 发布 (4.6.3)内部版本号:20170314-1500
曼·斯特伦
给
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
在 Linux 和 macOS 等 POSIX 系统上,您需要将其指定的宏定义为功能测试宏,方法是将-D_POSIX_C_SOURCE=200809L
传递给编译器或在#include
之前编写#define _POSIX_C_SOURCE 200809L
。
在Windows上,您不需要任何特殊的宏,可以直接使用strnlen
。
注意C标准实际上并没有定义strnlen
,而是strnlen_s
,这是相似的,但并不完全相同。但是,许多实现不包括它,即使对于那些包含它的实现,您也可能需要在包含string.h
之前将__STDC_WANT_LIB_EXT1__
定义为 1。
_GNU_SOURCE或_POSIX_C_SOURCE
您可以参考此手册页 https://www.man7.org/linux/man-pages/man3/strnlen.3.html
#define _GNU_SOURCE
或
#define _POSIX_C_SOUCRE 200809L