I've just downloaded the C++ sockets library from www.alhem.net/Sockets, but I can't get my programs to link -- I get a lot of "undefined reference"s.... I'm using g++ version 3.3.3. on Fedora Core 2 (kernel version 2.6.5-1.358).
I can get the programs to compile (using the -c flag to g++), but any attempt to link results in the error below. My program is a verbatim copy from the tutorial on the site.
It appears that (some of) the functions for the SocketHandler and TcpSocket classes are not implemented, but I've checked, and they're there!
I'll really appreciate any help anyone can give.

Here's the compiler command I use:

g++ -L/usr/local/lib -I/usr/local/include/Sockets -o sock_test sock_test.cpp

If I use the -c flag to that command, I get no errors, meaning that I can compile, but the linker can't find the functions it's complaining about, right? I wonder if I missed something while downloading?

Please help!!

MT

/*************************************
 *****  Here's the code  *************
 *************************************/
#include <TcpSocket.h>
#include <SocketHandler.h>

class DisplaySocket : public TcpSocket {
public:
	DisplaySocket(SocketHandler&);
	void OnRead();
};

DisplaySocket::DisplaySocket(SocketHandler& h) : TcpSocket(h) {}

#define RSIZE 1024

void DisplaySocket::OnRead() {

	TcpSocket::OnRead();
	size_t n = ibuf.GetLength();
	if (n > 0) {
		char tmp[RSIZE];

		n = (n > RSIZE) ? RSIZE : n;
		ibuf.Read(tmp, n);
		printf("Read %d bytes:\n", n);
		for (size_t i = 0; i < n; i++) {
			printf("%c", isprint(tmp[i]) ? tmp[i] : '.');
			}
		printf ("\n");
	}
}

int main() {
	SocketHandler h;
	DisplaySocket *p = new DisplaySocket(h);

	p->SetDeleteByHandler();
	p->Open("localhost", 9002);
	h.Add(p);
	h.Select(1, 0);
	while (h.GetCount()) {
		h.Select(1, 0);
	}
}

/*************************************
***** And here's the error output ***
*************************************/

/tmp/ccbnbrB1.o(.text+0x10): In function `DisplaySocket::DisplaySocket[not-in-charge](SocketHandler&)':
: undefined reference to `TcpSocket::TcpSocket[not-in-charge](SocketHandler&)'
/tmp/ccbnbrB1.o(.text+0x32): In function `DisplaySocket::DisplaySocket[in-charge](SocketHandler&)':
: undefined reference to `TcpSocket::TcpSocket[not-in-charge](SocketHandler&)'
/tmp/ccbnbrB1.o(.text+0x54): In function `DisplaySocket::OnRead()':
: undefined reference to `TcpSocket::OnRead()'
/tmp/ccbnbrB1.o(.text+0xb7): In function `DisplaySocket::OnRead()':
: undefined reference to `CircularBuffer::Read(char*, unsigned)'
/tmp/ccbnbrB1.o(.text+0x17b): In function `main':
: undefined reference to `SocketHandler::SocketHandler[in-charge](StdLog*)'
/tmp/ccbnbrB1.o(.text+0x20e): In function `main':
: undefined reference to `Socket::SetDeleteByHandler(bool)'
/tmp/ccbnbrB1.o(.text+0x25b): In function `main':
: undefined reference to `TcpSocket::Open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned short)'
/tmp/ccbnbrB1.o(.text+0x2e7): In function `main':
: undefined reference to `SocketHandler::Add(Socket*)'
/tmp/ccbnbrB1.o(.text+0x2fd): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccbnbrB1.o(.text+0x30f): In function `main':
: undefined reference to `SocketHandler::GetCount()'
/tmp/ccbnbrB1.o(.text+0x32b): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccbnbrB1.o(.text+0x34b): In function `main':
: undefined reference to `SocketHandler::~SocketHandler [in-charge]()'
/tmp/ccbnbrB1.o(.text+0x371): In function `main':
: undefined reference to `SocketHandler::~SocketHandler [in-charge]()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x10): undefined reference to `Socket::Init()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x14): undefined reference to `Socket::Close()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x1c): undefined reference to `TcpSocket::OnWrite()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x20): undefined reference to `Socket::OnException()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x24): undefined reference to `Socket::OnDelete()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x28): undefined reference to `Socket::OnConnect()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x2c): undefined reference to `Socket::OnAccept()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x30): undefined reference to `TcpSocket::OnLine(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x34): undefined reference to `Socket::OnSSLInitDone()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x38): undefined reference to `Socket::OnConnectFailed()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x3c): undefined reference to `Socket::CheckConnect()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x40): undefined reference to `TcpSocket::ReadLine()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x44): undefined reference to `Socket::SSLCheckConnect()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x4c): undefined reference to `Socket::GetPort()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTV13DisplaySocket+0x50): undefined reference to `TcpSocket::SendBuf(char const*, unsigned)'
/tmp/ccbnbrB1.o(.gnu.linkonce.t._ZN13DisplaySocketD1Ev+0x16): In function `DisplaySocket::~DisplaySocket [in-charge]()':
: undefined reference to `TcpSocket::~TcpSocket [not-in-charge]()'
/tmp/ccbnbrB1.o(.gnu.linkonce.t._ZN13DisplaySocketD0Ev+0x16): In function `DisplaySocket::~DisplaySocket [in-charge deleting]()':
: undefined reference to `TcpSocket::~TcpSocket [not-in-charge]()'
/tmp/ccbnbrB1.o(.gnu.linkonce.r._ZTI13DisplaySocket+0x8): undefined reference to `typeinfo for TcpSocket'
collect2: ld returned 1 exit status

Recommended Answers

All 9 Replies

Try this:

g++ -o sock_test sock_test.cpp -L/usr/local/lib -I/usr/local/include/Sockets

Try this:

g++ -o sock_test sock_test.cpp -L/usr/local/lib -I/usr/local/include/Sockets

Thanks, Narue. Unfortunately, I still get exactly the same error when I try your suggestion.

>I still get exactly the same error when I try your suggestion.
It was worth a shot. Have you tried linking directly with the library (using the -l<lib> switch)?

Are you sure the library is built for your compiler?
You might have to build it from source first if it was compiled against another compiler (version).

Thanks again. Yes, you're right. I eventually had to link directly against my library, and also the pthread library:

... -lSockets -lpthread

Problem now sorted.

Cheers.

I'm having similar issues, but including the lib explicitly doesn't fix it. I'm using Sockets 1.7.1 with the following g++ line:

g++ `Sockets-config` -o udpserver -I/usr/local/include/Sockets \
-L/usr/local/lib/  -lSockets  UdpTestSocket.cpp udpserver.cpp

Of course Sockets-config returns:

-D_VERSION='"1.7.1"' -DLINUX -DHAVE_OPENSSL

I took udpserver.cpp from the tutorial on the C++ Sockets homepage and modified it just a little. Here's what I'm compiling this:

#include <StdoutLog.h>
#include <SocketHandler.h>

#include "UdpTestSocket.h"


int main(int argc,char *argv[])
{
 SocketHandler h;
 StdoutLog log;
 h.RegStdLog(&log);
 UdpTestSocket s(h);
 port_t port = 5353;

 if (s.Bind(port, 10) == -1)
 {
  printf("Exiting...\n");
  exit(-1);
 }
 else
 {
  printf("Ready to receive on port %d\n",port);
 }
 s.AddMulticastMembership("224.0.0.251");
 s.SetMulticastTTL(255);
 h.Add(&s);
 h.Select(1,0);
 while (h.GetCount())
 {
  h.Select(1,0);
 }

}

I'm compiling on GNU/Linux using GCC 3.3.4. I compiled this same code on my Powerbook running OS X (10.3.6) using GCC 3.3 by Apple. I had similar problems under OS X, for some reason I had to run ranlib on the library again and it worked fine. Also, I can compile the same on the Linux box to object files, it just doesn't link. This is obvious from the error ouput below.

The exact output is below.

Thanks

bash-2.05b$ g++ `Sockets-config` -o udpserver -I/usr/local/include/Sockets -L/usr/local/lib/ -lSockets UdpTestSocket.cpp udpserver.cpp
/tmp/ccVwaqcS.o(.text+0x1c): In function `UdpTestSocket::UdpTestSocket(SocketHandler&)':
: undefined reference to `UdpSocket::UdpSocket(SocketHandler&, unsigned int)'
/tmp/ccVwaqcS.o(.text+0x48): In function `UdpTestSocket::UdpTestSocket(SocketHandler&)':
: undefined reference to `UdpSocket::UdpSocket(SocketHandler&, unsigned int)'
/tmp/ccVwaqcS.o(.text+0xc2): In function `UdpTestSocket::OnRawData(char const*,unsigned int, sockaddr*, unsigned int)':
: undefined reference to `Socket::l2ip(unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/tmp/ccVwaqcS.o(.text+0x1f4): In function `UdpTestSocket::OnRawData(char const*, unsigned int, sockaddr*, unsigned int)':
: undefined reference to `Socket::l2ip(in6_addr const&, std::basic_string<char,std::char_traits<char>, std::allocator<char> >&, bool)'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x10): undefined reference to `Socket::Init()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x14): undefined reference to `Socket::Close()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x18): undefined reference to `UdpSocket::OnRead()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x1c): undefined reference to `Socket::OnWrite()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x20): undefined reference to `Socket::OnException()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x24): undefined reference to `Socket::OnDelete()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x28): undefined reference to `Socket::OnConnect()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x2c): undefined reference to `Socket::OnAccept()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x30): undefined reference to `Socket::OnLine(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x34): undefined reference to `Socket::OnSSLInitDone()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x38): undefined reference to `Socket::OnConnectFailed()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x3c): undefined reference to `Socket::CheckConnect()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x40): undefined reference to `Socket::ReadLine()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x44): undefined reference to `Socket::SSLCheckConnect()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x4c): undefined reference to `Socket::GetPort()'
/tmp/ccVwaqcS.o(.gnu.linkonce.t._ZN13UdpTestSocketD1Ev+0x16): In function `UdpTestSocket::~UdpTestSocket()':
: undefined reference to `UdpSocket::~UdpSocket()'
/tmp/ccVwaqcS.o(.gnu.linkonce.t._ZN13UdpTestSocketD0Ev+0x16): In function `UdpTestSocket::~UdpTestSocket()':
: undefined reference to `UdpSocket::~UdpSocket()'
/tmp/ccVwaqcS.o(.gnu.linkonce.r._ZTI13UdpTestSocket+0x8): undefined reference to `typeinfo for UdpSocket'
/tmp/ccpbIqWq.o(.text+0x26): In function `main':
: undefined reference to `SocketHandler::SocketHandler(StdLog*)'
/tmp/ccpbIqWq.o(.text+0x4c): In function `main':
: undefined reference to `SocketHandler::RegStdLog(StdLog*)'
/tmp/ccpbIqWq.o(.text+0x8d): In function `main':
: undefined reference to `UdpSocket::Bind(unsigned short&, int)'
/tmp/ccpbIqWq.o(.text+0x153): In function `main':
: undefined reference to `UdpSocket::AddMulticastMembership(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/tmp/ccpbIqWq.o(.text+0x22e): In function `main':
: undefined reference to `UdpSocket::SetMulticastTTL(int)'
/tmp/ccpbIqWq.o(.text+0x246): In function `main':
: undefined reference to `SocketHandler::Add(Socket*)'
/tmp/ccpbIqWq.o(.text+0x264): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccpbIqWq.o(.text+0x272): In function `main':
: undefined reference to `SocketHandler::GetCount()'
/tmp/ccpbIqWq.o(.text+0x296): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccpbIqWq.o(.text+0x2e4): In function `main':
: undefined reference to `SocketHandler::~SocketHandler()'
/tmp/ccpbIqWq.o(.text+0x306): In function `main':
: undefined reference to `SocketHandler::~SocketHandler()'
/tmp/ccpbIqWq.o(.gnu.linkonce.t._ZN9StdoutLogC1Ev+0x16): In function `StdoutLog::StdoutLog()':
: undefined reference to `vtable for StdoutLog'
collect2: ld returned 1 exit status
bash-2.05b$ g++ `Sockets-config` -o udpserver -I/usr/local/include/Sockets -L/usr/local/lib/ -lSockets UdpTestSocket.cpp udpserver.cpp
/tmp/ccl6Rgot.o(.text+0x1c): In function `UdpTestSocket::UdpTestSocket(SocketHandler&)':
: undefined reference to `UdpSocket::UdpSocket(SocketHandler&, unsigned int)'
/tmp/ccl6Rgot.o(.text+0x48): In function `UdpTestSocket::UdpTestSocket(SocketHandler&)':
: undefined reference to `UdpSocket::UdpSocket(SocketHandler&, unsigned int)'
/tmp/ccl6Rgot.o(.text+0xc2): In function `UdpTestSocket::OnRawData(char const*,unsigned int, sockaddr*, unsigned int)':
: undefined reference to `Socket::l2ip(unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
/tmp/ccl6Rgot.o(.text+0x1f4): In function `UdpTestSocket::OnRawData(char const*, unsigned int, sockaddr*, unsigned int)':
: undefined reference to `Socket::l2ip(in6_addr const&, std::basic_string<char,std::char_traits<char>, std::allocator<char> >&, bool)'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x10): undefined reference to `Socket::Init()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x14): undefined reference to `Socket::Close()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x18): undefined reference to `UdpSocket::OnRead()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x1c): undefined reference to `Socket::OnWrite()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x20): undefined reference to `Socket::OnException()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x24): undefined reference to `Socket::OnDelete()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x28): undefined reference to `Socket::OnConnect()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x2c): undefined reference to `Socket::OnAccept()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x30): undefined reference to `Socket::OnLine(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x34): undefined reference to `Socket::OnSSLInitDone()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x38): undefined reference to `Socket::OnConnectFailed()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x3c): undefined reference to `Socket::CheckConnect()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x40): undefined reference to `Socket::ReadLine()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x44): undefined reference to `Socket::SSLCheckConnect()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTV13UdpTestSocket+0x4c): undefined reference to `Socket::GetPort()'
/tmp/ccl6Rgot.o(.gnu.linkonce.t._ZN13UdpTestSocketD1Ev+0x16): In function `UdpTestSocket::~UdpTestSocket()':
: undefined reference to `UdpSocket::~UdpSocket()'
/tmp/ccl6Rgot.o(.gnu.linkonce.t._ZN13UdpTestSocketD0Ev+0x16): In function `UdpTestSocket::~UdpTestSocket()':
: undefined reference to `UdpSocket::~UdpSocket()'
/tmp/ccl6Rgot.o(.gnu.linkonce.r._ZTI13UdpTestSocket+0x8): undefined reference to `typeinfo for UdpSocket'
/tmp/ccADbXyN.o(.text+0x26): In function `main':
: undefined reference to `SocketHandler::SocketHandler(StdLog*)'
/tmp/ccADbXyN.o(.text+0x4c): In function `main':
: undefined reference to `SocketHandler::RegStdLog(StdLog*)'
/tmp/ccADbXyN.o(.text+0x8d): In function `main':
: undefined reference to `UdpSocket::Bind(unsigned short&, int)'
/tmp/ccADbXyN.o(.text+0x153): In function `main':
: undefined reference to `UdpSocket::AddMulticastMembership(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/tmp/ccADbXyN.o(.text+0x22e): In function `main':
: undefined reference to `UdpSocket::SetMulticastTTL(int)'
/tmp/ccADbXyN.o(.text+0x246): In function `main':
: undefined reference to `SocketHandler::Add(Socket*)'
/tmp/ccADbXyN.o(.text+0x264): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccADbXyN.o(.text+0x272): In function `main':
: undefined reference to `SocketHandler::GetCount()'
/tmp/ccADbXyN.o(.text+0x296): In function `main':
: undefined reference to `SocketHandler::Select(long, long)'
/tmp/ccADbXyN.o(.text+0x2e4): In function `main':
: undefined reference to `SocketHandler::~SocketHandler()'
/tmp/ccADbXyN.o(.text+0x306): In function `main':
: undefined reference to `SocketHandler::~SocketHandler()'
/tmp/ccADbXyN.o(.gnu.linkonce.t._ZN9StdoutLogC1Ev+0x16): In function `StdoutLog::StdoutLog()':
: undefined reference to `vtable for StdoutLog'
collect2: ld returned 1 exit status

commented: Let sleeping threads lie. +0

Hi
When compiling and linking with gcc/g++ you should always enter the libs part last on your command line, like this:

g++ `Sockets-config` -o udpserver -I/usr/local/include/Sockets \
UdpTestSocket.cpp udpserver.cpp -L/usr/local/lib/ -lSockets -lpthread

I tested this right now and it works. Also note that I added -lpthread, since the library needs that.

Best regards
/ah

Hi
When compiling and linking with gcc/g++ you should always enter the libs part last on your command line, like this:

g++ `Sockets-config` -o udpserver -I/usr/local/include/Sockets \
UdpTestSocket.cpp udpserver.cpp -L/usr/local/lib/ -lSockets -lpthread

/ah

It's not a little late for this?

It's not a little late for this?

Yes. Better late than not at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.