c - 如何将共享库添加到nginx源代码中进行编译?
问题描述
我正在 Nginx(Centos7.6,Nginx1.16) 上开发几个模块,所有这些模块都依赖于本地缓存的键值对库。它是 libshmcache键值对
,所以我想将它构建到 Nginx 中,但我正在编译它以使用错误:未定义对shmcache_init_from_file_ex
.
这是我在编译时使用的命令:
./configure --prefix=/root/test/nginx --user=www --group=www
--with-openssl=/root/test/openssl-1.0.2s
--with-http_ssl_module
--with-threads
--with-debug
--add-module=/root/test/nginx-libshmcache-test-module
--with-ld-opt="-L /root/libfastcommon/src/libfastcommon -L /root/libfastcommon/libshmcache/src/libshmcache"
--with-cc-opt="-I/usr/local/include"
nginx-libshmcache-test-module的代码很简单,没有任何问题,这是它的核心代码。
#include "fastcommon/logger.h"
#include "fastcommon/shared_func.h"
#include "shmcache/shmcache.h"
static ngx_http_module_t ngx_libshmcache_test_module = {
NULL,
ngx_libshmcache_test_func, //Call ngx_libshmcache_test_func function when the configuration file is loaded
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
static ngx_int_t ngx_libshmcache_test_func(ngx_conf_t *cf)
{
int result;
struct shmcache_context context;
result = shmcache_init_from_file_ex(&context,
"/root/test/lib-cache/libshmcache/conf/libshmcache.conf", false, true))
return NGX_OK;
}
我按照上面的命令编译的时候,由于加载了ngx_libshmcache_test_func方法,导致Nginx的启动出错,undefined reference,所以这一定是我编译的问题。我该怎么办?
谢谢
解决方案
您仍然必须将库添加libshmcache
为对共享对象的引用。
您可以通过-lshmcache
在链接共享对象时指定来做到这一点,例如:
gcc -O2 -shared nginx-libshmcache-test-module.c -o nginx-libshmcache-test-module.so -fPIC -lshmcache -L/root/libfastcommon/libshmcache/src/libshmcache
否则,libshmcache
当您的共享对象打开时,不会在运行时链接,并且函数的符号查找和动态链接将shmcache_init_from_file_ex
失败并显示您观察到的错误消息。
推荐阅读
- python - python请求给
- angular - ngrx 8:来自 reducer 函数的 @ngrx/data 实体的访问状态
- python-3.x - 如何更改字符串的特定部分?
- javascript - 选择 6 个下拉选项之一时 Javascript 隐藏字段
- bluetooth - 蓝牙经典 5.1?
- c++ - 将一个类的对象放入另一个类的数组中
- c++ - 如何获得 BITMAPFILEHEADER 和 BITMAPINFOHEADER 的字节表示?
- string - Bash:如何计算文件中字符串的出现次数?
- python - 调试“ImportError:尝试使用没有已知父包的相对导入”
- c# - 如何使用 OpenCvSharp4 v4.2 合并两个图像