链接共享库与其他共享库的C++代码

3
我已经成功创建了我的共享库,libA.so。 其中所有的类都在命名空间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)

然后,我需要创建另一个名为B的库,它使用A库。因此,我链接它(-lA和-L< path_to_A >),并使用_I/编译。所有内部类都有命名空间common::B。
编译并制作libB.so,但是:
1)Eclipse在调用A方法的代码处放置了红色X:
 Multiple markers at this line
- Symbol '<A_method>' could not be resolved
- Function '<A_method>' could not be resolved

2) 库 A 似乎与 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)

我不明白为什么我没有A链接:

libA.so => .....

有什么想法吗?

更新

这是eclipse自动生成的makefile:

-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

我并不是想立刻关闭它,只是想表明它可能是一个重复的问题。如果你认为它没有回答你的问题,请评论一下,我会重新打开它。 - MByD
我认为这不是同一种情况。我正在使用共享库(.so),而不是静态库(.a)。 - Luca Davanzo
它适用于共享库和静态库,请试一试。 - MByD
抱歉,我已经包含了“-L<库文件路径>”.. 我更新了问题! - Luca Davanzo
嗯,我很抱歉。我投票支持重新开放,但请在问题中修复这个问题。 - MByD
显示剩余6条评论
2个回答

1
如果您的libB没有使用libA中的任何内容,则链接器可能会丢弃与libA的链接(这取决于链接器标志--as-needed在您的工具链中是否为默认设置)。
要强制libB链接到libA,请指定-Wl,--no-as-needed标志出现在-lA之前。

-1
如果您正在使用MakeFile,则可以像这样操作:
xxxxxx_la_SOURCES = $(FILES)
xxxxxx_la_CFLAGS = $(CFLAGS)
xxxsxx_la_LDFLAGS = $(location_of_so_file)

我的Makefile是由Eclipse自动生成的。因此,如果需要,我必须找到在项目设置中设置这些值的位置。 - Luca Davanzo
@Velthune:别费心了,这是GNU Automake所做的魔法。我怀疑Eclipse CDT是否使用相同的机制(但我可能错了,因为我两个都不用)。 - DevSolar
@Velthune 看一下这个链接:http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html - Vineet1982

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接