Hi
I want to create a library (.lib) for such a function:
#define baud 19200 #define fpclk 12000000 void setbaud (void);
as a header file (set.h) and
void setbaud (void){ unsigned short int a=(fpclk/16/baud); . . . }
as a .c file (set.c).
compiler gives me a .lib file format and I've add it to my UVproject
and every thing is OK but when I try to write a new #define for baud and fpclk, compiler can compile without any error or warning but every thing is fixed same as those definitions which are mentioned in library(here they're always 19200 and 12000000).
if I remove them in library I'll get an error.
how can I set my library to get such information from outside of library. (Rl-ARM is in this way without any kind of problem)
thanks in advance
Time for you to study C.
Time to you to figure out difference between preprocessing, compile time and run time.
How the xx do you think you can build a function and then later change the preprocessor symbols and have the already compiled code to suddenly and magically recompile itself to behave completely differently?
Runtime makes use of parameters or global variables. A #define is not a variable, so it is not processed during runtime but represents input to the compiler during the compilation stage.
You really should get a good book on the C language.
I wrote an example to explain my problem
I'm not perfect but I know how to write C, but I couldn't find a good way to explain my problem
I know, this is not possible to change a #define in another place or having duplicated definition.
I have some definitions (maybe I should say: constants) and I need to change them outside of library to change the behavior of my library. is it possible or not? if yes, how?
if it's not possible to do such a thing except define them outside of lib same as below:
void setbaud (void);
as a header file in library
void setbaud (unsigned long fpclk,unsigned long baud){ unsigned short int a=(unsigned short int)(fpclk/16/baud); . . . }
as a .c file in library
and in my project I do such a thing:
#define baud 19200 #define fpclk 12000000 void setbaud (unsigned long fpclk,unsigned long baud);
as a header file of my main project and
setbaud (fpclk,baud );
as a .c code
but it's not optimized for me because I don't have enough SRAM space(to use them for local variables and stack).
if anybody has a simple question about fundamental matters, it doesn't mean he or she is stupid, his or her question is foolish and silly question.
if you are perfect, instead of quarreling, you can finish the topic with your direct answer
if you wanna say nonsense words, please say nothing.
hi fellow gurus,,,, we have the problem too,,,,,, and prof needs the code tomorrow lecture. when you have the answer post it here to share with all,,,, only answer or good work accepted...... harash
If they change, then they aren't constant - are they?!
Hi all
I was thinking I myself have such a problem.
you said:
these definitions are same as constant that developer can change them before compiling
when I was surfing in keil setup path I found it that rl-arm have such a definitions for its library and in rtx_lib.c implement in this way:
for example in rtx_config.c we have:
#ifndef OS_TASKCNT #define OS_TAKCNT 5 #endif
and in rtx_lib.c we have:
U16 const OS_maxtaskrun=OS_TASKCNT;
here I try to make a library same as it:
in library:
extern int const baud; extern unsigned long const fpclk; void set(void){ unsigned short int a=((fpclk/16)/baud); }
and in main project:
#define baud_rate 19200 #define fpclock 12000000 int const baud=baud_rate; unsigned long const fpclk=fpclock;
as a header file and
int main(void){ set(); }
and it works properly but, I think #define is other think for C development
any idea?
only answer or good work accepted
It seems that quite some people here are developing an acute form of a Saddam Hussain/Muamar Ghadaffi/Osama Bin Laden etc. complex. You know, the rule of thumb is that we don't talk to terrorists...
@harash amakaar,
Ah, "fellow gurus"? You're not even close to that. Right now you're only a slum-bound, cheating scumbag (sorry for that) student/thief/liar. Shame on you!
"these definitions are same as constant that developer can change them before compiling"
So you still haven't grasped what Per pointed out about libraries?
Libraries are compiled code - so you can't change the constants definitions!
I've written a lot of samples here. I want to set for example baudrate as an optional value for user. so I need to change some constants
about rl-arm rtx confige has the same thing about volume of stack per task,num of tasks, and etc
are they fixed in library, and there is no way to change them?
I think the best way is that one which I said, but if anybody knows another way, please mention it.
Then provide a function that the user calls to do that at run time; eg,
void set_baudrate( int baudrate );
This will probably require some other supporting functions (or additional parameters); eg, to tell the library the available clock frequency...
Remember that a project can let a library function make use of a global variable that get its value in the application code.
And library functions can of course take parameters.
It's just that they can't make use of constants that are defined first at the compilation of the application code.
For something to happen at runtime, you need to send the value of the constant to the library function, or you need to store the value of the constant in a variable (potentially read-only) that the library function knows the existence and type of.