GCC warning with std=c11 arg - c

Here is a little C source code using pthread_kill() call:
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pthread_t th = NULL;
pthread_kill(th, 0);
return 0;
}
Gcc compilation produces various results depending on -std argument value (see below). I don't understand these different behaviors.
I didn't get interesting informations into man pages except pthread_kill() is POSIX.1-2008 compliant.
Environment: Linux 3.2 64bits. GCC 4.7.2.
With -std=c11
gcc main.c -std=c11 -pthread
I get an implicit declaration:
main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
With -std=c99
gcc main.c -std=c99 -pthread
Same result as -std=c11:
main.c:9:2: warning: implicit declaration of function ‘pthread_kill’ [-Wimplicit-function-declaration]
With -std=c90
gcc main.c -std=c90 -pthread
It simply works without any errors/warnings.
Thank you for your feedbacks.

If you use a Posix feature, you need to define an appropriate feature test macro. See man feature_test_macros or the Posix standard.
If you don't define _POSIX_C_SOURCE to an appropriate value (depending on the minimum Posix level you require), then interfaces from that and subsequent Posix standards will not be defined by standard library headers.
If you need Posix.1-2008, for example, you need to do this:
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
int main(int argc, char *argv[])
{
pthread_t th = NULL;
pthread_kill(th, 0);
return 0;
}

Related

Why is clock_gettime() not working with MinGW, even though I have time.h

The linker keeps telling me that clock_gettime() is undefined.
I have tried using -lrt but then gcc says that it can't find that either.
Am I missing some extra library I needed to download?
// test.c
#include <time.h>
#include <stdio.h>
int main()
{
struct timespec x;
clock_gettime(CLOCK_REALTIME, &x);
printf("%d\n", x.tv_sec);
return 1;
}
When I try to compile this using GCC with MinGW, it puts out this:
gcc test.c
undefined reference to `clock_gettime'
or
gcc test.c -lrt
cannot find -lrt: No such file or directory

Why does GCC claim clock_gettime() implicitly declared, but the preprocessor is perfectly happy with a related macro?

I'm trying to measure the amount of time an operation is taking, as accurately as possible. My research led me to believe that clock_gettime() and friends is what I want.
However, I can't for the life of me get it to work. Consider this seemingly trivial example:
#include <time.h>
#include <unistd.h>
int main(void)
{
struct timespec t;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
return 0;
}
If I run that through the preprocessor, everything looks like it's just fine:
$ cpp time.c | tail -n10
# 1163 "/usr/include/unistd.h" 3 4
# 3 "time.c" 2
int main(void)
{
struct timespec t;
clock_gettime(2, &t);
return 0;
}
However, if I try to compile the preprocessed code, it won't:
$ cpp time.c > time-prep.c
$ cc -o time -Wall -std=c11 -lrt time-prep.c
/tmp/user/1000/cc00SdhB.o: In function `main':
time-prep.c:(.text+0x15): undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
$
If I try to compile the original, it doesn't go any better:
$ cc -o time -Wall -std=c11 -lrt time.c
time.c: In function ‘main’:
time.c:6:18: error: storage size of ‘t’ isn’t known
time.c:7:2: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
time.c:7:16: error: ‘CLOCK_PROCESS_CPUTIME_ID’ undeclared (first use in this function)
time.c:7:16: note: each undeclared identifier is reported only once for each function it appears in
time.c:6:18: warning: unused variable ‘t’ [-Wunused-variable]
$
The man page for clock_gettime says that I need to
Link with -lrt (only for glibc versions before 2.17).
but as you can see, I'm already doing that. Adding or removing -lrt to cc doesn't seem to make any difference whatsoever in my case.
I have looked at /usr/include/time.h but don't see anything obvious that I am missing.
What (presumably trivial) incantation is missing for me to be able to use clock_gettime() in my code?
The Linux documentation for clock_gettime() specifies a feature-test requirement:
_POSIX_C_SOURCE >= 199309L
You could consider fulfilling that directly, perhaps via a #define directive at the very beginning of your code, since the code in fact does depend on it.
If you do not provide such a #define, then gcc may nevertheless do it for you, depending on the options you specify to it. By default, it will do. Likewise with -std=gnu99 or -std=gnu11. But you attempted to compile with -std=c11, which asks for strict(ish) compliance with C11. C11 does not define the needed POSIX feature-test macro.
You can define _GNU_SOURCE above any #include. There is an example:
$cat ./main.c
// Need for clock_gettime()
// Have to be at the begining of the file
#define _GNU_SOURCE
#include <time.h>
#include <unistd.h>
int main(void)
{
...
}

Implicit declaration of timersub() function in Linux - what must I define?

I am trying to compile timersub() function in linux but i always get:
test.c: In function ‘main’:
test.c:27:2: warning: implicit declaration of function ‘timersub’ [-Wimplicit-function-declaration]
timersub(&now, &then, &diff);
^
/tmp/ccLzfLsl.o: In function `main':
test.c:(.text+0x55): undefined reference to `timersub'
collect2: error: ld returned 1 exit status
this is just a simple code of the function with all the library that i use..
#define _XOPEN_SOURCE
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "openflow.h"
#include "cbench.h"
#include "fakeswitch.h"
#include <unistd.h>
int main()
{
struct timeval now, then, diff;
gettimeofday(&then,NULL);
sleep(1);
gettimeofday(&now, NULL);
timersub(&now, &then, &diff);
return 0;
}
i am compiling it with:
gcc --std=c99 -Wall -DTRACE -o test test.c
See the manual. It's not in POSIX but in BSD function. So you need _BSD_SOURCE.
Define it at the top:
#define _BSD_SOURCE
or alternatively compile with:
gcc --std=c99 -Wall -DTRACE -D_BSD_SOURCE -o test test.c
Since Glibc 2.20, the macro _BSD_SOURCE has been deprecated and has been superseded by _DEFAULT_SOURCE. From feature test macros:
_DEFAULT_SOURCE (since glibc 2.19)
This macro can be defined to
ensure that the "default" definitions are provided even when the
defaults would otherwise be disabled, as happens when individual
macros are explicitly defined, or the compiler is invoked in one of
its "standard" modes (e.g., cc -std=c99). Defining _DEFAULT_SOURCE
without defining other individual macros or invoking the compiler in
one of its "stan‐ dard" modes has no effect.
The "default" definitions comprise those required by POSIX.1-2008 and
ISO C99, as well as various definitions originally derived from BSD
and System V. On glibc 2.19 and earlier, these defaults were
approximately equivalent to explicitly defining the following:
cc -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809
But if you are using an older Gblic, you'd still need to use _BSD_SOURCE.
Basically i just deleted #define _XOPEN_SOURCE and #define _POSIX_SOURCE and compiled it only with gcc -Wall -DTRACE and it worked

How to include both for loop initial declarations and gnu extensions?

I have this code:
#include <string.h>
#include <stdio.h>
char numbers[5] = "12345";
int main(){
memrchr(numbers,'2',5);
for(int i=0;i<5;i++){
printf("%d",i);
}
return 0;
}
It uses for loop inital declarations (for(int i) and gnu extensions (memrchr).
I am using the compiler gcc
The problem is that it doesn't seem to let both go through. I can either do
gcc program.c -o program
Which complains about the for loops, or I can do
gcc -std=gnu11 program.c -o program
Which complains about memrchr being undefined. (or rather it complains about the implicit declaration of the function)
How do I do both? Is it possible?
You need to define the _GNU_SOURCE feature-test macro before any library #include. A convenient way to do that is to put it in your command line:
gcc -std=c11 -D_GNU_SOURCE program.c -o program
Or you could put the #define at the very top of every file which needs it. (Putting them in the source file is probably better, but I've gotten into the habit of putting them in my Makefiles. YMMV.)
You can see the needed feature-test macros in the manpage for every C library function. For example, man memrchr includes:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
memrchr(), rawmemchr(): _GNU_SOURCE
As that says, man feature_test_macros will tell you more.

netfilter queue undefined reference to `nfq_open'

I have writting this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
int main(int argc, char **argv)
{
struct nfq_handle *h;
printf("opening library handle\n");
h = nfq_open();
nfq_close(h);
exit(0);
}
and when I try to compile it says that:
/tmp/ccEv9MYS.o: In function `main':
test1.c:(.text+0x1a): undefined reference to `nfq_open'
test1.c:(.text+0x2a): undefined reference to `nfq_close'
collect2: ld returned 1 exit status
I tried checking if the library is found by gcc and it is (when I modifiy the incluse of libnetfilter_queue there is an error), I recompiled the library and made sur that the fonctions I'm calling are in in it.
If you have any clue thanks for helping
Icompile using this:
gcc -o test test1.c
I have also tried:
gcc -o test -lnetfilter_queue test1.c
gcc -o test -L/usr/local/lib test1.c
Well, from the gcc manual page, for the -llibrary linking option
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
That says, the linker works from left to right, so need to put the dependent on left hand side.
You need to change your compilation statement to
gcc -o test test1.c -lnetfilter_queue

Resources