Skip to content

Complete cross compilation example

tat edited this page Sep 14, 2012 · 2 revisions

by AUGER Mickael (from France)

This document gives an example of complete and sample program (named myproject) using some modules (Configuration) of LibU. The compilation use MaKL and a custom cross compilation for ARM. MaKL and LibU are install only in the user folder (/home/ma/myproject/ in the example).

Part 1 : local installation of MaKL

Objective : configure and install MaKL in the user folder. This installation of MaKL is independent of the target.

  • Download the last version of MaKL (here 1.9.0) - Instead of type all command line in shell, create a script such as

      #!/bin/sh
      ### install_makl.sh ###
      GNUMAKE=make
      LOCAL_DIR=`pwd`
      #here LOCAL_DIR=/home/ma/myproject
    
      V_MAKL=1.9.0
      MAKL_DIR=$LOCAL_DIR/makl
      MAKL_BIN=$MAKL_DIR/bin
    
      tar zxvf makl-${V_MAKL}.tar.gz
      cd makl-${V_MAKL}/
      ${GNUMAKE} toolchain
      ./configure --gnu_make=${GNUMAKE} --prefix="${MAKL_DIR}"
      ${GNUMAKE} install
      
  • Execute : ./install_makl.sh - Add the binary folder of MaKL in your PATH

      export PATH="${PATH}:${MAKL_BIN}"
      

Part 2 : create a toolchain file

In this example, we compile for the ARM architecture. Here the binary are in /home/public/arm-2007q3/bin/.

  • Create .tc file

      ### arm.tc ###
      XC_BASE = /home/public/arm-2007q3/bin
      ARM_CFLAGS = -Wunused-variable -Wunused-parameter -Wunused-function
      #compile pour gdb
      #ARM_CFLAGS = -g -Wunused-variable -Wunused-parameter -Wunused-function
      ARM_CXXFLAGS = ${ARM_CFLAGS}
    
      CC = ${XC_BASE}/arm-none-linux-gnueabi-gcc ${ARM_CFLAGS}
      CXX = ${XC_BASE}/arm-none-linux-gnueabi-g++ ${ARM_CXXFLAGS}
      AR = ${XC_BASE}/arm-none-linux-gnueabi-ar
      RANLIB = ${XC_BASE}/arm-none-linux-gnueabi-ranlib
      LD = ${XC_BASE}/arm-none-linux-gnueabi-ld
      NM = ${XC_BASE}/arm-none-linux-gnueabi-nm
      STRIP =
      INSTALL_STRIP =
      

Part 3 : local installation of LibU

Objective : configure and install LibU in the user folder.

  • Download the last version of LibU (here 2.2.0) - Instead of type all command line in shell, create a script such as

      #!/bin/sh
      ### install_libu.sh ###
      GNUMAKE=make
      LOCAL_DIR=`pwd`
    
      V_LIBU=2.2.0
      LIBU_DIR=$LOCAL_DIR/libu
    
      tar zxvf libu-${V_LIBU}.tar.gz
      cd libu-${V_LIBU}/
    
      #we can disabled all unused modules of LibU
      #and specifiy cross_compile to disabled test
      makl-conf --prefix="${LIBU_DIR}" --no_hmap --no_net --no_env --no_fs --no_pwd --no_list --no_array --no_ringbuffer --no_pqueue --no_json --no_bst --cross_compile
    
      makl --makl-tc-file=${LOCAL_DIR}/arm.tc --makl-tc-onthefly
      makl install
      
  • Execute

      ./install_libu.sh
      

Now, we can create our project !

Part 4 : create the project

Our project will be very minimal : - one library (named mylib) - one main source file (program and named myproject)

MaKL can create a template for each type of package : include, program, library, man ... We use it like this (see also MaKL Wiki for more information)

  • Create the main Makefile and the tree of folder

      mkdir mylib program include
      makl-new subdir
      
  • Edit Makefile to add SUBDIR, CFLAGS and LDADD such as

      SUBDIR = include mylib program
      CFLAGS += $(LIBU_CFLAGS)
      LDADD += $(LIBU_LDADD)
    
      include subdir.mk
      
  • Create a VERSION file that contains the version of our project (here 1.0.0)

      1.0.0
      
  • Init include folder

      cd include
      makl-new include
      
  • Edit include/Makefile to add INCS such as

      include common.mk
      include ../Makefile.conf
    
      INCS = mylib.h
    
      include incs.mk
      
  • Create a new file mylib.h in include such as

      #ifndef MYLIB_H
      #define MYLIB_H
    
      #include "conf.h"
    
      #include 
      /*
      add here the other include
      */
    
      void hello();
      #endif
      
  • Init mylib folder

      cd mylib
      makl-new lib
      
  • Edit mylib/Makefile to add LIB, SRCS, CFLAGS ... such as

      include common.mk
      include ../Makefile.conf
    
      LIB = mylib
      SRCS = mylib.c
    
      CFLAGS += -I../include
      CFLAGS += $(LIBU_CFLAGS)
      LDADD += $(LIBU_LDADD)
    
      include lib.mk
      
  • Create a new file mylib.c in mylib such as

      #include "mylib.h"
    
      void hello()
      {
        u_con("Hello LibU world !");
      }
      
  • Init program folder

      cd program
      makl-new prog
      
  • Edit program/Makefile to add PROG, SRCS, CFLAGS ... such as

      include common.mk
      include ../Makefile.conf
    
      PROG = myproject
      SRCS = main.c
    
      #LDFLAGS += __SETME__
      CFLAGS += $(LIBU_CFLAGS)
      CFLAGS += -I../include
      LDADD += $(LIBU_LDADD)
      LDADD += ../mylib/libmylib.a
    
      include prog.mk
      
  • Create a new file main.c in program such as

      #include "mylib.h"
    
      int facility = LOG_LOCAL0;
    
      int main(int argc, char *argv[])
      {
        printf("VERSION=%s\n",MYPROJECT_VERSION);
        
        hello();
        
        /*
        for example, use the Configuration module of LibU
        */
        
        return 0;
      }
      

At this step, the contents of /home/ma/myproject/ is such as :

- /home/ma/myproject/
-- include
--- Makefile
--- mylib.h
-- program
--- Makefile
--- main.c
-- mylib
--- mylib.c
--- Makefile
-- libu
--- ... installation of LibU
-- libu-2.2.0
--- ... source of LibU
-- makl
--- ... installation of MaKL
-- makl-1.9.0
--- ... source of MaKL
-- Makefile
-- arm.tc
-- VERSION

Part 5 : configure the project

Objective : configure the project for the target.

  • Create a file such as – This file will be automatically use by MaKL

      #!/bin/sh
      ### configure.sh ###
    
      GNUMAKE=make
      LOCAL_DIR=`pwd`
    
      V_MAKL=1.9.0
      MAKL_DIR=$LOCAL_DIR/makl
      MAKL_BIN=$MAKL_DIR/bin
      LIBU_DIR=$LOCAL_DIR/libu
      MAKL_DIR=${MAKL_DIR}/share/makl-${V_MAKL}
    
      . "${MAKL_DIR}"/cf/makl.init
      makl_args_init "$@"
    
      makl_pkg_name "xtc_utilities"
      makl_pkg_version
    
      makl_set_var_mk "LOCALDIR" "${LOCAL_DIR}"
      makl_append_var_mk "CFLAGS" "-I\$(LOCALDIR)"
      makl_append_var_mk "CFLAGS" "-I\$(LOCALDIR)/include"
    
      makl_set_var_mk "LIBUDIR" "${LIBU_DIR}"
      makl_append_var_mk "LIBU_CFLAGS" "-I\$(LIBUDIR)/include"
      makl_append_var_mk "LIBU_LDADD" "\$(LIBUDIR)/lib/libu.a"
    
      . "${MAKL_DIR}"/cf/makl.term
      
  • Instead of type all command line in shell, create a script such as

      #!/bin/sh
      ### config_project.sh ###
      GNUMAKE=make
      LOCAL_DIR=`pwd`
    
      LIBU_DIR=$LOCAL_DIR/libu
    
      makl-conf --lib-u=${LIBU_DIR}
      
  • Execute ./config_project.sh – This execution create a Makefile.conf and conf.h

Part 6 : compile the project

The compilation is the more simple operation :

makl --makl-tc-file=${LOCAL_DIR}/arm.tc --makl-tc-onthefly clean all
  • This only command line will be call after each modification of source