having main in shared library

A

athresh

I have created a shared library with main() .Still it is compiled and
loaded during invocation. Below is the code and build file.

sample_test.cpp
int test_method(CfgChkSession &s)
{
cout<<"working"<<endl;
return 1;
}

int main()
{
cout << "Hello, This is a sample test\n";
return 0;

}


Sample build file is :
all:libcfgchk_samplelib.so

libcfgchk_samplelib.so : $(OBJS)
$(GCC) -shared -Wl,-soname,libcfgchk_samplelib.so -o
libcfgchk_samplelib.so $(OBJS) - -lutil -lext_stl -lprocess -lnsl -
lcfgchk_utils -ldl

I am curious to know how the .so in invoked in the program which has a
main(). Please update me on the same
 
D

Dan Noland

I have created a shared library with main() .Still it is compiled and
loaded during invocation. Below is the code and build file.

sample_test.cpp
int test_method(CfgChkSession &s)
{
cout<<"working"<<endl;
return 1;

}

int main()
{
cout << "Hello, This is a sample test\n";
return 0;

}

Sample build file is :
all:libcfgchk_samplelib.so

libcfgchk_samplelib.so : $(OBJS)
$(GCC) -shared -Wl,-soname,libcfgchk_samplelib.so -o
libcfgchk_samplelib.so $(OBJS) - -lutil -lext_stl -lprocess -lnsl -
lcfgchk_utils -ldl

I am curious to know how the .so in invoked in the program which has a
main(). Please update me on the same

I believe that in the .so main is a "weak symbol" which prevents the
multiply defined symbol error you are expecting. The global symbol
main is chosen preferentially over the weak symbol main in the shared
object.
http://www.tortall.net/projects/yasm/manual/html/objfmt-elf-directives.html#objfmt-elf-dir-weak

YT,
Dan Noland
http://nolandda.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top