libssh2 and C++

强颜欢笑 提交于 2019-12-11 10:27:42

问题


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


回答1:


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.




回答2:


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.)



来源:https://stackoverflow.com/questions/4615563/libssh2-and-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!