I want to check the detail of the function sem_post() in semaphore header file,which is below
extern int sem_post (sem_t *__sem) __THROWNL __nonnull ((1));
And then I find the sem_post.c file in source code of glibc, but in this file, the function's name is __new_sem_post().
I know it's about CRT, but I don't know how. So, there are questions:
- Why there is an "extern" and how it work?
- How does the name change from __new_sem_post() to sem_post()?
externmeans that you are declaring something that is not (necessarily) defined here, but in some other units. Soextern int x;doesn't allocate memory for a variablex. It just declares to the compiler that, somewhere else in this code (may be in another compilation unit, that is another.cfile) that variable will be defined. That is that, somewhere else, there is aint x;declaration.This allows the compiler to know that it should not fail if some code uses
x. Instead of saying "x: symbol undefined", it will know thatxis some global variable of typeint, and compile any code referring toxas such.Then, of course, at some point, it will be necessary to check that, indeed, there is such a variable. But that is done at linking stage. That is, not when you compile, separately, each
.cfile into a.ofile. But when you put all the.ofiles, plus the libraries, together, to create an executable.Understand that, mainly, this is what
.hfile do: they contain, no code (I mean not code that should generate assembly code), but declarations. Declarations whose purpose is to give some contextual information for the compiler when compiling your.cfile.Example
Let's consider an example case, in which you do, in a file
testsin.cThen compile that file with
gcc -c testsin.c.And then link the given
.oand thelibmlibrary together, to create atestsinexecutable, usinggcc -o testsin testsin.o -lmWhen compiling (
gcc -c)testsinthe compiler doesn't have any code about asinfunction, nor does it have any reason to know what you are talking about, and where thissinfunction is. Later, when linking (gcc -o testsin testsin.o -lm) you'll provide the library that contains thesinfunction (libm.so, here passed with-lm). But that is later. Without any DeLorean, it can't guess at compile time (gcc -c) what thissinthing is. So you must tell it "don't worry, don't raise an error, I swear this function exists, with this signature, and I will provide it to you later".This, in C language, is said by the declaration
extern double sin(double);Now, of course, more realistically, what you would do is not that, but
But that is the exact same thing. As its name suggest
#includejust "copy&paste" the content ofmath.hin your code. And math.h just contains some such declaration asextern double sin(double);.implicit
externAnd I kept main information for the end, since I suppose, once all that clarified, that the reason why you are surprised to see this
extern, is that you have already seen some other code with simplydouble sin(double);to do the same declaration.Well, yes,
externis implied for function declarations: if there is no code (no{...}containing actula code), thenexternis implied any way.It is for variables that you must always say it explicitly, because
int x;is the creation of the variable, not a declaration.So, variable creation =
int x;. Function creation =double sin(double x){...}Variable declaration =
extern int x;. Function declaration =extern double sin(double x);ORdouble sin(double);since that is unambiguous in this case.As for your second question, I don't know (and anyway, rule around here is that you are supposed to ask a single question). But it is quite usual to have some "private" implementation of functions, in internal code, for example specific to an architecture, named with some
_, and then, in some other places, a simple alias to makesomefuncpoint to__someprefix_somefunc_somesuffix_or thing like that.