Moving to another Linux build system, getting an error: undefined symbol: stat

This may be a problem for the build system I am porting to, but I will include the differences in the two systems and how I came across the problem.

My old build system is a SLES 10 machine. Gcc / cpp / g ++ version is 4.1.0

My new system is on SLES 11 SP4, and the gcc / cpp / g ++ version is 4.3.4.

I am creating a shared library; creation and coordination of work in the new system. However, under load time in the new system, I get the following:

error ./mysharedlib.so: undefined symbol: stat

Since the stat () function is included in /usr/include/sys/stat.h, I looked at glibc on both systems. Old:

# rpm -q -f /usr/include/sys/stat.h
glibc-devel-2.4-31.2

and new:

# rpm -q -f /usr/include/sys/stat.h
glibc-devel-2.11.3-17.95.2

I also looked at objdump output related to stat () on the old system:

# objdump -T mysharedlib.so | grep stat
0000000000000000      D  *UND*  0000000000000000              __xstat

# objdump -x mysharedlib.so | grep stat
00000000000e3f8a l     F .text  0000000000000024              stat
0000000000000000         *UND*  0000000000000000              __xstat

And the new system:

# objdump -T mysharedlib.so | grep stat
0000000000000000      D  *UND*  0000000000000000              stat
0000000000000000      D  *UND*  0000000000000000              lstat
# objdump -x mysharedlib.so | grep stat
0000000000000000         *UND*  0000000000000000              stat
0000000000000000         *UND*  0000000000000000              lstat

, stat() .text . Stat - undefined mysharedlib .

feature_test_macros , , .h stat.h makefile, _XOPEN_SOURCE:

cc -D_XOPEN_SOURCE=500  

.

"-lc" ld- libc. , , , stat() ( ), .

StackOverflow:

-O-gcc "stat" ?

, -O make g++ , stat(). , , . , , ; , . ? , ?

+4
1

, , , ld. UNIX ld . (g++ ) .

:

// t.c
#include <sys/stat.h>

void fn(const char *p)
{
  struct stat st;
  stat(p, &st);
}

gcc -fPIC -c t.c
ld -shared -o t.so t.o
nm t.so | grep stat
             U stat       ## problem: this library is not linked correctly

:

gcc -shared -o t.so t.o
nm t.so | grep stat

0000000000000700 t stat
0000000000000700 t __stat
                 U __xstat@@GLIBC_2.2.5

, stat, :

gcc  -shared -o t.so t.o -Wl,-y,stat
t.o: reference to stat
/usr/lib/x86_64-linux-gnu/libc_nonshared.a(stat.oS): definition of stat

, U stat :

gcc -E t.c | grep -A2 ' stat '

extern int stat (const char *__restrict __file,
  struct stat *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2)));

gcc -E t.c -O | grep -A2 ' stat '

__attribute__ ((__nothrow__ , __leaf__)) stat (const char *__path, struct stat *__statbuf)
{
   return __xstat (1, __path, __statbuf);

: .

+6

Source: https://habr.com/ru/post/1678291/


All Articles