September 4, 2014

cross compile nginx for ARM

nginx is a nice web server for embedded systems, but it is frustrating to cross compile because it does not use the standard autoconf structure, and the author provides no documentation to help make this process easier.

This is just my way of cross compiling it, feel fee to comment on your way.

1. compile it for your host. In my case, that's x86_64 Linux. I do the following:

  ./configure --without-pcre --without-http_rewrite_module
  make

2. Now, hand tweak the generated Makefie

   vi objs/Makefile

replace the top few lines with the following (change it to your environment):

TOP := $(dir $(lastword $(MAKEFILE_LIST)))
CC =    arm-none-linux-gnueabi-gcc
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value
CFLAGS += -I${TOP}/../openssl-1.0.1i/openssl-1.0.1i/include  -I/opt/ezsdk/linux-devkit/arm-none-linux-gnueabi/usr/include
LINK =  $(CC) -L${TOP}/../openssl-1.0.1i/

3. You may need to change the file objs/ngx_auto_config.h because some functions available on host are not available on your target. In my case, I had to remove lines containing ACCEPT4

4. clean the old obj files:
   find . -name "*.o" | xargs rm -f

5. make. The final binary is located objs/nginx

Note: make clean will remove the objs/Makefile you just changed. Make sure you have a backup.

7 comments:

  1. Man, thanks. Needed an ARM binary real quick and this really helped.

    ReplyDelete
    Replies
    1. can you tell which version of nginx compiled successfully

      Delete
    2. which version of nginx compiled successfully ?

      Delete
  2. Running the result binary on my arm marchine gives me an error
    nginx: line 1: syntax error: word unexpected (expecting ")")

    ReplyDelete
    Replies
    1. Use qemu-arm to run your binary.

      Delete
  3. i have an error in compilation I do not undertsand what is it

    src/core/ngx_open_file_cache.c
    src/core/ngx_open_file_cache.c: In function 'ngx_file_o_path_info':
    src/core/ngx_open_file_cache.c:601: error: 'AT_EMPTY_PATH' undeclared (first use in this function)
    src/core/ngx_open_file_cache.c:601: error: (Each undeclared identifier is reported only once
    src/core/ngx_open_file_cache.c:601: error: for each function it appears in.)
    src/core/ngx_open_file_cache.c: In function 'ngx_open_file_wrapper':
    src/core/ngx_open_file_cache.c:660: error: 'O_PATH' undeclared (first use in this function)
    make[1]: *** [objs/src/core/ngx_open_file_cache.o] Error 1
    make[1]: Leaving directory `/home/erlab/workdir/nginx-1.8.1'

    please help me :(

    same with nginx 1.12.1
    -o objs/src/core/ngx_open_file_cache.o \
    src/core/ngx_open_file_cache.c
    src/core/ngx_open_file_cache.c: In function 'ngx_file_o_path_info':
    src/core/ngx_open_file_cache.c:601: error: 'AT_EMPTY_PATH' undeclared (first use in this function)
    src/core/ngx_open_file_cache.c:601: error: (Each undeclared identifier is reported only once
    src/core/ngx_open_file_cache.c:601: error: for each function it appears in.)
    src/core/ngx_open_file_cache.c: In function 'ngx_open_file_wrapper':
    src/core/ngx_open_file_cache.c:660: error: 'O_PATH' undeclared (first use in this function)
    make[1]: *** [objs/src/core/ngx_open_file_cache.o] Error 1
    make[1]: Leaving directory `/home/erlab/workdir/nginx-1.12.1'
    make: *** [build] Error 2

    ReplyDelete
  4. Hello
    I tried to compile by your instructions, I have an error at the end that pwritev is not defined :(

    ReplyDelete