首页 > 解决方案 > 如何将共享库添加到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,所以这一定是我编译的问题。我该怎么办?
谢谢

标签: cnginx

解决方案


您仍然必须将库添加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失败并显示您观察到的错误消息。


推荐阅读