วันพฤหัสบดีที่ 20 มีนาคม พ.ศ. 2551

็How to create linux shared library (.so)

Hi guys... this is my first topic.
I would like to talk about the way to create shared library on Linux.

Last week I was assigned to create dll version on Linux . That is shared library.
Don't talk anymore ... let see the way :) .

This work will build new library that use a OCCI library internal.

1.Declare nessecary macro.
CC = g++

OBJ = Th_lexer_lib2_linux.o Worddict.o
OCCI_SHARELIBS = /home/nat/oracle/product/10.2.0/db_1/lib/
OCCI_HEADER = /home/nat/oracle/product/10.2.0/db_1/rdbms/public/

all:Th_lexer_lib2_linux.so
@echo "Build TestBox_linux module complete!!"
clean:
rm -fr *.o Th_lexer_lib2_linux.so
@echo "Remove all TestBox_linux modules!!!"


2.Build nessacary object files that use in library. flag "-fpic" is need.

Worddict.o:Worddict.cpp Thaifunc.h Thaihead.h TypeLinux.h
$(CC) -fpic -c Worddict.cpp

Th_lexer_lib2_linux.o:Th_lexer_lib2_linux.cpp TypeLinux.h
$(CC) -I $(OCCI_HEADER) -fpic -c Th_lexer_lib2_linux.cpp


3.After we get all object files we will collect them to new library by using -shared.
Th_lexer_lib2_linux.so: Th_lexer_lib2_linux.o Worddict.o
$(CC) -shared -o Th_lexer_lib2_linux.so Worddict.o Th_lexer_lib2_linux.o -L. -locci -lclntsh

That's all .... now you can get new library that all function can access by not using _declspec(dllexport) in c code like as window dll.

If you want to limited scope for access you must include mapfile for define local and global part of library.

In g++ complier you must use extern keywords for defined function name correctly for protect undefined symbol problem when you call that function.

extern "C"{
void function_a();
}

And you can check all modules in library by using
nm "library_name"

Goodluck ,

ไม่มีความคิดเห็น: