Native compilation of Force C99 in clang

I am trying to write C99 code that should compile on solaris in c99 mode , but I do not have access to the Solaris machine. Instead, I am trying to do this on OSX using clang. However, using (min.c file):

#include <stdio.h>
#include <string.h>

int main() {
  printf("%d\n", (int) strnlen("hello world", 5));
  return 0;
}

I do not receive any errors or warnings about strnlen

$ clang -std=c99 -pedantic-errors -Wall -Wextra min.c
$ ./a.out
5

although it strnlenis an extension of posix 2008 .

This is using

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Is there any way to get clangto strictly match c99 so that I have a better chance of Solaris tolerance?

+4
source share
2 answers

Based on @DietrichEpp answer , the following works:

clang -std=c99 -Wpedantic -Wall -Wextra -D_POSIX_C_SOURCE=200112L min.c
min.c:5:24: warning: implicit declaration of function 'strnlen' is invalid in C99
      [-Wimplicit-function-declaration]
  printf("%d\n", (int) strnlen("hello world", 5));
                       ^
  1 warning generated.

This support page supports this :

POSIX.1-2001 C99, , C99, POSIX.1-2001.

OSX 10.12.6 Xcode clang.

+1

, - .

(Ubuntu 17.04, x86_64, clang 4.0.0) :

$ clang -std=c99 -pedantic-errors -Wall -Wextra min.c
min.c:5:24: warning: implicit declaration of function 'strnlen' is invalid in C99 [-Wimplicit-function-declaration]
  printf("%d\n", (int) strnlen("hello world", 5));
                       ^
1 warning generated.
$ 

strnlen, C, . , str, wcs mem, , <string.h>.

C99 7.1.3p2 :

, ( 7.1.4) , undefined.

: M.M, strnlen; . .

undefined (), . , , .

, , . , stpcpy POSIX, . -std=c99 -pedantic-errors:

#include <stdio.h>
#include <string.h>
int main(void) {
    char s[10];
    stpcpy(s, "hello");
    puts(s);
}

.

+3

Source: https://habr.com/ru/post/1685893/


All Articles