Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link shared library with each other shared libraries c++

I've successfully create my shared library, libA.so.
All classes inside, have namspace common::A

ldd libA.so
linux-vdso.so.1 =>  (0x00007fffd632d000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f6497d19000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f6497b03000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6497743000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f6497447000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6498243000)

Then, I need to create another library, B, that uses A. So I link it (-lA and -L< path_to_A >) and also compile with _I/. All classes inside, have namspace common::B

Compile and make libB.so, but:

1) Elipse put red X in code where I call A methods:

 Multiple markers at this line
- Symbol '<A_method>' could not be resolved
- Function '<A_method>' could not be resolved

2) Library A seems not linked to B:

ldd libB.so
linux-vdso.so.1 =>  (0x00007fffbcbfe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f695ca59000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f695c842000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f695c483000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f695bf7e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f695d1d9000)

I can't understand why I don't have a link to A:

libA.so => .....

Any ideas?

update

This is makefile (autogenerated by eclipse):

-include ../makefile.init

RM := rm -rf

# All of the sources participating in the build are defined here
-include sources.mk
-include src/network/mqtt/subdir.mk
-include src/data/subdir.mk
-include subdir.mk
-include objects.mk

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif

-include ../makefile.defs

# Add inputs and outputs from these tool invocations to the build variables 

# All Target
all: libB.so

# Tool invocations
libB.so: $(OBJS) $(USER_OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: GCC C++ Linker'
    g++ -L/path_where_is_A_so/ -shared -o "libB.so" $(OBJS) $(USER_OBJS) $(LIBS)
    @echo 'Finished building target: $@'
    @echo ' '

# Other Targets
clean:
    -$(RM) $(LIBRARIES)$(CC_DEPS)$(C++_DEPS)$(C_UPPER_DEPS)$(CXX_DEPS)$(OBJS)$(CPP_DEPS)$(C_DEPS) libB.so
    -@echo ' '

.PHONY: all clean dependents
.SECONDARY:

-include ../makefile.targets
like image 913
Luca Davanzo Avatar asked Apr 26 '26 08:04

Luca Davanzo


1 Answers

If your libB doesn't use anything in libA, the linker might discard linking to libA (depending on whether the linker flag --as-needed is the default in your toolchain.)

To force libB to link to libA, specify the flag -Wl,--no-as-needed to appear before -lA.

like image 149
nos Avatar answered Apr 27 '26 22:04

nos



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!