LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2017, 11:01 AM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
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.
 
Old 07-19-2017, 12:10 PM   #2
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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.
 
Old 07-19-2017, 12:18 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Laserbeak View Post
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.

Last edited by BW-userx; 07-19-2017 at 12:20 PM.
 
Old 07-19-2017, 12:32 PM   #4
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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
 
Old 07-19-2017, 01:18 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Ok everything is working again.
 
Old 07-19-2017, 01:40 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Laserbeak View Post
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. 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 -

Last edited by BW-userx; 07-19-2017 at 01:47 PM.
 
Old 07-19-2017, 01:44 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,581
Blog Entries: 11

Rep: Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596Reputation: 3596
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.
 
Old 07-19-2017, 02:23 PM   #8
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by BW-userx View Post
pound sign? you confused me for a min, that was to just indicate a comment. 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
 
Old 07-19-2017, 02:35 PM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by Laserbeak View Post
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.
 
Old 07-19-2017, 02:42 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Original Poster
Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by astrogeek View Post
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.

Last edited by BW-userx; 07-19-2017 at 02:46 PM.
 
Old 07-19-2017, 02:45 PM   #11
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by BW-userx View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] compiling header file and souce files hmtng Linux - Newbie 3 03-22-2012 02:28 AM
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
How do you like to set up your files with a header.h file? RHLinuxGUY Programming 5 03-30-2006 07:38 AM
declare and initialize variables in a header file alaios Programming 3 09-04-2005 04:25 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration