' undefined reference to ' with namespaces in g++

Jul 6, 2008 at 12:39am
hi,
Im new to this forum ,and I would realy like if anybody can help me

When I compile client.cpp file with -c option it works fine ,but when I try to link it gives lot of errors hers a part of the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

// //  #include "test_8x8.xpm"

#include <rcssbase/thread.h>
#include <rcssbase/messagequeue.h>
#include <rcssbase/sharedvar.h>
#include <rcssbase/net/socketstreambuf.hpp>
#include <rcssbase/net/udpsocket.hpp>
#include <rcssbase/gzip/gzstream.hpp>




#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include <iostream>
#include <cstdio>
#include <cstdlib>

// #include <cerrno>
// #include <netdb.h>

#ifdef HAVE_LIBNCURSES
#include <ncurses.h>
#endif

using namespace rcss;

class Client {
private:
    //friend
    class ServerReader {
    private:
        Client & M_client;
        std::istream & M_src;
    public:
        ServerReader( Client & client,
                      std::istream & src )
            : M_client( client ),
              M_src( src )
          { }

        ~ServerReader()
          { }

        void operator()()
          {
              while ( M_src.good() )
              {
                  char buffer[ 8192 ];
                  M_src.getline( buffer, 8192, 0 );
                  M_client.processInput( buffer );
              }
              M_client.displayError( "Error receiving from server" );
          }
    };

    //friend
    class ServerWriter {
    private:
        Client & M_client;
        std::ostream & M_dest;
    public:
        ServerWriter( Client & client,
                      std::ostream & dest )
            : M_client( client ),
              M_dest( dest )
          { }

        ~ServerWriter()
          { }

        void
        operator()()
          {
              while ( M_dest.good() )
              {
                  char buffer[ 8192 ];
                  M_client.getData( buffer, 8192 );
                  M_dest << buffer << std::ends << std::flush;
              }
              M_client.displayError( "Error sending to server" );
          }
    };

    rcss::net::UDPSocket M_socket;
    rcss::net::SocketStreamBuf M_write_strmbuf;
    rcss::net::SocketStreamBuf M_read_strmbuf;
    rcss::gz::gzostream M_write_strm;
    rcss::gz::gzistream M_read_strm;
    rcss::thread::SharedVar< bool > M_cycle_is_clean;
    rcss::thread::MessageQueue< int > M_level;

    ServerWriter M_server_writer;
    rcss::thread::Thread M_server_writer_thread;
    ServerReader M_server_reader;
    rcss::thread::Thread M_server_reader_thread;







imantha@VAIO:~/rcss/rcssserver-12.1.0/src$ g++ client.o
client.o: In function `main':
client.cpp:(.text+0x16d): undefined reference to `rcss::net::Addr::ANY'
client.cpp:(.text+0x17c): undefined reference to `rcss::net::Addr::Addr(unsigned short, unsigned int)'
client.cpp:(.text+0x1a3): undefined reference to `rcss::net::Addr::setHost(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
client.o: In function `rcss::net::SocketStreamBuf::writeData()':
client.cpp:(.text._ZN4rcss3net15SocketStreamBuf9writeDataEv[rcss::net::SocketStreamBuf::writeData()]+0x46): undefined reference to `rcss::net::Socket::isConnected() const'
and many more similar errors


rcss is declared and implemented in a seperate folder

addr.hpp (defenition)
addr.cpp (implementation)
addr.lo

Can sombody show me what am I doing wrong?

thanks in advance
Jul 6, 2008 at 2:09am
I'm sure I just answered a similar Q, but I can't find it.

I don't see a main() in your code, so a couple of those errors seem odd to me, but they resolve to the same problem.

When you say
g++ client.o
you are asking GCC to just call the linker (/usr/bin/ld*) to turn your .o into an executable.

Every executable must have a main() function. I don't see one in your code. (Hence the oddness of the first error.)

The second problem is that you have asked GCC to link your .o with ... nothing -- but the .o requires the rcss code.

Is rcss a library installed with your compiler (in /usr/lib/ and /usr/include/)? Or is it in another directory under your project's main directory?

If the former, make sure you are linking with the appropriate library. Your compilation should look something like:

g++ -o myprog mymain.o client.o -lrcss


I just made that up. I don't know if "rcss" is the library name or not, but whatever it is you need to link the library in that manner. If you need to link more than one library, put each one with its own -l switch.

"mymain.o" came from "mymain.cpp" which defines main() and executes your program, and presumably uses the class defined in client.h and client.cpp.

Your executable will be named "myprog".

I just noticed that you are using NCurses, so you'll have to link with -lcurses also.

If it is just another .o file somewhere, you'll have to list it on the command line:

g++ -o myprog mymain.o client.o ./rcss/foo.o ... -lcurses


Whew. Hope this helps.
Last edited on Jul 6, 2008 at 2:13am
Jul 6, 2008 at 5:51pm
Thanks for the reply,

I just included a part of that file (not enough space) , there is a main implemented in that file . I don't think there is a fault in this program because its a well known application (robocup) and I can run the whole package when i ' ./configure' ,'make' , 'make install' it ,I'm hoping to change this client file ,so Im trying to create the executable before I do any modification.

'rcss' is implemented in several files on a different folder (.hpp & .cpp files )

should there be a library as well?

or

can I create the 'library object file' which implements the 'rcss' and link it with client.o ,(something like this )

g++ -o client client.o rcss_file.lo (where rcss_file.lo is the library object file )

any help is much appreciated

many thanks

Jul 6, 2008 at 9:15pm
The first option will do. You just have to use the -L and -I command line arguments to tell g++ where the .cpp and .hpp files are.
Topic archived. No new replies allowed.