我有一个简单的C ++程序,可以调用C函数。当我链接2个目标文件以创建C ++可执行文件时,我得到了一些“未定义的引用”错误和单个“多个svrport引用”错误。我有以下文件chorusmain.cpp,chorusclient.c和chorusclient.h。 chorusmain.cpp在chorusclient.c中调用函数。 chorusclient.c非常大,我在2年前写过它。我现在需要在C ++应用程序中使用这些函数,并且不想在C ++中重新编写并重新调试代码。我将chorusclient.c重构为一组可调用函数。为简单起见,我将代码简化为重要的组件。
chorusclient.c
#include <...>
#include "chorusclient.h"
int my_C_Function()
{
...
}
chorusclient.h
#ifdef __cplusplus
extern "C" {
#endif
int my_C_Function();
#ifdef __cplusplus
}
#endif
chorusmain.cpp
#include <...>
#include "chorusclient.h"
int main(int argc, char *arvg[])
{
...
my_C_Function();
...
}
这是gcc / g ++构建和链接命令,
gcc -c -o chorusclient.o chorusclient.c -I/usr/include/json-c -
I/usr/include/openssl -I/usr/include -I. -I. -lssl -lcrypto -ljson-c
g++ -c -o chorusmain.o chorusmain.cpp -I/usr/share/qt4/mkspecs/linux-g++ -I. -
I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -
I/usr/include -I. -I. -lQtGui -lQtCore -lpthread
g++ -o ChorusMain chorusclient.o chorusmain.o
前2个gcc / g ++版本没有错误。我在g ++链接期间得到以下未定义的引用错误
debian@beaglebone:~/Projects/CloudClient$ ls
chorusclient.c chorusclient.h chorusclient.o chorusmain.cpp chorusmain.o
debian@beaglebone:~/Projects/CloudClient$ g++ -o ChorusMain chorusmain.o chorusclient.o
chorusclient.o:(.data+0x0): multiple definition of `svrport'
chorusmain.o:(.data+0x0): first defined here
chorusclient.o: In function `chorusServices':
chorusclient.c:(.text+0x408): undefined reference to `OPENSSL_init_crypto'
chorusclient.c:(.text+0x416): undefined reference to `OPENSSL_init_crypto'
chorusclient.c:(.text+0x41c): undefined reference to `OPENSSL_config'
chorusclient.o: In function `httpSend':
chorusclient.c:(.text+0x1c2c): undefined reference to `OPENSSL_init_ssl'
chorusclient.c:(.text+0x1c3a): undefined reference to `OPENSSL_init_ssl'
chorusclient.c:(.text+0x1c4c): undefined reference to `OPENSSL_init_ssl'
chorusclient.c:(.text+0x1c50): undefined reference to `TLSv1_2_client_method'
chorusclient.c:(.text+0x1c58): undefined reference to `SSL_CTX_new'
chorusclient.c:(.text+0x1c62): undefined reference to `SSL_CTX_set_options'
chorusclient.c:(.text+0x1c7e): undefined reference to `ERR_print_errors_fp'
chorusclient.c:(.text+0x1ca0): undefined reference to `SSL_new'
chorusclient.c:(.text+0x1caa): undefined reference to `SSL_set_fd'
chorusclient.c:(.text+0x1cb0): undefined reference to `SSL_connect'
chorusclient.c:(.text+0x1ce4): undefined reference to `SSL_get_peer_certificate'
chorusclient.c:(.text+0x1cec): undefined reference to `X509_get_subject_name'
chorusclient.c:(.text+0x1cfa): undefined reference to `X509_NAME_oneline'
chorusclient.c:(.text+0x1d1a): undefined reference to `X509_get_issuer_name'
chorusclient.c:(.text+0x1d28): undefined reference to `X509_NAME_oneline'
chorusclient.c:(.text+0x1d3e): undefined reference to `X509_free'
chorusclient.c:(.text+0x1d4a): undefined reference to `SSL_get_current_cipher'
chorusclient.c:(.text+0x1d52): undefined reference to `SSL_CIPHER_get_name'
chorusclient.c:(.text+0x1d80): undefined reference to `SSL_write'
chorusclient.c:(.text+0x1d90): undefined reference to `SSL_read'
chorusclient.c:(.text+0x1df2): undefined reference to `SSL_read'
chorusclient.c:(.text+0x1e8e): undefined reference to `SSL_free'
chorusclient.c:(.text+0x1e9a): undefined reference to `SSL_CTX_free'
chorusclient.o: In function `jsonBatteryData':
chorusclient.c:(.text+0x21a8): undefined reference to `json_object_new_object'
chorusclient.c:(.text+0x21b2): undefined reference to `json_object_new_string'
chorusclient.c:(.text+0x21be): undefined reference to `json_object_new_string'
chorusclient.c:(.text+0x21ca): undefined reference to `json_object_new_string'
chorusclient.c:(.text+0x21d8): undefined reference to `json_object_new_double'
chorusclient.c:(.text+0x21e6): undefined reference to `json_object_new_double'
chorusclient.c:(.text+0x21f4): undefined reference to `json_object_new_double'
chorusclient.c:(.text+0x2202): undefined reference to `json_object_new_double'
chorusclient.c:(.text+0x2210): undefined reference to `json_object_new_double'
chorusclient.c:(.text+0x2220): undefined reference to `json_object_object_add'
chorusclient.c:(.text+0x222e): undefined reference to `json_object_object_add'
chorusclient.c:(.text+0x223c): undefined reference to `json_object_object_add'
chorusclient.c:(.text+0x224a): undefined reference to `json_object_object_add'
chorusclient.c:(.text+0x2258): undefined reference to `json_object_object_add'
chorusclient.o:chorusclient.c:(.text+0x2266): more undefined references to `json_object_object_add' follow
chorusclient.o: In function `jsonBatteryData':
chorusclient.c:(.text+0x2292): undefined reference to `json_object_to_json_string'
chorusclient.o: In function `blockencrypt':
chorusclient.c:(.text+0x22de): undefined reference to `EVP_CIPHER_CTX_new'
chorusclient.c:(.text+0x22ee): undefined reference to `EVP_aes_256_cbc'
chorusclient.c:(.text+0x22fe): undefined reference to `EVP_EncryptInit_ex'
chorusclient.c:(.text+0x231a): undefined reference to `EVP_EncryptUpdate'
chorusclient.c:(.text+0x233c): undefined reference to `EVP_EncryptFinal_ex'
chorusclient.c:(.text+0x2354): undefined reference to `EVP_CIPHER_CTX_free'
chorusclient.o: In function `decrypt':
chorusclient.c:(.text+0x2370): undefined reference to `EVP_CIPHER_CTX_new'
chorusclient.c:(.text+0x2380): undefined reference to `EVP_aes_256_cbc'
chorusclient.c:(.text+0x2390): undefined reference to `EVP_DecryptInit_ex'
chorusclient.c:(.text+0x23ac): undefined reference to `EVP_DecryptUpdate'
chorusclient.c:(.text+0x23ce): undefined reference to `EVP_DecryptFinal_ex'
chorusclient.c:(.text+0x23e6): undefined reference to `EVP_CIPHER_CTX_free'
chorusclient.o: In function `handleErrors':
chorusclient.c:(.text+0x2404): undefined reference to `ERR_print_errors_fp'
collect2: error: ld returned 1 exit status
debian@beaglebone:~/Projects/CloudClient$
当我创建一个调用chorusclient.c的简单C程序并构建链接时,我没有得到任何未定义的引用错误,并且程序正常工作。有什么想法吗?
答案 0 :(得分:2)
你说你有:
gcc -c -o chorusclient.o chorusclient.c -I/usr/include/json-c \
-I/usr/include/openssl -I/usr/include -I. -I. -lssl -lcrypto -ljson-c
g++ -c -o chorusmain.o chorusmain.cpp -I/usr/share/qt4/mkspecs/linux-g++ -I. \
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 \
-I/usr/include -I. -I. -lQtGui -lQtCore -lpthread
g++ -o ChorusMain chorusclient.o chorusmain.o
在编译时列出库以创建目标文件;在尝试链接可执行文件时,您不会列出库。创建目标文件时,这些库是不相关的 - 它们在链接可执行文件时至关重要。
因此,您需要更多类似的内容:
gcc -Wall -Werror -c -o chorusclient.o chorusclient.c -I/usr/include/json-c \
-I/usr/include/openssl -I/usr/include -I.
g++ -Wall -Werror -c -o chorusmain.o chorusmain.cpp -I/usr/share/qt4/mkspecs/linux-g++ \
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 \
-I/usr/include -I.
g++ -o ChorusMain chorusclient.o chorusmain.o -lQtGui -lQtCore -lpthread -lssl -lcrypto -ljson-c
我不相信你需要-I/usr/include
,而且你经常不需要-I.
(除非你坚持使用#include <chorusclient.h>
而不是{{1} }}),你绝对不需要重复#include "chorusclient.h"
。
您可能需要重新排序链接中的库列表 - 最好将-I.
放在最后。
我添加了-lpthread
选项,因为您不应该尝试运行代码,除非它至少在这些选项下干净地编译(我在代码中添加更多内容 - 这足以说得好。
我有点惊讶你需要-Wall -Werror
行;您是否应该使用-I/usr/include/openssl
等(对于OpenSSL标头),以便您不需要#include <openssl/ssl.h>
命令行选项?类似地,也许,使用Qt和JSON-C标头?
另一个问题是你有一个对象 - 可能是一个全局变量 - 在-I/usr/include/openssl
和svrport
中都定义了chorusclient.o
。这意味着通过某种机制或其他机制,您在两个源文件中都定义了相同的对象(而不是声明)。这可能是因为它是在头文件中定义而不是声明(但代码中没有显示)。或者可能是因为您在两个源文件中都定义了它。如果它需要是一个全局变量,它应该在头文件中声明并在其中一个源文件中定义(可能chorusmain.o
,因为chorusclient.c
标头声明它)并且没有在另一个中定义。