I am using GCC 4.9 on ubuntu 15.04. I am coding in eclipse CDT. This is a C program with the dialect set to c99. For some reason my compiler keeps warning me about this...
warning: implicit declaration of function ‘posix_memalign’ [-Wimplicit-function-declaration]
I am not sure why. I have #include<stdlib.h> at the top and when I use eclipse the ctrl+click posix_memalign it takes me to the function declaration in stdlib.h. Why am I getting this warning?
I just tried changing the dialext to std=gnu99 and this fixed the issue. Is posix_memalign not included with c99?
The #define _POSIX_C_SOURCE 200809L and other feature test macros have to be defined before any #include lines.
This is because the macros tell the standard C library headers which features it should provide in addition/instead of the standard C library features; the features are "locked" at the point of #include.
posix_memalign() is provided by stdlib.h, but only if POSIX.1-2001 or later are enabled; this means defining _POSIX_C_SOURCE as 200112L or larger (the L is there because it is an integer constant of long type), or _XOPEN_SOURCE with 600 or larger.
The error shown only occurs when
The macros were not defined when stdlib.h was included
or
stdlib.h was not included
or
The C library implementation does not provide POSIX.1 features
Using GCC in Ubuntu, it has to be one of the first two, because the C library most definitely does provide these POSIX.1 features.
Related
I try to check the output of the get_wch function from the ncurses library, on Archlinux install.
But when I call the function, I got this GCC error:
main.c:6:15: warning: implicit declaration of function "get_wch";
I linked the lib like my GCC command-line suggest:
gcc main.c -lncursesw -o exec
I have also check that ncurses is installed:
core/ncurses 6.2-1 [installed]
multilib/lib32-ncurses 6.2-1 [installed]
And I see the header generated at the lib compile time that allow me to use "widec" functions, when I list header files in the "usr/include" directory.
#include <curses.h>
int main() {
initscr();
int test = 0;
int result = get_wch(&test);
printf("Caractère : {} / Function code : {}\n", test, result);
endwin();
return 0;
}
I don't understand how to use this lib. And the available "documentation" seems to play against me...
The gcc warning
main.c:6:15: warning: implicit declaration of function "get_wch";
tells you that there is no function prototype for get_wch. X/Open Curses specified all of the wide-character functions conditionally (to avoid breaking old programs). That's summarized in the ncurses manual page:
You must also enable the wide-character features in the header
file when compiling for the wide-character library to use the
extended (wide-character) functions. The symbol which enables
these features has changed since XSI Curses, Issue 4:
Originally, the wide-character feature required the symbol
_XOPEN_SOURCE_EXTENDED but that was only valid for XPG4
(1996).
Later, that was deemed conflicting with _XOPEN_SOURCE defined
to 500.
As of mid-2018, none of the features in this implementation
require a _XOPEN_SOURCE feature greater than 600. However,
X/Open Curses, Issue 7 (2009) recommends defining it to 700.
Alternatively, you can enable the feature by defining
NCURSES_WIDECHAR with the caveat that some other header file
than curses.h may require a specific value for _XOPEN_SOURCE
(or a system-specific symbol).
The prototype for get_wch uses wint_t (an integer which can hold a "wide character" such as Unicode). The manual page lists these types which are used in the wide-character ncursesw library (and function prototypes): cchar_t, wchar_t and wint_t
If you want to use a function prototype using any of those types, your program should turn on the feature. As mentioned before, defining NCURSES_WIDECHAR is simplest.
This question already has answers here:
GCC with -std=c99 complains about not knowing struct timespec
(3 answers)
Closed 2 years ago.
I am currently experiencing the error
Unknown type name 'ssize_t'
I had a look at Where is ssize_t defined in Linux?
but the error remained.
I added the
#include <sys/types.h>
My function is
ssize_t ss_size(sparse_set_ptr sparse_set)
{
return false;
}
What could be the cause of the problem and can it be fixed?
Also I am running C Executable Language Standard: C99
From `man ssize_t:
ssize_t
Include: <sys/types.h>. Alternatively, <aio.h>, <monetary.h>,
<mqueue.h>, <stdio.h>, <sys/msg.h>, <sys/socket.h>, <sys/uio.h>,
or <unistd.h>.
Try all these one by one, with one of them it should work.
Note that even if you include <sys/types.h>, the type might not be defined if you specify a strict C standard with GCC (-std=c99) rather than the GNU variant (-std=gnu99). In fact, you don't need to explicitly include <sys/types.h> with POSIX 2008 (2018) or the prior version, POSIX 2004; it is effectively included automatically.
If you're including <sys/types.h> and still see the problem, then you need to enable the POSIX extensions, probably with #define _XOPEN_SOURCE 700 — the number's easier to remember than #define _POSIX_C_SOURCE 200809L which is a valid alternative. Both these select POSIX 2008; there are other values for other versions of POSIX. There are a few subtle differences between X/Open and POSIX (X/Open might till specify a few things that POSIX does not), but they're minimal and seldom relevant. The #define must appear before any system header is included. It could be specified on the command line as -D_XOPEN_SOURCE=700.
You have to decide which standard you want to use: C99 or Posix.
If you want to use the type ssize_t, then you have to define (activate) that standard in your sources, see man feature_test_macros.
But beware, if you activate any standard, which enables the desired Posix standard, you're not pure C99 anymore.
I get the following error when I run my code:
error: implicit declaration of function ‘mkdtemp’ [-Werror=implicit-function-declaration]
This occurs even after including the correct header files for mkdtemp():
#include <stdlib.h>
Any ideas why this might be occurring?
The <stdlib.h> header is mandated by the C standard. The C standard makes no reference to a mkdtemp() function. If you're using gcc -std=c11 or some similar option, only the definitions provided by the C standard are exposed. If you compile using gcc -std=gnu11, then you'll get an indeterminate set of extension features enabled (and mkdtemp() would be one of them).
Since mkdtemp() is a POSIX function, you can explicitly request it by defining the appropriate enabling macro before including any standard header. A command-line option -D_XOPEN_SOURCE=700 would (probably) do the job, for example; there's also the option of using -D_POSIX_C_SOURCE=200809 but remembering the correct number is harder (it is the date of the POSIX 2008 standard as year and month).
Or you can place the appropriate #define at the top of the file:
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
or:
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
These stanzas allow you to override the POSIX version on the command line. Simply writing the #define without the conditional around it would generate a warning (or error) for a non-benign redefinition of the macro.
There used to be major differences between the POSIX and X/Open functionality — X/Open included some things that POSIX doesn't. This distinction is smaller these days, and generally, you'll not get into trouble using the X/Open macro.
There are other enabling macros for other platforms, but one of these two will enable the declaration of mkdtemp(). On Linux (RHEL 7.x), /usr/include/features.h) documents these enabling macros:
/* These are defined by the user (or the compiler)
to specify the desired environment:
__STRICT_ANSI__ ISO Standard C.
_ISOC99_SOURCE Extensions to ISO C89 from ISO C99.
_ISOC11_SOURCE Extensions to ISO C99 from ISO C11.
_POSIX_SOURCE IEEE Std 1003.1.
_POSIX_C_SOURCE If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
if >=199309L, add IEEE Std 1003.1b-1993;
if >=199506L, add IEEE Std 1003.1c-1995;
if >=200112L, all of IEEE 1003.1-2004
if >=200809L, all of IEEE 1003.1-2008
_XOPEN_SOURCE Includes POSIX and XPG things. Set to 500 if
Single Unix conformance is wanted, to 600 for the
sixth revision, to 700 for the seventh revision.
_XOPEN_SOURCE_EXTENDED XPG things and X/Open Unix extensions.
_LARGEFILE_SOURCE Some more functions for correct standard I/O.
_LARGEFILE64_SOURCE Additional functionality from LFS for large files.
_FILE_OFFSET_BITS=N Select default filesystem interface.
_BSD_SOURCE ISO C, POSIX, and 4.3BSD things.
_SVID_SOURCE ISO C, POSIX, and SVID things.
_ATFILE_SOURCE Additional *at interfaces.
_GNU_SOURCE All of the above, plus GNU extensions.
_REENTRANT Select additionally reentrant object.
_THREAD_SAFE Same as _REENTRANT, often used by other systems.
_FORTIFY_SOURCE If set to numeric value > 0 additional security
measures are defined, according to level.
Note, too, that the manual page for mkdtemp() shows what is needed:
NAME
mkdtemp - create a unique temporary directory
SYNOPSIS
#include <stdlib.h>
char *mkdtemp(char *template);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
mkdtemp():
_BSD_SOURCE
|| /* Since glibc 2.10: */
(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
What I called 'enabling macros' are also known as 'Feature Test' macros.
See also POSIX System Interfaces: General Information: The Compilation Environment.
I know that very few compilers actually support C11 threads (which is sad, but whatever). The C11 standard demands that an implementation that doesn't support threads defines __STDC_NO_THREADS__. Yet this program seems to give an error:
#include <stdio.h>
#ifndef __STDC_NO_THREADS__
#include <threads.h> //error is here
#endif // __STDC_NO_THREADS__
int main(void)
{
#ifdef __STDC_NO_THREADS__
printf("There are no threads");
#else
printf("There are threads");
#endif // __STDC_NO_THREADS__
}
//Error at line 3: fatal error: threads.h: No such file or directory
Compiler version is GCC 9.2.0 (Windows 10 x64), with __STDC_VERSION__ = 201710L (so it is C17). If you cannot tell, the problem is that my compiler doesn't define either __STDC_NO_THREADS__ or <threads.h>, which doesn't conform to C11. What could the problem be?
Pre-C11 compilers and libraries will not define __STDC_NO_THREADS__, nor will post-C11 ones with support for threading. So a correct check needs to look like this:
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#include <threads.h>
Otherwise older versions of the compiler/library will not work. In your case, you seem to be using Mingw under Windows, in which case the non-compliant Microsoft CRT is used (it does not conform to C99 and beyond).
Later versions of gcc using later versions of libc seem to work correctly with the original code.
Please note that gcc is not to be regarded as a conforming implementation unless you compile with -std=c17 -pedantic-errors. I don't think it matters in this specific case.
Is an ANSI C-compliant implementation allowed to include additional types and functions in its standard library, beyond those enumerated by the standard? (An ideal answer would reference the relevant part of the ANSI standard.)
I ask particularly because Mac OS 10.7 declares the getline function in stdio.h, even when compiling with gcc or clang using the -ansi flag. This breaks several older programs that define their own getline function. Is this a fault of Mac OS 10.7? (The man page for getline on Mac OS 10.7 says that getline conforms to the POSIX.1 standard, which came in 2008.)
Edit: To clarify, I find it odd that including stdio.h in an ANSI C89 program on Mac OS 10.7 also pulls in the declaration for the getline function, since getline is not one of the functions enumerated in the K&R (and presumably ANSI) description of stdio.h. In particular, attempting to compile noweb:
gcc -ansi -pedantic -c -o notangle.o notangle.c
In file included from notangle.nw:28:
getline.h:4: error: conflicting types for ‘getline’
/usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here
Is it a bug in Mac OS 10.7 includes the declaration of getline in stdio.h even when compiling for the ANSI C89 standard?
From section 7.1.3 paragraph 2 of n1570 (which is a draft of C1x):
No other identifiers are reserved.
This is the part that means getline shouldn't be defined by the <stdio.h>, since it's not a reserved identifier according to the spec. So if your library defines getline in <stdio.h>, it's not technically compliant with the C standard...
However, you should be able to use the feature test macros to cause getline to be undefined in <stdio.h>.
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
This will give you only the definitions from the older POSIX standards. This won't work on some GNU C++ implementations, which is ExTrEmeLY fruSTRaTiNG for some folks.
The relevant section of the manpage is (taken from a glibc manpage, sorry...)
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getline(), getdelim():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
Before glibc 2.10:
_GNU_SOURCE
This part of the manpage tells you which macros need to be defined to which values in order to get the definition. My bet is that _POSIX_C_SOURCE is already defined by your compiler to 200809L.
The idea of feature test macros is that if you define your macros, like _POSIX_C_SOURCE, _BSD_SOURCE, _XOPEN_SOURCE, etc. to the values you want, you won't need to worry about new library functions clashing with your existing functions. There is also _GNU_SOURCE, which turns everything on if you use glibc, but I suggest giving that macro a wide berth.
Yes, a compliant implementation is allowed to define additional identifiers, including functions, as long as they are one of the reserved identifiers in the standard. For example:
All identifiers that begin with an underscore and either an uppercase letter or another
underscore are always reserved for any use;
All identifiers that begin with an underscore are always reserved for use as identifiers
with file scope in both the ordinary and tag name spaces;
All external names that begin with is, to, str, mem or wcs followed by a lowercase letter;
In addition there are names that are reserved only if you include certain headers; for example, if you include <errno.h> then it can define any macro starting with E followed by a digit or uppercase letter.
However, getline() is not such a reserved name, and a compliant implementation must make it available for the programmer's own use.