list_head not recognized when compiling linux kernel - c

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

Related

Use typedef from one header in another and vice versa

Suppose I have such a project structure:
main.c
#include "hashtable.h"
#include "list.h"
int main()
{
hash_table ht = calloc(1, sizeof(htable));
cmp_function f;
TLDI list;
return 0;
}
hashtable.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _HASH_TABLE_
#define _HASH_TABLE_
#include "list.h"
typedef int (*hash_function)(void*, int);
typedef struct _hasht_{
int maxElemNumber;
hash_function hf;
TLDI* key_array;
} htable, *hash_table;
void test2(cmp_function cmp);
#endif
list.h
#include "hashtable.h"
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
typedef int (*cmp_function)(void*, void*);
typedef struct _node_ {
void *info;
struct _node_ *pre, *urm;
} TNode, *TLDI;
int test(hash_table ht);
#endif
and another two C files:
hash_func.c
#include "hashtable.h"
void test2(cmp_function cmp)
{
printf("test\n");
}
list_func.c
#include "list.h"
int test(hash_table ht)
{
return 1;
}
I want to use in hashtable.h a typedef from list.h, it's typedef struct...},*TLDI;. In the same way, list.h uses a typedef struct ...},*hash_table; from hashtable.h. Can I do something like this or I'm wrong? Cause I get this error while compiling whole project:
In file included from hashtable.h:7,
from main.c:1:
list.h:14:10: error: unknown type name ‘hash_table’
14 | int test(hash_table ht);
In file included from hashtable.h:7,
from hash_func.c:1:
list.h:14:10: error: unknown type name ‘hash_table’
14 | int test(hash_table ht);
I'm not strong in typedef and headers, but if I would get an answer to this question or at least a source from where I could find out more about them I would be very grateful.
Two headers that rely to each other are not a show stopper if well-formed. What I observe is that your include guards don't enclose the full header but only part of it, this I think is wrong. The right way to use include guards is shown in this
example header some_component.h:
#ifndef SOME_COMPONENT_H
#define SOME_COMPONENT_H
// include whatever you need here (*after* the opening guard):
#include "some_other_component.h"
// start type definitions and declarations *after* includes:
struct some_component_t {
// ...
};
#endif
This way, you headers will work most consistently:
either read completely
or completely ignored
I advise you to avoid placing definitions before includes, as this allows you to modify the content of the included content. What looks like a tempting idea at first, turns into a confusing nightmare in the long run in the vast majority of cases.
Another point is that if the definitions in the two headers really rely on each other, you should rethink your design.
Also, it's not clear why void test2(cmp_function cmp); which relies on cmp_function is declared in hashtable.h and why int test(hash_table ht); which relies on hash_table is declared in list.h; to me this seems like you were mixing up things here. In other words, by switching places of some declarations, you'd get rid of most of the entanglement.
You should also know that typedefs and pointers are allowed on incomplete types, so it's possible to declare a pointer to a structure that is not yet defined. So, for example, the following compiles:
typedef int (*hash_function)(void*,int);
typedef int (*cmp_function)(void*,void*);
typedef struct _hasht_ hasht, *hash_table;
typedef struct _node_ TNode, *TLDI;
struct _node_ {
void *info;
struct _node_ *pre, *urm;
};
struct _hasht_{
int maxElemNumber;
hash_function hf;
TLDI* key_array;
};
... as does this (version without struct typedefs):
struct _node_ {
void *info;
struct _node_ *pre, *urm;
};
typedef int (*hash_function)(void*,int);
struct _hasht_{
int maxElemNumber;
hash_function hf;
struct _node_** key_array;
};
The overall interdependency of the headers is kind of ugly, but the errors can be corrected with some forward declarations:
hashtable.h
#ifndef _HASH_TABLE_
#define _HASH_TABLE_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _hasht_ htable, *hash_table;
typedef int (*hash_function)(void*,int);
#include "list.h"
struct _hasht_ {
int maxElemNumber;
hash_function hf;
TLDI* key_array;
};
void test2(cmp_function cmp);
#endif
list.h
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
typedef struct _node_ TNode, *TLDI;
typedef int (*cmp_function)(void*,void*);
#include "hashtable.h"
struct _node_ {
void *info;
struct _node_ *pre, *urm;
};
int test(hash_table ht);
#endif

"unknown type name" in c struct while ussing gcc

I have a program which is processing list in c, it is working perfectly as long as I have it in one source file, when I try to separate it and compile it got this error “ delete_functions.c:15:13: error: unknown type name ‘nodetype’ ” same error goes for functionality_functions.c and insert_functions.c here is the code
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "delete_functions.h"
#include "insert_functions.h"
#include "functionality_functions.h"
int main(){
//i did not upload all the main function code because it is way to long
}
types.h
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
delete_functions.h
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
functionality_functions.h
void print(nodetype *head);
nodetype *search(nodetype *list,char item[4]);
nodetype *create();
insert_functions.h
void *InsertLast(nodetype *list,char item[4]);
void *InsertAfter(nodetype *list,char item[4],char node[4]);
According to the GCC error message, there error is in the delete_functions.c file.
Presumably, it looks like this at the beginning:
#include "delete_functions.h"
Since delete_functions.h does not itself include types.h, you need to include it first:
#include "types.h"
#include "delete_functions.h"
Alternatively, you can add include guards to your headers so, that they can safely be included multiple times, like this for types.h:
#ifndef TYPES_H
#define TYPES_H
typedef char AirportCode[4];
typedef struct nodetype{
char Airport[4];
struct nodetype *next;
} nodetype;
#endif
And for delete_functions.h:
#ifndef DELETE_FUNCTIONS_H
#define DELETE_FUNCTIONS_H
void Delete(nodetype *list,char node[4]);
void DeleteLast(nodetype *list);
#endif
The *_H include guard macros are necessary because otherwise, main.c would not compile anymore: each type in types.h can only be defined once per translation unit, and without the guards, every *.h would bring in another definition, leading to compiler errors.

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.

C - Unknown type name

I need to build a "social network" for college, but I always get unknown type name 'List' while compiling. I removed a lot of functions from my headers, but I still get the same error and I don't know why.
I've got 3 headers:
My friend's header
#ifndef FRIEND_H
#define FRIEND_H
#include "ListHeadTail.h"
typedef struct Friend{
int id;
struct Friend *nextFriend;
}Friend;
void printFriends(List *l);
void removeFriend(List *l);
void addFriend(List *l);
#endif /* FRIEND_H */
My list header:
#ifndef LISTHEADTAIL_H
#define LISTHEADTAIL_H
#include "Student.h"
typedef struct pStudent{
struct pStudent *ant;
Student *s;
struct pStudent *prox;
}pStudent;
typedef struct list{
pStudent *head;
pStudent *tail;
}List;
void startList(List *l);
void printList(List *l);
void freeList(List *l);
#endif /* LISTHEADTAIL_H */
My student's header
#ifndef STUDENT_H
#define STUDENT_H
#define MAX 51
#include "Friend.h"
#include "ListHeadTail.h"
typedef struct Student{
int id;
char name[MAX];
Friend *friends;
}Student;
Student* readStudent ();
void printStudent(Student* a);
void changeData(List *l);
#endif /* STUDENT_H */
My main:
#include <stdio.h>
#include <stdlib.h>
#include "ListHeadTail.h"
#include "Friend.h"
#include "Student.h"
int main(int argc, char** argv) {
List l;
startList(&l);
freeList(&l);
return (EXIT_SUCCESS);
}
Thanks for reading.
Here's the (first) error I get when I try to compile this set of files:
$ cc main.c
In file included from main.c:4:
In file included from ./ListHeadTail.h:4:
In file included from ./Student.h:6:
./Friend.h:11:19: error: unknown type name 'List'
void printFriends(List *l);
Look at the file names and line numbers. Note that at ListHeadTail.h line 4, you've already defined LISTHEADTAIL_H, but you haven't yet reached the actual declaration of List. You then go into Student.h, and from there into Friend.h. That includes ListHeadTail.h again -- but since LISTHEADTAIL_H is already defined, this include does nothing. So you continue through Friend.h with no declaration of List, and therefore get an error on the declarations that reference it.
As noted by #lurker in their comment, the basic issue here is circular dependency, and a simple fix is forward declaration. In this case, you could simply modify Friend.H, replacing #include "ListHeadTail.h" with typedef struct list List;.
But to me this is a bit hacky. If you shift the order of includes somewhere, the build might break again.
I think the real problem is that the declarations of the functions (printFriends, etc.) don't belong in Friend.h; they belong in ListHeadTail.h. The functions have nothing to do with the Friend type. Sure, they have "Friend" in their names, but the only type referenced in the declarations is List. So they belong in ListHeadTail.h. Same goes for the changeData function in Student.h.
In an object-oriented design (say, in Java), these functions would all probably be methods of the List class, and would be declared in that class's source file.

Declaring the various typedefs in a stack ADT

Here's my problem: I need to implement a FIFO/LIFO list stack as ADT species 1. My program is modular and it have an item.h module:
#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
typedef struct
{
char stringa[20];
int numero;
} Item;
#endif // ITEM_H_INCLUDED
The head.h module:
#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
void QUEUEinit();
int QUEUEempty();
void QUEUEput_top(Item);
void QUEUEput_bottom(Item);
Item QUEUEget_top();
Item QUEUEget_bottom();
#endif // HEAD_H_INCLUDED
The main.c and data.c; what i need is how i declare a QEUEnode struct and where.
Thank you for the help :)
Since none of your QUEUE* functions receive a QUEUEnode *, you can hide it in the head.c file, along with the QUEUEnode root; that they operate on.
If you want to use multiple queues, then it should probably be in the head.h file so they can be created in main.c. For this, you'll also need to modify the functions to accept a queue to operate on.

Resources