简体   繁体   中英

Linux C program using shared libs written in c/c++

I am doing a project, the main program write in C, it's on a embedded Linux system. the hardware supplied by another company, there I got their libs(static libs both in c and c++ language).for later porting to other devices, I made new libs(shared libs) to link with the applications which in c language,that is:

 their libs(static libs,c/c++) --> my libs(shared libs,c) --> my applications(c).

All the c static libs works good for me, when doing with c++ libs, my libs compiled good, but 2 errors come out when linking to my applications:

libplate.so: undefined reference to operator delete(void*)
libplate.so: undefined reference to vtable for __cxxabiv1::__class_type_info

here are the makefile parts for compile this shared lib.

CFLAGS = -Wall -mlittle-endian -I$(INC_PATH)/plate
CPPFLAGS = -Wall -mlittle-endian -I$(PLAT_PATH)/include/sfc -I$(INC_PATH)/plate
OBJ = $(patsubst %.c, %.o, $(SRC)) 
OBJ += $(patsubst %.cpp,%.o,$(SOURCPP))

$(D_LIB):$(OBJ)

$(CC) -o $(D_LIB)  -lm -lpthread -lc -shared -fPCI $(OBJ) $(LIB)
$(STRIP) -s $(D_LIB)

%.o : %.c 
$(CC) $(CFLAGS) -c $<

#CC use gcc

%.o :%.cpp
    $(CXX) $(CPPFLAGS)  -c $< 

#CXX use g++

I have added options for CPPFLAGS like (-fno-rtti -fno-exceptions), they don't work

For application, since all libs compiled in c language, I made my makefile belown:

CFLAGS = -Wall -I$(INC_PATH)/logic ->I$(INC_PATH)/plate

$(BIN):$(OBJ)

$(CC) -o $(BIN) $(OBJ) -Wl,-rpath,$(LD_RUN_PATH) $(LIBS)

%.o : %.c 

$(CC) $(CFLAGS) -c $<

I have tried add some other system libs(-ldl -lc) it doesn't works.

PS: the c++ static lib works just fine when I link this lib direct to c programs.

You need to compile your shared library with g++. Also, you made a mistake with -fPCI. Should be -fPIC. Either that, or for some strange reason you retyped your Makefile here instead of using cut and paste.

So, the line should look like:

$(D_LIB):$(OBJ)
    $(CXX) -o $(D_LIB) -lm -lpthread -shared -fPIC $(OBJ) $(LIB)

Compiling with g++ links the support libraries that C++ needs as well as making sure to include important information such as type information classes and exceptions.

If their C++ library that you are linking to uses RTTI or exceptions, you will need to link those in. Giving the flags -fno-rtti or -fno-exceptions during the shared object link will only break the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM