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

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

Related

list_head not recognized when compiling linux kernel

I am compiling a modfied version of the kernel that has a C program I made. I'm new to this, and I can't get rid of this error:
error: unknown type name ‘list_head’; did you mean ‘rcu_head'?
Here are the lines leading up to the line with the error:
//directives
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/sched/task.h>
#include <asm-generic/barrier.h>
#include <linux/resource.h>
#define set_task_state(tsk, state_value) \
do { \
(tsk)->task_state_change = _THIS_IP_; \
smp_store_mb((tsk)->state, (state_value)); \
} while (0)
#define PROC_NUM 50
#define MILSEC_GAP 2
*(delta_entry) get_delta_entry(int num, *linked_list);
int get_delta_time(list_head *linked_list, delta_entry *de, int max);
I know list.h includes types.h, which defines list_head. What am I doing wrong?
Having looked at types.h, it seems to me that the problem is this: list_head is the name of a structure tag, not a type. So you need to write
struct list_head ... rather than simply list_head .... In other words, the function prototype should read
int get_delta_time(struct list_head *linked_list, delta_entry *de, int max);
and whenever you want a variable, you need to declare it as
struct list_head head;
head.next = ...
See
http://c-faq.com/struct/typedef.html and
http://c-faq.com/struct/impltypedef.html

Facing error Conflicting data type but cannot find any conflict

I have declared a function in file_utils.h and defined it in file_utils.c At compile time it is gives a conflicting type error.
File_utils.h
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
#define dirent direct
#define NAMLEN(dirent) ((dirent)->d_namlen)
#ifdef HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif
#ifdef HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif
#ifdef HAVE_NDIR_H
#include <ndir.h>
#endif
#endif
bool is_relative_path(struct dirent *ent);
File_utils.c
#include "file_utils.h"
#include <stdbool.h>
#include <dirent.h>
bool is_relative_path(struct dirent *ent){
return (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0);
}
Error:
abhiram#abhiram-Lenovo-G50-70:~/libpostal-master/src$ gcc -DLIBPOSTAL_DATA_DIR='"$//home/abhiram/libpostal-master/data"' -o main main.c libpostal.c file_utils.c json_encode.c string_utils.c -std=c99 -w
file_utils.c:45:6: error: conflicting types for ‘is_relative_path’
bool is_relative_path(struct dirent *ent){
^
In file included from file_utils.c:1:0:
file_utils.h:59:6: note: previous declaration of ‘is_relative_path’ was here
bool is_relative_path(struct dirent *ent);
^
I have included both dirent.h and stdbool.h libraries.
Resolve all compiler warnings.
Compile with all compiler warnings enabled -Wall -Wextra.
Resolve all compiler warnings.
The code you posted has a missing #endif, maybe probably from #ifdef HAVE_DIRENT_H, but I am guessing the last line of the file_utils.h should be a closing #endif.
The warning I get from gcc is this:
warning: ‘struct direct’ declared inside parameter list
warning: its scope is only this definition or declaration, which is probably not what you want
This is the the most important warning.
Structure definition is only valid inside function parameter list. Ex:
void other_f(
struct B a // this will forward declare struct B
// scope of this variable is _only_ inside function parameter list
); // here struct B get's out of scope!
struct B b; // will error, there is no struct B here
// struct B was declared inside function parameter list
// you can't use it anywhere else
The MCVE to your problem would be this example:
void f(struct A);
struct A;
void f(struct A); // error conflicting types for 'f'
The struct A will be declared (I call it "auto-declared") inside the function parameter list void f( <here> ) on the first use. The structure declaration will be visible only inside the parameter list. So it's similar to a pseudocode:
{
struct A; // type only valid inside `{` `}` braces
void f(struct A a); // imagine this symbol is visible outside `{` `}`
}
ie. the struct A is not visible outside the { }.
Then you declare another struct A:
struct A;
void f(struct A a);
But this struct A is different type as the other struct A. As it's different struct A, the function f is different, the compiler issues an error.
No consider your header:
#define dirent direct
...
bool is_relative_path(struct dirent *ent);
I don't know if direct is a typo or not. But you need to forward declare the struct direct, so that the forward declaration of struct direct is visible outside the function parameter list of is_relative_path function.
struct direct;
#define dirent direct
...
// or here:
struct dirent;
bool is_relative_path(struct dirent *ent);
Your problem seems to be file inclusion order if "File_utils.c" and / or a missing definition of HAVE_DIRENT_H.
In the given inclusion order, "file_utils.h" has no idea what a struct dirent is, since (presumably HAVE_DIRENT_H) is defined within <dirent.h>. If this is NOT the case, simply ensure that HAVE_DIRENT_H IS defined before including "file_utils.h"
The net effect as-is in the code is that in "file_utils.h", bool is_relative_path(struct dirent *ent) is actually seen as bool is_relative_path(some_pointer_to_an_unknown_struct_type ent), while the "file_utils.c" sees the function signature as as bool is_relative_path(a_pointer_to_a_struct_type_i_definately_know_about ent).
Thus the two files do NOT agree on the function signature.
Edit
#n.m. is correct in that "file_utils.h" essentially sees a distinct definition of struct dirent, and that one cannot declare a type inside a function parameter list.
TLDR
Edit File_utils.c to define HAVE_DIRENT_H and / or #include <dirent.h> before #include "file_utils.h" so that both "file_utils.h" and "file_utils.c" see a common function signature for bool is_relative_path(struct dirent *ent)
It looks like the declaration of bool is_relative_path(struct dirent *ent); is AFTER the #endif preprocessor declaration (include guards). That mean including this header file in two different files will cause two declarations. Have a look at this: https://en.wikipedia.org/wiki/Include_guard to get some more details
This should fix it:
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
#define dirent direct
#define NAMLEN(dirent) ((dirent)->d_namlen)
#ifdef HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif
#ifdef HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif
#ifdef HAVE_NDIR_H
#include <ndir.h>
#endif
// moved the declaration between the #ifndef #endif block
bool is_relative_path(struct dirent *ent);
#endif
You cannot declare a type inside a function parameter list. This is what happens when in File_utils.h you have a declaration of is_relative_path with no prior declaration of stuct dirent.
Either #include <dirent.h> in File_utils.h (recommended), or add a declaration
struct dirent;
somewhere in it above is_relative_path.

Unable to include timespec in another struct

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;
}

Using typedef struct in multiple files

I have a typedef struct declared in one my headers. Its associated C file can find the typedef, but other headers have trouble reading it.
// In projectiles.h I have
#ifndef PROJECTILES_H_
#define PROJECTILES_H_
struct TheProjectile { };
typedef struct TheProjectile Projectile;
#endif /* PROJECTILES_H_ */
In physics.h I want to use Projectile
#ifndef PHYSICS_H_
#define PHYSICS_H_
#include "projectiles.h"
struct TheProjectile;
void set_Current_Angle(Projectile* PI);
#endif /* PHYSICS_H_ */
However, in Eclipse I keep getting "expecting ) before PI" error. Without typedef it does work fine. What am I doing wrong?

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