编译 c 和 c++ 模块

Compiling c and c++ module

本文关键字:模块 c++ 编译      更新时间:2023-10-16

假设我正在开发一个已经存在的自由开关模块(https://github.com/signalwire/freeswitch(。它们是动态加载的。

我过去创建过模块,这不是我的问题。我的问题来自一个已经存在的模块,我们称之为my_module。在本模块中,我将添加一个新功能,我需要解密在 AES 中加密的 jwt 令牌参数。

现有的模块主文件,由主 freeSWITCH 加载器加载的文件是 C 语言,我们这样说:

#include <switch.h>
#include <switch_json.h>
#include <switch_stun.h>
#include <jwt.h>
#include "token_crypto.h" <-- This is my addition
...
<some stuff goes here>

在某些时候,我会这样做:

plaintext_len = token_decrypt( *token_encoded, plaintext );

我的token_crypto.h 是

SWITCH_BEGIN_EXTERN_C
#include <stdio.h>
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/sha.h>

void handleErrors(unsigned char *ciphertext);
int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len,
unsigned char *aad, int aad_len,
unsigned char *tag,
unsigned char *key,
unsigned char *iv, int iv_len,
unsigned char *plaintext);
int token_decrypt( const char token_encoded, unsigned char *plaintext );

SWITCH_END_EXTERN_C

然后实际实现token_crypto.cpp

我在 Makefile.am 中添加了编译token_crypto的要求,如下所示:

mod_mymodule_la_SOURCES  = 
base64url.cpp 
token_crypto.cpp 
mod_mymodule.c

然后代码编译正常,但是当我尝试加载它时,我得到:

**/usr/local/freeswitch/mod/mod_mymodule.so: undefined symbol: token_decrypt**

我知道链接器找不到编译的引用,但我只是不知道如何链接它们......

可以在此处找到示例生成文件 https://github.com/signalwire/freeswitch/blob/master/src/mod/applications/mod_skel/Makefile.am

也许我应该指出,在该.c文件上还有其他cpp源代码。

我知道通过使用"extern c"编译器不会破坏函数名称......但是,就实际使用 C 源文件中的函数而言,这意味着什么?

像"谷歌这个","谷歌那个"这样的评论是没有帮助的。在来这里之前,我显然做了所有这些,所以...

所以,显然使用"extern c"包含头文件是不够的......出于某种原因,这就是我记得的。

如果一个人不"extern c"实际的cpp实现,那么功能将被破坏。将"extern C"添加到实现中对我有用。

无论如何,谢谢大家。