Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

Can Clang warn me when I use non-standard functions provided by the GNU C Library?

I am writing a C program on OS X and compiling it with clang. I am quite new to C and I have come to understand that some functions like getline() are useful but non-standard. I would like to be able to compile the program on a system where the GNU C Library is not available. Not knowing exactly what functions are non-standard, I am hoping there is a command-line switch for clang to warn me whenever I use such functions. Is there?

Output from clang --version:

Apple LLVM version 5.1 (clang-503.0.40)

Upvotes: 3

Views: 480

Answers (4)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

You can use feature test macros (see also: XSH 2.2.1 POSIX.1 Symbols) to request visibility of only a particular set of standard interfaces. In particular,

-D_XOPEN_SOURCE=600

on the command line or

#define _XOPEN_SOURCE 600

should expose POSIX base plus the XSI option for the outdated 2001 version of POSIX (the latest OSX supports). If you want just base, without XSI, define _POSIX_C_SOURCE to 200112L.

Upvotes: 3

user2485710
user2485710

Reputation: 9801

No there isn't, mostly due to the fact that pretty much any project that I know of usually makes a distinction between:

  • the compiler
  • the standard library
  • the ABI library

and typically this 3 things are kept separated into different project and they are designed to not step into each other. Sometimes there is even a more fragmented vision about this, where the compiler is splitted into backend, frontend, and multiple other modular projects and plugins like what happens in the big llvm project with clang.

As Petesh already suggested, your best options is some building system with this kind of ability where you can script the behaviour of your building process and detect if a macro fails or not, or if the targeted system offers the X features or not.

My suggestion, especially for a beginner, is cmake and its own wiki .

Upvotes: 1

fuz
fuz

Reputation: 93014

You can define a feature-test-macro like _POSIX_C_SOURCE. Conforming implementations are required to hide any extensions (i.e. extra library functions) when _POSIX_C_SOURCE is present and no implementation-defined feature-test macros (like _BSD_SOURCE or _GNU_SOURCE) are defined:

#define _POSIX_C_SOURCE 200809L

If you try to use an extension and this macro is defined you either get a warning (ANSI mode) or an error (C99) mode, as the prototype for the function is not present.

For Mac OS X, there is no POSIX.1 2008 support. Therefore, you have to define the macro like this to get POSIX.1 2001 support:

#define _POSIX_C_SOURCE 200112L

Notice that a couple of functions were removed in POSIX.1 2008, but that shouldn't matter as these functions are usually (read: always) present on the target system as it probably also supports POSIX.1 2001.

Upvotes: 1

Anya Shenanigans
Anya Shenanigans

Reputation: 94644

There is no command line switch to say 'tell me if this is nonstandard'. The most common way of developing code that needs to be compatible across a variety of platforms is the gnu autotools suite, which gives you a configuration script which checks for various functionality and allows you to code around it, or have the system give up before going any further.

Upvotes: 2

Related Questions