Problems with ld, libc, and "struct stat"
David Demelier
markand at malikania.fr
Wed Oct 16 07:18:55 UTC 2019
Le 15/10/2019 à 20:44, Jan Behrens a écrit :
> Hello,
>
> I stumbled across a weird problem related stat() that (according to my
> research) seems to be related to an update of the "struct stat"
> C-structure in recent Kernel versions.
>
> Consider the following two files.
>
> testlib.c:
> #include <sys/stat.h>
> #include <stdio.h>
> void testfunc() {
> struct stat sb;
> stat("testlib.c", &sb);
> printf("Size of testlib.c is %i bytes.\n", (int)sb.st_size);
> }
Please test the result of stat otherwise sb is left untouched (so all
member undefined).
> testprog.c:
> extern void testfunc(void);
> int main(int argc, char **argv) {
> testfunc();
> return 0;
> }
>
> Now I run:
>
> % cc -Wall -c -fPIC -o testlib.o testlib.c
> % cc -Wall -o testprog testlib.o testprog.c
> % ./testprog
> Size of testlib.c is 168 bytes.
>
> But when I make a shared library like this, I get a different result:
>
> % ld -shared -o testlib.so testlib.o
Hmm, we usually never call the linker itself when creating shared libraries.
Try instead: cc -shared -o testlib.so testlib.o
HTH
--
David
More information about the freebsd-questions
mailing list