Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Handle undefined symbols in shared object

I've struggled with dlopen and undefined symbols.

What I have and tried so far:

libraryA.a with function functionA()

libraryA.so with function functionA()

libraryB.so which is using functionA()

an executable which loads libraryB.so with dlopen.

What I got is:

undefined symbol: functionA

How I build:

libraryA:

gcc -std=gnu11 -O2 -g -Wall -Wstack-usage=2000 -Werror
-fdiagnostics-color -fPIC -pipe -fsigned-char -fno-asynchronous-unwind-tables      
-fno-stack-protector -I../include/ libraryA.c -shared -o libraryA.so

libraryB:

gcc -std=gnu11 -O2 -g -Wall -Wstack-usage=2000 -Werror -fdiagnostics-color 
-fPIC -pipe -fsigned-char -fno-asynchronous-unwind-tables 
-fno-stack-protector -I../include/ libraryB.c -shared -o libraryB.so

Build executable:

-std=gnu99 -pipe -fno-strict-aliasing -fsigned-char -fno-asynchronous-unwind-tables
-fno-stack-protector -Wall -Wextra -Wundef -Wno-sign-compare -Wno-unused-parameter
-Wno-packed-bitfield-compat -Wno-misleading-indentation -fomit-frame-pointer
-maccumulate-outgoing-args -fPIC -m64 -g3 -gdwarf-2 -fno-common -Wstrict-prototypes
-Wimplicit -Wno-pointer-sign -c executable.c -o executable libraryA.a

Open libraryB in executable like this here:

void *handle = dlopen("libraryB.so", RTLD_LAZY);

if (handle == NULL) {
    fprintf( stderr, "Load error (%s)!\n", dlerror());
    return NULL;
}

How can I fix this behavior? I also tried the flag -rdynamic without success. I also tried different dlopen flags but also without success.

Can anyone please help me?

like image 831
Elec Avatar asked Feb 02 '26 18:02

Elec


1 Answers

You need to link libraryB.so with libraryA.so since libraryB.so uses a symbol from libraryA.so.

Just add libraryA.so to libraryB.so's link command.

Depending on where the libraries are installed you may also need to use the -rpath flag.

Also, since libraryB.so is dependent on libraryA.so, your Makefile rules should explicitly specify this dependency.

like image 91
Sam Varshavchik Avatar answered Feb 04 '26 08:02

Sam Varshavchik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!