Zeng
Zeng

Reputation: 107

How to add Shared libraries to the nginx source code to compile?

I'm developing several modules on Nginx(Centos7.6,Nginx1.16), all of which rely on a local cached key-value pair library. It's libshmcache key-value pair , so I want to build it into Nginx, but I'm compiling it to use with an error: undefined reference to shmcache_init_from_file_ex.

This is the command I used at compile time:

   ./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"

The code for nginx-libshmcache-test-module is very simple, and it has no problems,This is its core code.

#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;
}

When I compiled according to the above command, the start-up of Nginx caused an error due to loading the ngx_libshmcache_test_func method, undefined reference, so this must be the problem of my compilation. What should I do?
Thank you

Upvotes: 1

Views: 1211

Answers (1)

Ctx
Ctx

Reputation: 18420

You still have to add the library libshmcache as a reference to your shared object.

You can do this by specifying -lshmcache when linking the shared object, for example like this:

gcc -O2 -shared nginx-libshmcache-test-module.c -o nginx-libshmcache-test-module.so -fPIC -lshmcache -L/root/libfastcommon/libshmcache/src/libshmcache

Otherwise, the libshmcache is not linked on runtime when your shared object is opened, and the symbol lookup and dynamic linking for the function shmcache_init_from_file_ex fails with the error message you observe.

Upvotes: 1

Related Questions