[SOLVED] How to declare a typedef in a header file for many other c files to use it?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to declare a typedef in a header file for many other c files to use it?
I'm spiting up this one C file into many, putting all of the functions into a separate c file with its header and creating other c file with there headers and they all are in need to use the data within the typedef within the functions.
1. where would be the best C file to put that typedef in, if that can even be answered.
2. how to define it then make it usable to all other c files?
Everything was going fine until I started moving them ones then trying to compile all of the c files I have already set up.
Code:
serx%slackwhere â¡ LearningVDTinC â¡> gcc -m64 -lX11 `imlib2-config --cflags` `imlib2-config --libs` mhsetrootV2.c vdt.c displayimage.c -o setroots
In file included from mhsetrootV2.c:77:0:
vdt.h:6:33: error: expected ')' before 'int'
int setVdesktopCenter(ImageMode,int,const char,int,int,int,Imlib_Image);
^
mhsetrootV2.c:488:37: error: unknown type name 'ImageMode'
int setOrginalSizeImageCenterScreen(ImageMode mode, const char *arg, int rootW, int rootH,
^
mhsetrootV2.c:558:34: error: unknown type name 'ImageMode'
int setResizedImageCenterScreen (ImageMode mode, const char *arg, int rootW, int rootH,
^
mhsetrootV2.c:627:38: error: unknown type name 'ImageMode'
that is just a few of the errors I am now getting.
You need to name your struct, and can you give some code or come with some example code that would use these definitions? Just giving the errors isn't particularly helpful.
You need to name your struct, and can you give some code or come with some example code that would use these definitions? Just giving the errors isn't particularly helpful.
Thanks -- I think I actually have that figured out now, and I am in the process of fixing all of that passing data redoing function ( prams ) stuff now so everybody can see everybody. When I get all of them kinks worked out, then I'm sure to know for sure.
I put the two into a header, then with that function call
Code:
#typedef struct
int parse_color (char *, Color *, int);
and the other one enum ImageMode fixed itself by adding the include header to all of the other c files.
pound sign? you confused me for a min, that was to just indicate a comment. lo lo lo lo lo
Code:
typedef struct Color {
int r, g, b, a;
} Color, *PColor;
then when I use it in a function
Code:
int getHex (char c)
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return c - '0';
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return c - 'A' + 10;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return c - 'a' + 10;
default:
return 0;
}// end switch
} // end function
int parse_color (char *arg, PColor c, int a)
{
if (arg[0] != '#')
return 1;
if ((strlen (arg) != 7) && (strlen (arg) != 9))
return 1;
c->r = getHex (arg[1]) * 16 + getHex (arg[2]);
c->g = getHex (arg[3]) * 16 + getHex (arg[4]);
c->b = getHex (arg[5]) * 16 + getHex (arg[6]);
c->a = a;
if (strlen (arg) == 9)
c->a = getHex (arg[7]) * 16 + getHex (arg[8]);
return 0;
}
I did not write them two I am just using them in the program -
1. where would be the best C file to put that typedef (i.e. any type definition) in...
2. how to define it then make it usable to all other c files?
The best place depends entirely on how you have organized, or not, your code. Some programmers provide large definition files with everything in one place, while others prefer to provide separate source for every major type, class or function. It is ultimately a matter of personal style.
To make any such definition visible to other code segments at compile-time, it must simply be included before it is referenced.
The posted code and error messages are not directly relevant to the question being asked.
I know that you want to ask complete questions, but please try to limit your code and data examples to well considered, minimal snippets which exactly illustrate a specific point. It takes time and energy for others to read through the extra bits, only to discover that they are not relevant.
Also, when posting command examples please do not include your personalized prompt string in command examples. Your eye recognizes and ignores it, but others must visually parse every single line just to find the beginning of the actual command.
Code:
serx%slackwhere âš¡ LearningVDTinC âš¡> gcc -m64 -lX11 `imlib2-config --cflags` `imlib2-config --libs` mhsetrootV2.c vdt.c displayimage.c -o setroots
In file included from mhsetrootV2.c:77:0:
Please help others more easily understand your questions, and help to keep the quality and accessibility of information here as high as possible. Thanks.
1. where would be the best C file to put that typedef (i.e. any type definition) in...
2. how to define it then make it usable to all other c files?
The best place depends entirely on how you have organized, or not, your code. Some programmers provide large definition files with everything in one place, while others prefer to provide separate source for every major type, class or function. It is ultimately a matter of personal style.
To make any such definition visible to other code segments at compile-time, it must simply be included before it is referenced.
The posted code and error messages are not directly relevant to the question being asked.
I know that you want to ask complete questions, but please try to limit your code and data examples to well considered, minimal snippets which exactly illustrate a specific point. It takes time and energy for others to read through the extra bits, only to discover that they are not relevant.
Also, when posting command examples please do not include your personalized prompt string in command examples. Your eye recognizes and ignores it, but others must visually parse every single line just to find the beginning of the actual command.
Code:
serx%slackwhere âš¡ LearningVDTinC âš¡> gcc -m64 -lX11 `imlib2-config --cflags` `imlib2-config --libs` mhsetrootV2.c vdt.c displayimage.c -o setroots
In file included from mhsetrootV2.c:77:0:
Please help others more easily understand your questions, and help to keep the quality and accessibility of information here as high as possible. Thanks.
errors shown
Code:
mhsetrootV2.c:488:37: error: unknown type name 'ImageMode'
int setOrginalSizeImageCenterScreen(ImageMode mode, const char *arg, int rootW, int rootH,
^
1. where would be the best C file to put that typedef in,
if that can even be answered.
your answer to it.
Code:
The best place depends entirely on how you have organized, or not, your code. Some
programmers provide large definition files with everything in one place, while others
prefer to provide separate source for every major type, class or function. It is
ultimately a matter of personal style.
indicating no it cannot be answered. because it is all up to the programmer.
yes that prompt looked crapy and perhaps was too much to look at.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.