【问题标题】:Compiling nginx module with dependant library使用依赖库编译 nginx 模块
【发布时间】:2016-01-27 23:59:29
【问题描述】:

我已经按照这里的教程成功构建了一个简单的 nginx 模块:https://github.com/perusio/nginx-hello-world-module

我可以毫无问题地构建和运行该代码,并且它可以正常运行。

我现在想将 MySQL 的依赖添加到简单的 nginx 模块中,所以我使用了 MySQL C 连接器http://dev.mysql.com/doc/refman/5.6/en/c-api-building-clients.html

你能写的最简单的程序是这个(取自http://zetcode.com/db/mysqlc/):

mysql_test.c

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv) 
{
  printf("MySQL client version: %s\n", mysql_get_client_info());  
  exit(0);
}

然后你像这样编译:

$ gcc mysql_test.c -o mysql_test  `mysql_config --cflags --libs`

这很好用。

现在,当我尝试在我的简单 nginx hello-world 模块中包含两个头文件 my_global.hmysql.h 时,我需要一种指定这些构建标志的方法,否则它将找不到这些头文件。目前,当我运行make,它成功构建所有其他模块后,我现在得到:

cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g   -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/mail \
        -o objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o \
        /vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c
/vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c:33:23: fatal error: my_global.h: No such file or directory
 #include <my_global.h>
                       ^
compilation terminated.
make[1]: *** [objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o] Error 1
make[1]: Leaving directory `/vagrant/nginx'
make: *** [build] Error 2

我的问题是:如何让 nginx 在构建我的模块时包含这些构建标志?

我还没有找到任何关于使用 Nginx 构建依赖项的信息。

谢谢!

【问题讨论】:

    标签: mysql c nginx


    【解决方案1】:

    编译控制

    --with-cc-opt=参数 设置将添加到 CFLAGS 变量的附加参数。

    --with-ld-opt=参数 设置将在链接期间使用的附加参数。

    Source

    对于 MySQL 连接器 C,它将类似于:

    --with-cc-opt="-I/usr/include/mysql"
    --with-ld-opt="-L/usr/lib64/mysql"
    

    在你模块的config 文件中:

    CORE_LIBS="$CORE_LIBS -lmysqlclient"
    

    在编译期间,它还可能会抱怨 _GNU_SOURCE 被重新定义,如果您使用 -Werror 而不实施解决方法,它将失败。

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2017-08-12
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多