I am trying to run a example on handling signals, and it fails compiling on a unfound identifier.
Here is how the header is loaded :
#define __USE_GNU
#include <ucontext.h>
And the error when compiling (with gcc):
$ gcc -o sa_siginfo sa_siginfo.c
sa_siginfo.c: In function ‘bt_sighandler’:
sa_siginfo.c:25:28: error: ‘REG_RIP’ undeclared (first use in this function)
uc->uc_mcontext.gregs[REG_RIP]);
GCC info:
$ gcc --version
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
/usr/include/ucontext.h does include /usr/include/sys/ucontext.h which has:
#ifdef __x86_64__
[...]
#ifdef __USE_GNU
/* Number of each register in the `gregset_t' array. */
enum
{
[...]
REG_RIP,
(My system is 64 bits)
So I don't understand why it doesn't find it ?
Try compiling your program like this
gcc -D_GNU_SOURCE -o sa_siginfo sa_siginfo.c
That __USE_GNU is defined only if you defined _GNU_SOURCE, and gcc will not define it by default.
Related
I'm using clock_gettime in a program. I've tried including as well as but neither works. I have also added -lrt to my compiler arguments but still I get the same errors.
This is on
CentOS Linux release 7.2.1511 (Core)
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
GNU ld version 2.23.52.0.1-55.el7 20130226
ldd (GNU libc) 2.17
Compiler output:
gcc -o main packet.c connect.c transport.c accept.c main.c close.c util.c receive.c send.c congestion.c -Wall -g -std=c99 -lrt
util.c: In function ‘millis’:
util.c:42:21: error: storage size of ‘t’ isn’t known
struct timespec t;
^
util.c:43:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
util.c:43:19: error: ‘CLOCK_MONOTONIC_RAW’ undeclared (first use in this function)
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
^
Makefile
CFLAGS = -Wall -g -std=c99
LIBS = -lrt
# Should be equivalent to your list of C files, if you don't build selectively
SRC=$(wildcard *.c)
main: $(SRC)
gcc -o $# $^ $(CFLAGS) $(LIBS)
Top of util.h
#ifndef UTILS_438_H_
#define UTILS_438_H_
#include "const.h"
#include "data.h"
#include "transport.h"
#include <time.h>
Top of util.c
#include "util.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
Please let me know if I can supply more information to help answer this
Before including the header(<time.h>), do
#define _POSIX_C_SOURCE 199309L
http://man7.org/linux/man-pages/man2/clock_gettime.2.html
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
clock_getres(), clock_gettime(), clock_settime():
_POSIX_C_SOURCE >= 199309L
http://man7.org/linux/man-pages/man7/feature_test_macros.7.html
_POSIX_C_SOURCE
Defining this macro causes header files to expose definitions as
follows:
· The value 1 exposes definitions conforming to POSIX.1-1990 and
ISO C (1990).
· The value 2 or greater additionally exposes definitions for
POSIX.2-1992.
· The value 199309L or greater additionally exposes definitions
for POSIX.1b (real-time extensions).
· The value 199506L or greater additionally exposes definitions
for POSIX.1c (threads).
· (Since glibc 2.3.3) The value 200112L or greater additionally
exposes definitions corresponding to the POSIX.1-2001 base
specification (excluding the XSI extension). This value also
causes C95 (since glibc 2.12) and C99 (since glibc 2.10)
features to be exposed (in other words, the equivalent of
defining _ISOC99_SOURCE).
· (Since glibc 2.10) The value 200809L or greater additionally
exposes definitions corresponding to the POSIX.1-2008 base
specification (excluding the XSI extension).
Simply replace -std=c99 with -std=gnu99.
Thisway you do not have to add _POSIX_C_SOURCE
if you can use the following linker and Compiler flags
CFLAGS = -g -Wall -std=c99 -D_POSIX_SOURCE -D_GNU_SOURCE
LDFLAGS = -lpthread -lrt
you can get the build done in the Linux systems. -lrt linker and -D_POSIX_SOURCE are the important ones
I compile this code main.c in CentOS7 with gcc:
#include <pthread.h>
void* mystart(void* arg)
{
pthread_yield();
return(0);
}
int main(void)
{
pthread_t pid;
pthread_create(&pid, 0, mystart, 0);
return(0);
}
1st compile: gcc -Wall -g main.c -pthread -o a.out
It's all OK.
2nd compile: gcc -Wall -g main.c -lpthread -o a.out
Gives
warning: implicit declaration of function 'pthread_yield' [-Wimplicit-function-declaration]
Can the 2nd a.out still run correctly ?
How to fix the warning without -pthread? Is sched_yield another way to yield a pthread ?
pthread_yield() is a non-standard function which is typically enabled by defining
#define _GNU_SOURCE
While you should use -pthread for compiling, I would expect you to get the same warning with both compilations (unless -pthread defines _GNU_SOURCE which may be the case).
The correct way to fix is to not use the non-standard function pthread_yield() and use the POSIX function sched_yield() instead by including #include <sched.h>.
You should use -pthread for compile and link. It not only links the library, it also sets preprocessor defines and sometimes selects a different runtime library (on Windows for example).
I'm working through some compile errors when building OpenSSL with no-asm -ansi. I'm catching the error:
$ ./config no-asm -ansi
...
$ make
...
gcc -DDSO_DLFCN -DHAVE_DLFCN_H -DOPENSSL_THREADS -DOPENSSL_NO_STATIC_ENGINE -DOPENSSL_PIC
-DOPENSSLDIR="\"/usr/local/ssl\"" -DENGINESDIR="\"/usr/local/lib/engines\"" -Wall -O3
-pthread -m64 -DL_ENDIAN -ansi -fPIC -Iinclude -I. -Icrypto/include -MMD -MF
crypto/bio/bss_dgram.d.tmp -MT crypto/bio/bss_dgram.o -c -o crypto/bio/bss_dgram.o
crypto/bio/bss_dgram.c
In file included from /usr/include/netdb.h:27:0,
from ./e_os.h:443,
from crypto/bio/bio_lcl.h:2,
from crypto/bio/bss_dgram.c:62:
crypto/bio/bss_dgram.c: In function ‘dgram_get_mtu_overhead’:
crypto/bio/bss_dgram.c:433:20: error: ‘const struct in6_addr’ has no member named ‘s6_addr32’
&& IN6_IS_ADDR_V4MAPPED(&tmp_addr))
^
I found the struct in /usr/include/linux/in6.h:
#if __UAPI_DEF_IN6_ADDR
struct in6_addr {
union {
__u8 u6_addr8[16];
#if __UAPI_DEF_IN6_ADDR_ALT
__be16 u6_addr16[8];
__be32 u6_addr32[4];
#endif
} in6_u;
#define s6_addr in6_u.u6_addr8
#if __UAPI_DEF_IN6_ADDR_ALT
#define s6_addr16 in6_u.u6_addr16
#define s6_addr32 in6_u.u6_addr32
#endif
};
#endif /* __UAPI_DEF_IN6_ADDR */
I don't recall needing to define __UAPI_DEF_IN6_ADDR_ALT in the past. Searching for it reveals the following from the kernel's libc-compat.h, line 95 or so (if I am parsing it correctly):
#define __UAPI_DEF_IN6_ADDR 1
/* We unconditionally define the in6_addr macros and glibc must coordinate. */
#define __UAPI_DEF_IN6_ADDR_ALT 1
#define __UAPI_DEF_SOCKADDR_IN6 1
#define __UAPI_DEF_IPV6_MREQ 1
#define __UAPI_DEF_IPPROTO_V6 1
#define __UAPI_DEF_IPV6_OPTIONS 1
#define __UAPI_DEF_IN6_PKTINFO 1
#define __UAPI_DEF_IP6_MTUINFO 1
How should I proceed to get the alternate symbols defined?
The system is Ubuntu 14.04 (x86_64):
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.4 LTS
Release: 14.04
Codename: trusty
GCC is:
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This turned out to be interesting... The short of it is I needed to add both -ansi and -D_DEFAULT_SOURCE=1. However, -D_DEFAULT_SOURCE=1 kind of undoes what -ansi is trying to accomplish so it needs to be contained.
First, /usr/include/linux/in6.h was the wrong header. I found it through a grep for struct in6_addr, and not tracing includes like needed to be done.
Next... -D_DEFAULT_SOURCE=1 indirectly came from /usr/include/netinet/in.h:
#ifndef __USE_KERNEL_IPV6_DEFS
/* IPv6 address */
struct in6_addr
{
union
{
uint8_t __u6_addr8[16];
#ifdef __USE_MISC
uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
#endif
} __in6_u;
#define s6_addr __in6_u.__u6_addr8
#ifdef __USE_MISC
# define s6_addr16 __in6_u.__u6_addr16
# define s6_addr32 __in6_u.__u6_addr32
#endif
};
#endif /* !__USE_KERNEL_IPV6_DEFS */
Manually enabling __USE_MISC did not work. Tracing things for __USE_MISC in /usr/include/features.h:
/*
...
__USE_MISC Define things from 4.3BSD or System V Unix.
...
*/
#undef __USE_MISC
...
#if defined _DEFAULT_SOURCE
# define __USE_MISC 1
#endif
And finally, from a comment in /usr/include/features.h:
_DEFAULT_SOURCE The default set of features (taking precedence over __STRICT_ANSI__).
Though _DEFAULT_SOURCE=1 diverges from -ansi, the impact can be limited to the one source file that's affected by IN6_IS_ADDR_V4MAPPED. That is, in for the C source file crypto/bio/bss_dgram.c:
/* OpenSSL copyright ... */
#ifndef _DEFAULT_SOURCE
# define _DEFAULT_SOURCE 1
#endif
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
...
Fiddling with __USE_KERNEL_IPV6_DEFS made things even worse. I believe it enabled /usr/include/linux/in6.h, which had similar (but completely different) member names than <netinet/in.h>.
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
I have defined the following in a c file:
#ifdef __clang__
// clang definition here
#elif defined _GNULINUX
#define _ALIGNED_FREE(pMempointer) _free((pMempointer))
#elif defined _MSC_VER
// VS definition here
#else
// default definition
#endif
void * pMemory = 0L;
if(pMemory) {
_ALIGNED_FREE(pMemory);
}
gcc flags are:
-Wall -Wno-long-long -fexpensive-optimizations -fomit-frame-pointer -funroll-loops -pipe -fexceptions -Wpointer-arith -Wcast-align -Wsign-compare -pedantic -mmmx -msse
After compiling (in a Ubuntu 14.04 machine) with gcc version 4.8.2 successfully the program exits at runtime with undefined symbol: _free
Obviously the symbol _free is unknown, but why does gcc not complain about this? I would expect it to throw an error at linking time like the VS linker does.
free does not have a leading underscore in Linux nor by the C Standard.