| Author |
Message |
|
|
Posted: May 31, 2012 - 02:50 PM |
|


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.
|
| |
|
|
|
|
|
Posted: May 31, 2012 - 02:51 PM |
|


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. |
_________________
|
| |
|
|
|
|
|
Posted: May 31, 2012 - 02:55 PM |
|


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.
|
| |
|
|
|
|
|
Posted: May 31, 2012 - 03:03 PM |
|


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. |
_________________
|
| |
|
|
|
|
|
Posted: May 31, 2012 - 03:27 PM |
|


Joined: Jan 08, 2005
Posts: 78
|
|
| got it, thanks |
_________________ -----------------------------------------------
goodby blue sky..
Alexi.
|
| |
|
|
|
|
|
Posted: Jun 01, 2012 - 04:22 AM |
|

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. |
|
|
| |
|
|
|
|
|
Posted: Jun 01, 2012 - 09:32 AM |
|


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. |
_________________
|
| |
|
|
|
|
|
Posted: Jun 01, 2012 - 10:28 AM |
|


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. |
|
|
| |
|
|
|
|
|