LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to declare a typedef in a header file for many other c files to use it? (https://www.linuxquestions.org/questions/programming-9/how-to-declare-a-typedef-in-a-header-file-for-many-other-c-files-to-use-it-4175610187/)

BW-userx 07-19-2017 11:01 AM

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?

I actually have two to deal with.
Code:



typedef enum
{
        // orginal size image modes center screen
        Fill, FullScreen, Center, Tile, FlipImgD, FlipImgH, FlipImgV,
        // tile image within image orginal size image
        TileV, TileH, TileHV,

        //resized image modes center screen,
        DImg, DFlipImgH, DFlipImgV, DFlipImgD,
        // resized image tiled
        DTile, DTileH, DTileV, DTileHV,
        //Virtual Desktop support
        SetVDT

} ImageMode;
 


typedef struct
{
        int r, g, b, a;
}        Color, *PColor;

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.

Laserbeak 07-19-2017 12:10 PM

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.

BW-userx 07-19-2017 12:18 PM

Quote:

Originally Posted by Laserbeak (Post 5737190)
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.

Laserbeak 07-19-2017 12:32 PM

I don't know why you're using a pound sign, it's something like this:


Code:


#include <stdio.h>

typedef struct myStruct {
    int a;
    int b;
    char *c;
    float d;
} myStruct;

int main (int argc, char *argv[]) {

  struct myStruct theStruct;
  theStruct.a = 1;
  theStruct.b = 2;
  theStruct.c = "Hello, World!\n";
  theStruct.d  = 3.14159;

  printf ("%d %d %s %f\n", theStruct.a, theStruct.b, theStruct.c, theStruct.d);

 return 0;

}

output:

Code:

1 2 Hello, World!
 3.141590


BW-userx 07-19-2017 01:18 PM

Ok everything is working again.

BW-userx 07-19-2017 01:40 PM

Quote:

Originally Posted by Laserbeak (Post 5737201)
I don't know why you're using a pound sign, it's something like this:


Code:


#include <stdio.h>

typedef struct myStruct {
    int a;
    int b;
    char *c;
    float d;
} myStruct;

int main (int argc, char *argv[]) {

  struct myStruct theStruct;
  theStruct.a = 1;
  theStruct.b = 2;
  theStruct.c = "Hello, World!\n";
  theStruct.d  = 3.14159;

  printf ("%d %d %s %f\n", theStruct.a, theStruct.b, theStruct.c, theStruct.d);

 return 0;

}

output:

Code:

1 2 Hello, World!
 3.141590


pound sign? you confused me for a min, that was to just indicate a comment. :rolleyes: 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 -

astrogeek 07-19-2017 01:44 PM

Your question reduces to this...

Quote:

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.

Laserbeak 07-19-2017 02:23 PM

Quote:

Originally Posted by BW-userx (Post 5737245)
pound sign? you confused me for a min, that was to just indicate a comment. :rolleyes: lo lo lo lo lo

Since when is a pound (#) sign used to indicate a comment in C? It should be /* my comment */ or // my comment

BW-userx 07-19-2017 02:35 PM

Quote:

Originally Posted by Laserbeak (Post 5737273)
Since when is a pound (#) sign used to indicate a comment in C? It should be /* my comment */ or // my comment

in LQ it is used in code blocks force of habit.

BW-userx 07-19-2017 02:42 PM

Quote:

Originally Posted by astrogeek (Post 5737250)
Your question reduces to this...
Code:

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,
                                    ^

typedef
Code:

typedef enum
{
        // orginal size image modes center screen
        Fill, FullScreen, Center, Tile, FlipImgD, FlipImgH, FlipImgV,
        // tile image within image orginal size image
        TileV, TileH, TileHV,

        //resized image modes center screen,
        DImg, DFlipImgH, DFlipImgV, DFlipImgD,
        // resized image tiled
        DTile, DTileH, DTileV, DTileHV,
        //Virtual Desktop support
        SetVDT

} ImageMode;

Indicating it was not working the way I had it.

first question:
Code:

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.

Laserbeak 07-19-2017 02:45 PM

Quote:

Originally Posted by BW-userx (Post 5737281)
in LQ it is used in code blocks force of habit.

Well it has a definitive meaning in C that is quite different, it indicates a compiler/preprocessor directive. Like:

#include

#define

#ifdef

#ifndef

etc...

So it's not very wise to use it for a totally different meaning mixed in with C code.


All times are GMT -5. The time now is 12:10 PM.