if i hava somewhere in a code:
what does extern do ? I really couldn't find anything relevant. Can somebody please explain me what does this do of redirect me to some usefull link?Code:extern int yyparse();
Thank you!
if i hava somewhere in a code:
what does extern do ? I really couldn't find anything relevant. Can somebody please explain me what does this do of redirect me to some usefull link?Code:extern int yyparse();
Thank you!
In this instance, it does nothing. A function prototype / declaration is assumed extern to begin with.
The extern keyword is most often used when referring to external global data.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Function declarations are extern by default, so the given code is explicitly stating the default: therefore doing nothing.
i don't understand it very well
external in what way ?
does this mean it will get the function content from a file in the header ?
Extern just means the definition - the code which does the real work - is somewhere else (not in this file).
The somewhere else is either
- another source file in your project.
- in an object file in a pre-compiled library.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok... i understand now. and if I use gcc how can I link an object to the program I use?
If it is in another source file this means that the file will be included in the header?
Sorry for the possibly dumb question and thank you for being so patient when answering me!
Since the function is yyparse() I suspect its definition can be found in some file with a name like y.tab.c (or something_else.tab.c). yyparse() is the entry point of an LR(1) parser created by a program called yacc or bison. This program creates .tab.c files from its inputs.
Code:// func.c void func ( void ) { printf("hello from func\n"); }Which we could compile with gcc usingCode:// main.c extern void func ( void ); int main ( ) { func(); return 0; }
gcc main.c func.c
For large projects, we compile each module separately, usually with the help of "make"
gcc -c main.c
gcc -c func.c
gcc main.o func.o
Now if func were part of a library (say libfunc.a), all that would change would be
gcc main.c -lfunc
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.