1

When I try to use libssh2 in my C++ class, I keep getting the following errors:

undefined reference to `libssh2_session_init_ex'
undefined reference to `libssh2_session_startup'

If I do the same thing using C, everything works fine.

Any help?

Following is the build command

g++ -Wall -g -I/libssh2-1.2.4/src -I/libssh2-1.2.4/include -L/libssh2-1.2.4/src/obj -L/openssl-0.9.8k/ -L/SecuritySDK/3.0.13/RC/LATEST/security/lib  -lssh2 -ldl -lnsl -lresolv -lhash -lhandlers -lcrypto -lssl -lz -lbpwp3   rhost.o rpipe.o rutils.o -o rpipe

following is the class member function

void rhost::InitSession()
{
    m_session = libssh2_session_init();
    if ( libssh2_session_startup(m_session, m_sock) )
    {
        fprintf (stderr, "Failure establishing SSH session\n");
        exit(-1);
    }
    return;
}

Yes platform is linux

Avinash
  • 12,851
  • 32
  • 116
  • 186

2 Answers2

2

Change your call to g++ -Wall -g -I/libssh2-1.2.4/src -I/libssh2-1.2.4/include -L/libssh2-1.2.4/src/obj -L/openssl-0.9.8k/ -L/SecuritySDK/3.0.13/RC/LATEST/security/lib rhost.o rpipe.o rutils.o -o rpip -lssh2 -ldl -lnsl -lresolv -lhash -lhandlers -lcrypto -lssl -lz -lbpwp3.

The order of libraries and object files in the parameter list matters. Read about it here.

Community
  • 1
  • 1
Raphael Bossek
  • 1,904
  • 14
  • 25
1

If C works and C++ doesn't, then it sounds like you're failing to include the libssh2 function prototypes in an extern "C" {} block, as described here. (You should double-check the header file; I'm a bit surprised that it doesn't use extern "C" {} itself.)

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • @Avinash: Then I suspect that you're doing something wrong in your build. You'll need to provide more information about how you're building your code. – Josh Kelley Jan 06 '11 at 14:00
  • When I say C work, I tried the examples provided with libssh which are in C and they work with the same build command. – Avinash Jan 06 '11 at 14:07
  • @Avinash: Would you mind posting the exact build command that the example code uses? I wonder if you're somehow getting a different libssh2 (one provided by your Linux distro?) that doesn't define those functions... – Josh Kelley Jan 06 '11 at 14:38
  • I am not using libssh2 provided by linux distro. I am have build my own libssh2 – Avinash Jan 06 '11 at 14:39
  • @Avinash: Are you building libssh2 itself with the C or C++ compiler? – Ben Voigt Jan 06 '11 at 15:05