Unable to include timespec in another struct - c

I've read many posts on this and I can tell you (every post I've read makes this set of assumptions, so lets get it out of way early):
I have included time.h appropriately
I have specified both the POSIX constants and -std=gnu99
Code:
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define uint unsigned int
struct KEYDATA
{
struct timestruct duration;
} ;
// output/display function
int main(void)
{
struct KEYDATA keyData[20];
keyData.duration.tv_nsec = 999;
return 0;
}
At compile time:
pi#raspberrypi:~/src/midi-timing $ gcc tmp.c -O2 -Wall -pedantic -o tmp -std=gnu99 -lrt
tmp.c:19:22: error: field ‘duration’ has incomplete type
struct timestruct duration;
^
tmp.c: In function ‘main’:
tmp.c:27:11: error: request for member ‘duration’ in something not a structure or union
keyData.duration.tv_nsec = 999;
^
tmp.c:25:19: warning: variable ‘keyData’ set but not used [-Wunused-but-set-variable]
struct KEYDATA keyData[20];
^
pi#raspberrypi:~/src/midi-timing $
I'll admit I'm a little rusty on my C programming, but there must be something here I'm not seeing. If you see the error, please let me know. Thanks.

You've identified the type of duration as struct timestruct instead of struct timespec. Simply fix this misspelling and I believe you should be fine.

You need to replace timestruct with timespec
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define uint unsigned int
struct KEYDATA
{
//struct timestruct duration;
struct timespec duration;
} ;
// output/display function
int main(void)
{
struct KEYDATA keyData[20];
//keyData.duration.tv_nsec = 999;
keyData->duration.tv_nsec = 999;
return 0;
}

Related

error: expected declaration specifiers or '…' before string constant

Does anybody know what is wrong with this piece of code? i can't see to find the issue among the comparable questions.
The code is written in C, and i keep getting this error. I do add -D SET_MIN_TEMP=5 -D Set_MAX_TEMP=30 to the gcc compile line to make sure the ifndefs should be false...
#ifndef CONFIG_H
#define CONFIG_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#ifndef RUN_AVG_LENGTH
#define RUN_AVG_LENGTH 5
#endif
#ifndef SET_MIN_TEMP
printf("please set SET_MIN_TEMP \n");
#endif
#ifndef SET_MAX_TEMP
printf("please set SET_MAX_TEMP \n");
#endif
typedef uint16_t sensor_id_t;
typedef uint16_t room_id_t;
typedef double sensor_value_t;
typedef time_t sensor_ts_t; // UTC timestamp as returned by time() - notice that the size of time_t is different on 32/64 bit machine
typedef struct {
sensor_id_t id;
sensor_value_t value;
sensor_ts_t ts;
} sensor_data_t;
typedef struct {
sensor_id_t sensor_id;
room_id_t room_id;
double running_avg[5];
sensor_ts_t timestamp;
} sensor_node_t;
#endif // CONFIG_H
You can not use a function call (printf) outside a function. You should take a look at #error if you want to report errors at compilation...
See here

Why crypt_data isn't known?

regarding to the man page of crypt_r() of freeBSD all I need to include is
#define _GNU_SOURCE
#include <crypt.h>
But it doesn't run. I ofcourse also linked -lcrypt
#define _XOPEN_SOURCE
#include <unistd.h>
#define _GNU_SOURCE
#include <crypt.h>
#include "ownstuff"
int CreateUser (some stuff)
{
//struct crypt_data data; Also didn't work
crypt_data data;
char *res;
data.initialized = 0;
res = crypt_r("password", "$6$QX", &data);
return 0;
}
So what's wrong?
I don't get it.
EDIT:
Also changing the header part to this:
#define _XOPEN_SOURCE
#include <unistd.h>
#include "ownstuff"
Didn't change anything from the error it self.

Cannot access ifreq structure definition, __USE_MISC macro undefined

I am trying to compile the following single C file (called main.c):
#include <stdio.h>
#define __USE_MISC 1
#include <net/if.h>
int main(int argc, char **argv)
{
ifreq id_ifreq;
fprintf(stdout, ">>>>>> OK <<<<<<\n");
}
... using "gcc main.c -o main". I get the following error:
main.c: In function ‘main’:
main.c:9:2: error: unknown type name ‘ifreq’
I know that "ifreq" structure definition lies within a "#ifdef __USE_MISC" macro, however, I cannot activate that block of code.
I developed the following code for checking which MACROS are defined (compiled with "gcc main.c -o main"):
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <net/if.h>
int main(int argc, char **argv)
{
#ifdef __USE_MISC
printf("__USE_MISC defined\n");
#endif
#ifdef _GNU_SOURCE
printf("_GNU_SOURCE defined\n");
#endif
#ifdef _BSD_SOURCE
printf("_BSD_SOURCE defined\n");
#endif
#ifdef _SVID_SOURCE
printf("_SVID_SOURCE defined\n");
#endif
}
The result is that they are all defined but the "_GNU_SOURCE" one. However, I am still not capable of using the definition of the "ifreq" structure included in the "net/if.h" file.
Anybody can help?
You are omitting the struct keyword (in C, a struct definition is not a typedef)
#include <stdio.h>
#include <net/if.h>
int main(int argc, char **argv)
{
struct ifreq id_ifreq;
fprintf(stdout, ">>>>>> OK <<<<<<\n");
return 0;
}

Undefined Reference? But I've already implemented the function

display.h
#ifndef PRO_DISPLAY_H
#define PRO_DISPLAY_H
/** Initializes the display **/
int pro_display_init(void);
#endif /* PRO_DISPLAY_H */
display.c
#include "main.h"
static int height_ = 300;
static int width_ = 300;
static int bpp_ = 16;
static SDL_Surface* screen_ = NULL;
int pro_display_init(void)
{
screen_ = SDL_SetVideoMode(width_, height_, bpp_, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen_)
{
pro_sdl_error("Video initialization failed.");
return 0;
}
return 1;
}
main.h
#ifndef PRO_MAIN_H
#define PRO_MAIN_H
// standard headers
#include <stdlib.h>
#include <stdlib.h>
// conditional headers
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#endif
// our own headers
#include "scripter.h"
#include "ttf_util.h"
#include "events.h"
#include "display.h"
// some macros
#define pro_error(...) fprintf(stderr, __VA_ARGS__)
#define pro_sdl_error(x) fprintf(stderr, "%s. \n=> %s\n", x, SDL_GetError())
#define pro_ttf_error(x) fprintf(stderr, "%s. \n=> %s\n", x, TTF_GetError())
#endif /* PRO_MAIN_H */
** main.c**
#include "main.h"
int main(int argc, char* argv[])
{
pro_display_init();
return 0;
}
The Error:
main.c|5|undefined reference to `pro_display_init()'|
Checked the build process. Made sure I was adding "display.c" to gcc's input files. I'm at my wit's end. Why the error?
display.c and main.c are compiled into their own "translation unit". What happens is that when trying to resolve symbols name (i.e. looking for pro_display_init), the C compiler thinks it's compiling a standalone .c unit. The proper way to go is to compile them separately and then link them, e.g.
gcc -c display.c # creates display.o
gcc main.c display.o # compiles main.o and then link with display.o
Of course, you'll be creating/reusing a Makefile soon that lets you define rules for all this.
I think, #include "main.h" or #include "display.h" (in main.h) "finds" the wrong include file. Check you include_path variable.

include/net/sch_generic.h:199: error: expected specifier-qualifier-list before ‘psched_time_t’

Hi there i am having problem with psched_time_t defined in the struct below it gives the identifier expected error of which I thought that error happens when the corresponding header file is not included and I did include it which is #include and in this file psched_time_t is declared. so what am I doing wrong? please help
#ifndef __NET_SCHED_GENERIC_H
#define __NET_SCHED_GENERIC_H
#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/rcupdate.h>
#include <linux/module.h>
#include <linux/pkt_sched.h>
#include <linux/pkt_cls.h>
#include <net/gen_stats.h>
#include <net/rtnetlink.h>
struct agg_queue {
__be32 dest;
__u32 currSize;
__u32 maxSize;
psched_time_t timestamp; //this is where the error is
struct agg_queue *next;
struct sk_buff_head skb_head;
};
The file below is net/pkt_sched.h which is where psched_time_t is defined:
#ifndef __NET_PKT_SCHED_H
#define __NET_PKT_SCHED_H
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <net/sch_generic.h>
struct qdisc_walker {
int stop;
int skip;
int count;
int (*fn)(struct Qdisc *, unsigned long cl, struct qdisc_walker *);
};
#define QDISC_ALIGNTO 64
#define QDISC_ALIGN(len) (((len) + QDISC_ALIGNTO-1) & ~(QDISC_ALIGNTO-1))
static inline void *qdisc_priv(struct Qdisc *q)
{
return (char *) q + QDISC_ALIGN(sizeof(struct Qdisc));
}
typedef u64 psched_time_t;
typedef long psched_tdiff_t;
/* Avoid doing 64 bit divide */
#define PSCHED_SHIFT 6
#define PSCHED_TICKS2NS(x) ((s64)(x) << PSCHED_SHIFT)
#define PSCHED_NS2TICKS(x) ((x) >> PSCHED_SHIFT)
#define PSCHED_TICKS_PER_SEC PSCHED_NS2TICKS(NSEC_PER_SEC)
#define PSCHED_PASTPERFECT 0
static inline psched_time_t psched_get_time(void)
{
return PSCHED_NS2TICKS(ktime_to_ns(ktime_get()));
}
I see a few #ifdefs/#ifndefs but no #endif anywhere. As you know, each of the former requires one of the latter. Add #endifs where they are needed and you'll get further along.
EDIT: The problem is not psched_time_t but, as the error message says, something before that line. So where is maxSize defined?

Resources