Alaa M. Alaa M. - 2 months ago 7
C Question

unresolvable symbol 'name' referenced in expression

I have a function defined in a

.c
file (say funcs.c):

void *funName()
{
//implementation
}


and compiled to a library (
libname.so
).

And I'm compiling another
.c
file (main.c) which uses this function, and I'm setting symbol names in command line:

gcc -Wl,--just-symbols=symbolsfile.txt main.c -o main -lname


symbolsfile.txt:


FunSym = funName;

Symbol2 = expression2;

...


but I'm getting this linking error:


symbolsfile.txt:1: unresolvable symbol 'funName' referenced in expression

symbolsfile.txt:2: unresolvable symbol 'expression2' referenced in expression

collect2.exe: error: ld returned 1 exit status


I tried adding a function declaration in funcs.c (I don't have a corresponding
.h
file), but it didn't change a thing.

If I change
FunSym = funName
to
FunSym = garbage
, the error changes to "undefined symbol...", so I guess the expression
funName
is found.

Update:

I tried adding
extern void *funName();
at the top of main.c or in a separate header file as suggested in the comments, but it didn't resolve the problem (same error). Is the flag
--just-symbols
needed to be added in compiling
main.c
or in compiling/linking the library (libname.so)?

Answer

Move the flag --just-symbols from compiling main.c to linking the library. So compile the library like this:

gcc -g -Wl,--just-symbols=symbolsfile.txt -shared -o libname.so *.o

and main.c like this:

gcc main.c -o main -lname