如何在C ++项目中使用C gnutls API

时间:2011-12-12 13:23:08

标签: c++ c linux gnu ssl

我正在尝试将GnuTLS Doc中的Gnutls示例集成到C ++类中。 不幸的是,它在与g ++链接时会抛出未定义的引用。 所以我尝试编译Example(它在纯C中)而不用g ++进行更改,但没有成功。 如果我使用gcc,它就可以工作。

tino@develeee:~/Code/tlstests$ gcc tlsclient.c -lgnutls
/tmp/ccBqJdQr.o: In function `main':
tlsclient.c:(.text+0x3d0): undefined reference to `tcp_connect'
tlsclient.c:(.text+0x58d): undefined reference to `tcp_close'
collect2: ld returned 1 exit status
tino@develeee:~/Code/tlstests$ g++ tlsclient.c -lgnutls
/tmp/ccRq5DcT.o: In function `load_keys()':
tlsclient.c:(.text+0x186): undefined reference to gnutls_pcert_import_x509_raw(gnutls_pcert_st*, gnutls_datum_t const*, gnutls_x509_crt_fmt_t, unsigned int)'
tlsclient.c:(.text+0x299): undefined reference to gnutls_privkey_init(gnutls_privkey_st**)'
tlsclient.c:(.text+0x2b5): undefined reference to `gnutls_privkey_import_x509(gnutls_privkey_st*, gnutls_x509_privkey_int*, unsigned int)'
/tmp/ccRq5DcT.o: In function `main':
tlsclient.c:(.text+0x37e): undefined reference to `gnutls_certificate_set_retrieve_function2(gnutls_certificate_credentials_st*, int (*)(gnutls_session_int*, gnutls_datum_t const*, int, gnutls_pk_algorithm_t const*, int, gnutls_pcert_st**, unsigned int*, gnutls_privkey_st**))'
tlsclient.c:(.text+0x3d6): undefined reference to `tcp_connect()'
tlsclient.c:(.text+0x594): undefined reference to `tcp_close(int)'
collect2: ld returned 1 exit status

你可以看到只有使用C ++编译器才能获得gnutls / abstract.h中声明的函数。 (忽略对tcp_connect()和tcp_close()的未定义引用,它们根本没有定义并声明为extern)

我认为C ++是C +类和模板吗?所以我无法想象,为什么我会遇到这些错误。

以前有人有这样的事吗?

2 个答案:

答案 0 :(得分:6)

将标题包裹在extern "C" {}块中。看起来gnutls/abstract.h包含在C ++标题中。

答案 1 :(得分:1)

此外,似乎作者也提供了c ++接口。

http://gnutls.org/manual/html_node/Simple-client-example-in-C_002b_002b.html

#include <gnutls/gnutlsxx.h>