Creating and sharing a c++ shared library ?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Creating and sharing a c++ shared library ?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Creating and sharing a c++ shared library ?  
vivekian


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-25-07 06:17 PM

Hi ,

These are the steps i followed to create a shared library

1. Compiled the cpp files

g++ -c -g -fpic  -Wall -Wno-deprecated SockException.cpp -o
SockException.o
g++ -c -g -fpic -Wall -Wno-deprecated Socket.cpp -o Socket.o
g++ -c -g -fpic -Wall -Wno-deprecated TcpSocket.cpp -o TcpSocket.o
g++ -c -g -fpic -Wall -Wno-deprecated UdpSocket.cpp -o UdpSocket.o
g++ -c -g -fpic -Wall -Wno-deprecated SocketReaderWriter.cpp -o
SocketReaderWriter.o
g++ -shared -Wl,-soname,libcppsocket.so.1 -o libcppsocket.so.1.0.1
SockException.o Socket.o TcpSocket.o UdpSocket.o SocketReaderWriter.o -
lc

2. Copied the compiled libcppsocket.so.1 to the directories /usr/lib

3. Moved the header files to the directories /usr/include and /usr/
local/include

4. To test the compiled library , i wrote a small program

#include <TcpSocket.h>

int main ()
{
TcpSocket sock ;
}

where TcpSocket.h is one of the header files copied to /usr/include

5. Compiling this test program

g++  main.cpp
/tmp/ccPqeVa9.o: In function `main':
main.cpp.text+0x8a): undefined reference to `TcpSocket::TcpSocket()'
main.cpp.text+0x95): undefined reference to
`TcpSocket::~TcpSocket()'
collect2: ld returned 1 exit status

gives the above error.

This i suppose indicates that the library has to be linked. But the
following doesnt help
g++ main.cpp -llibcppsocket

What am i missing here ?

Thanks in advance,
vivekian






[ Post a follow-up to this message ]



    Re: Creating and sharing a c++ shared library ?  
Markus Kossmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:16 AM

vivekian wrote:

> Hi ,
>
> These are the steps i followed to create a shared library
>
> 1. Compiled the cpp files
>
> g++ -c -g -fpic  -Wall -Wno-deprecated SockException.cpp -o
> SockException.o
> g++ -c -g -fpic -Wall -Wno-deprecated Socket.cpp -o Socket.o
> g++ -c -g -fpic -Wall -Wno-deprecated TcpSocket.cpp -o TcpSocket.o
> g++ -c -g -fpic -Wall -Wno-deprecated UdpSocket.cpp -o UdpSocket.o
> g++ -c -g -fpic -Wall -Wno-deprecated SocketReaderWriter.cpp -o
> SocketReaderWriter.o
> g++ -shared -Wl,-soname,libcppsocket.so.1 -o libcppsocket.so.1.0.1
> SockException.o Socket.o TcpSocket.o UdpSocket.o SocketReaderWriter.o -
> lc
>
> 2. Copied the compiled libcppsocket.so.1 to the directories /usr/lib
>
> 3. Moved the header files to the directories /usr/include and /usr/
> local/include
>
> 4. To test the compiled library , i wrote a small program
>
> #include <TcpSocket.h>
>
> int main ()
> {
>         TcpSocket sock ;
> }
>
> where TcpSocket.h is one of the header files copied to /usr/include
>
> 5. Compiling this test program
>
> g++  main.cpp
> /tmp/ccPqeVa9.o: In function `main':
> main.cpp.text+0x8a): undefined reference to `TcpSocket::TcpSocket()'
> main.cpp.text+0x95): undefined reference to
> `TcpSocket::~TcpSocket()'
> collect2: ld returned 1 exit status
>
> gives the above error.
>
> This i suppose indicates that the library has to be linked. But the
> following doesnt help
> g++ main.cpp -llibcppsocket
>
> What am i missing here ?
The linker looks only for .so and .a files ( without any version number)
Create a link libcppsocket.so -> libcppsocket.so.1.0.1.






[ Post a follow-up to this message ]



    Re: Creating and sharing a c++ shared library ?  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:16 AM

"vivekian" <viveklinux@gmail.com> writes:

> These are the steps i followed to create a shared library

These steps are un-necessarily complicated.
You are (apparently) following some DSO-HOWTO, but without
understanding why things are done in "complicated" way, and whether
that complicated way is appropriate for your situation.

> g++ -shared -Wl,-soname,libcppsocket.so.1 -o libcppsocket.so.1.0.1
> SockException.o Socket.o TcpSocket.o UdpSocket.o SocketReaderWriter.o -
> lc

Until you need "external library versioning", I suggest you use
"simple" way:

g++ -shared -o libcppsocket.so \
SockException.o Socket.o TcpSocket.o UdpSocket.o SocketReaderWriter.o

> 5. Compiling this test program
>
> g++  main.cpp
> /tmp/ccPqeVa9.o: In function `main':
> main.cpp.text+0x8a): undefined reference to `TcpSocket::TcpSocket()'
> main.cpp.text+0x95): undefined reference to
> `TcpSocket::~TcpSocket()'
> collect2: ld returned 1 exit status
>
> gives the above error.
>
> This i suppose indicates that the library has to be linked.

Correct.

> But the following doesnt help
> g++ main.cpp -llibcppsocket

And it shouldn't help. Provided /usr/lib/libcppsocket.so exists,
correct link line is:

g++ main.cpp -lcppsocket

> What am i missing here ?

Well, for starters you didn't tell us (nor paid attention to)
the error linker gave you, which likely was:

/usr/bin/ld: cannot find -llibcppsocket

Please read "info gcc" to understand what library the linker will
search for given '-llibcppsocket' argument. Hint libcppsocket.*
it is not.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.





[ Post a follow-up to this message ]



    Re: Creating and sharing a c++ shared library ?  
vivekian


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-07 12:16 AM

On Feb 25, 2:19 pm, Paul Pluzhnikov <ppluzhnikov-...@charter.net>
wrote:
> "vivekian" <vivekli...@gmail.com> writes: 
>
> These steps are un-necessarily complicated.
> You are (apparently) following some DSO-HOWTO, but without
> understanding why things are done in "complicated" way, and whether
> that complicated way is appropriate for your situation.
> 
>
> Until you need "external library versioning", I suggest you use
> "simple" way:
>
>   g++ -shared -o libcppsocket.so \
>     SockException.o Socket.o TcpSocket.o UdpSocket.o SocketReaderWriter.o
> 
> 
> 
> 
>
> Correct.
> 
>
> And it shouldn't help. Provided /usr/lib/libcppsocket.so exists,
> correct link line is:
>
>   g++ main.cpp -lcppsocket
> 
>
> Well, for starters you didn't tell us (nor paid attention to)
> the error linker gave you, which likely was:
>
>   /usr/bin/ld: cannot find -llibcppsocket

Yes, you are right , i didnt mention that , but yes i did notice it.
>
> Please read "info gcc" to understand what library the linker will
> search for given '-llibcppsocket' argument. Hint libcppsocket.*
> it is not.

I did. This is the relevant portion i guess :
"The only difference between using an -l option and specifying a file
name is that -l surrounds library with lib and .a and   searches
several directories"

That solved the problem. It compiles fine. I was following a HowTo
without understanding  .

Thanks a ton . Appreciate it.

vivekian






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:46 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register