我有一个简单的C++程序,可以调用C函数。当我链接2个对象文件来创建C++可执行文件时,我得到了一些“未定义的引用”错误和“SvrPoT”的多个引用错误。我有以下文件chorusmain.cpp、chorusclient.c和chorusclient.h.chorusmain.cpp对chorusclient.c中的函数进行调用。chorusclient.c非常大,我两年前就编写了它。现在我需要在C++应用程序中使用这些函数,不想在C++中重写并重新调试代码。我将chorusclient.c重构为一组可调用函数。为了简单起见,我将代码简化为重要的组件。
绒毛膜细胞

#include <...>
#include "chorusclient.h"

int my_C_Function()
{
  ...
}

绒毛膜细胞
#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

前两个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程序并构建一个链接时,我不会得到未定义的引用错误,并且程序可以正常工作。有什么想法吗?

最佳答案

你说你有:

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>而不是#include "chorusclient.h"),而且您绝对不需要重复-I.
有可能您需要重新排序链接中的库列表-最好将-lpthread放在末尾。
我添加了-Wall -Werror选项,因为您不应该尝试运行代码,除非它至少在这些选项下编译得很干净(我会在代码中添加更多的内容——这足以说明问题)。
我有点惊讶您需要-I/usr/include/openssl行;您是否应该使用#include <openssl/ssl.h>etc(用于OpenSSL头文件)以便不需要-I/usr/include/openssl命令行选项?类似地,可能是Qt和JSON-C头?
另一个问题是你有一个对象-可能是一个全局变量-在svrportchorusclient.o中都被定义为chorusmain.o。这意味着通过某种机制,在两个源文件中定义(而不是声明)了相同的对象。这可能是因为它是定义的,而不是在头中声明的(但代码中没有显示)。或者可能是因为在两个源文件中都定义了它。如果它需要是一个全局变量,则应该在头文件中声明它,并在其中一个源文件中定义它(因为头文件正在声明它),而不是在另一个源文件中定义它。

09-30 00:11