Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
alexi
PostPosted: May 31, 2012 - 02:50 PM
Wannabe


Joined: Jan 08, 2005
Posts: 78


hi, i have a function:
Code:

long test(){
 return(0x04030201)
}

this function is work good at the main program, but when transport in other header file, it's return only "0x04030000" can anybody help?


regards

_________________
-----------------------------------------------
goodby blue sky..
Alexi.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 31, 2012 - 02:51 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England

If the function is not "static inline" what would you be doing putting it in a header file anyway?

Suggest you read Managing Large Projects to understand how to split code across files in a linking compiler.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
alexi
PostPosted: May 31, 2012 - 02:55 PM
Wannabe


Joined: Jan 08, 2005
Posts: 78


clawson wrote:
If the function is not "static inline" what would you be doing putting it in a header file anyway?


would u plz explain it more, how must i do?

thanks

_________________
-----------------------------------------------
goodby blue sky..
Alexi.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 31, 2012 - 03:03 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England

Read the tutorial I gave a link to. but basically if one .c file wants to call a function or use a function in another you just do:
Code:
//file1.c
int shared_var;
long test(void) {
 return(0x04030201)
}
Code:
//file2.c
extern int shared_var;
long test(void);

void some_func(void) {
  shared_var = 12345;
  PORTB = test() & 0xFF;
}

The first creates a variable and a function that can be used in the other. In file2.c there are DECLARATIONS of both the variable and the function so the compiler knows their type and interface. Thos two lines could equally go into a .h file that was shared between the two. A declaration does not create any storage but merely prepares the compiler to know what it's using in the "other file". A variable declaration will have the word "extern" on the front while a function declaration shows the interface to the function but ends with a semi-colon rather than actually providing the function code (between { and }). If you like you can even put the word extern on the front too just to make it clear that it's a declaration and not a definition.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
alexi
PostPosted: May 31, 2012 - 03:27 PM
Wannabe


Joined: Jan 08, 2005
Posts: 78


got it, thanks

_________________
-----------------------------------------------
goodby blue sky..
Alexi.
 
 View user's profile Send private message  
Reply with quote Back to top
stevech
PostPosted: Jun 01, 2012 - 04:22 AM
Raving lunatic


Joined: Dec 18, 2001
Posts: 4713


clawson wrote:
If the function is not "static inline" what would you be doing putting it in a header file anyway?
Would this apply to a header for a C++ class?

I've not learned why some people use .h and some use .hpp for C++ header files.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 01, 2012 - 09:32 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62299
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

Would this apply to a header for a C++ class?

No, throw the rule book out of the window when talking about C++, you often define small public functions (things like accessors) in the class definition in the .h file.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 01, 2012 - 10:28 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18561
Location: Lund, Sweden

Putting the member function definition within the class declaration implies inlining.

Everything you wanted to know about C++ inlining here: http://www.parashift.com/c++-faq-lite/i ... tions.html

In fact, most everything you'd want to know about C++ here: http://www.parashift.com/c++-faq-lite/index.html

Quote:
I've not learned why some people use .h and some use .hpp for C++ header files.

Arbitrary, and different customs/conventions. That's all. They are both "header file" suffixes. Some argue that .hpp helps distinguish a header file with C++ stuff from a .h header file with C stuff.

YMMV.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits