git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Rationale behind 'extern' on protypes in .h files

From
Jed Brown <jed@59a2.org>
Date
Dec 23, 2013, 16:59 UTC
Message-ID
<87fvpjqz9u.fsf@jedbrown.org>
In-Reply-To
<CAKTJ_1z-pMePmh4phM2TXSMx0kOjGJ0afQ_JRESggi=k6+y-jA@mail.gmail.com>
Ravi Shekhar Jethani <rsjethani@gmail.com> writes:
Show 7 quoted lines
> To check this I installed the libgit2-dev package which installed:
> /usr/include/git2/*.h , /usr/lib/libgit2.so
> Now, I exported all symbols using:
> $ readelf -s /usr/lib/libgit2.so
> and tried to match these with 'externed' prototypes in the Git source
> directory..no matches.
> I am confused!!!.

libgit2 is an entirely different package from Git. If you look at the libgit2 sources (https://github.com/libgit2/libgit2), look in include/git2/common.h:

/** Declare a public function exported for application use. */
#if __GNUC__ >= 4
# define GIT_EXTERN(type) extern \
                         __attribute__((visibility("default"))) \
                         type
#elif defined(_MSC_VER)
# define GIT_EXTERN(type) __declspec(dllexport) type
#else
# define GIT_EXTERN(type) extern type
#endif

I have always used __attribute__((visibility("default"))), but the gcc man page says

    extern declarations are not affected by -fvisibility, so a lot of
    code can be recompiled with -fvisibility=hidden with no
    modifications.  However, this means that calls to "extern" functions
    with no explicit visibility use the PLT, so it is more effective to
    use "__attribute ((visibility))" and/or "#pragma GCC visibility" to
    tell the compiler which "extern" declarations should be treated as
    hidden.

However, I don't understand what the first statement means (documentation bug?) since -fvisibility=hidden causes functions declared with 'extern' to be hidden.

symbols.c: EXTERN int foo(void); int foo(void) {return 1;}

$ gcc -fvisibility=hidden -DEXTERN=extern -shared -o libsymbols.so symbols.c $ nm -D libsymbols.so | grep foo $

meanwhile,

$ gcc -fvisibility=hidden -DEXTERN='__attribute((visibility("default")))' -shared -o libsymbols.so symbols.c $ nm -D libsymbols.so | grep foo 000000000000055c T foo

Show 5 quoted lines
> Also I checked this:
> $ ldd git
> There is no 'gitish' .so in the output; it seems everything is packed
> inside one executable.
> So your second point 'skipping the PLT...' also doesn't seem to apply here.

My comment applied to shared libraries in general, not specifically to Git (which isn't a shared library).

Previous: Ravi Shekhar Jethani
Message 5 of 5 in “Rationale behind 'extern' on protypes in .h files”
  1. Ravi Shekhar JethaniDec 22, 2013
  2. Stefan BellerDec 22, 2013
  3. Jed BrownDec 22, 2013
  4. Ravi Shekhar JethaniDec 23, 2013
  5. Jed BrownDec 23, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.