我正試圖將來自GnuTLS Doc的Gnutls示例整合到C++類中。 不幸的是,它會在使用g ++進行鏈接時引發未定義的引用。 所以我嘗試編譯示例(它在純C)沒有改變與g ++,但沒有成功。 它工作,如果我使用gcc。如何在C++項目中使用C gnutls API
[email protected]:~/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
[email protected]:~/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
您可以看到只有在gnutls/abstract.h中聲明的函數是使用C++編譯器獲得的。 (忽略對tcp_connect()和tcp_close()的未定義引用,它們根本就沒有被定義和聲明爲extern)
我是否C++是C +類和模板?所以我無法想象,爲什麼我得到這些錯誤。
以前有沒有人有過這樣的事情?
謝謝,它的工作原理:-) –