OpenWrt Forum Archive

Topic: How to cross-compile a program for openwrt router?

The content of this topic has been archived on 11 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

I wanted to compile a program for my openwrt router from source i already have but to add an option.

I ran ./autogen.sh in the terminal but it returned aclocal not found.

Please guide me through this as i am new to the programming world

Thx in advance !

is it a single program or a package ?
does it depend on too many things ?

you can build a make file for it .
you can also try directly compiling  if you have a toolchain ready and your program is simple to compile.

what package are you trying to compile ?

Hi,

I am trying to recompile rtorrent and libtorrent with gcc 4.3.3 since the one in the openwrt trunk wasn't working. Others say it's because the gcc used is old. I got the source of the package from openwrt as well. I use ubuntu 10.10 and where so i get the tool chain? what is it used for??

Thx

Hi,

its not so easy, there is no complete and up-to-date guide anywhere. I went through the process of picking up all the required info from serveral wiki pages, blog posts, forums entries, and trying around a little by myself. I created a little guide for myself, so next time I need to do it, it is not so much work anymore. This is for backfire (current release of openwrt). This is my guide:

HOWTO create a simple c/c++ executable for openwrt backfire:

1. set up development environment:

sudo apt-get install subversion gcc g++ libncurses5-dev zlib1g-dev gawk flex patch
cd
svn co svn://svn.openwrt.org/openwrt/branches/backfire
cd backfire
scripts/feeds update
scripts/feeds install uclibcxx
# here, you can also install other packages that are in the repository, using scripts/feeds install xyz. You can find packages with scripts/feed list | grep xyz
make menuconfig
# select "target system" (in my case: atheros ar71xx)
# select "target profile" (in my case: tp-link tl-wr1043nd v1)
# enable "build the openwrt sdk"
# under "libraries" select uclibcxx
# also select other packages you might have installed before. They can be in any section or subsection, so you might have to search.
# exit (save)
make

2. create openwrt makefile for new project:

mkdir package/helloworld
# create package/helloworld/Makefile with the contents:

include $(TOPDIR)/rules.mk

PKG_NAME:=helloworld
PKG_RELEASE:=1
PKG_SOURCES:=~/dev/$(PKG_NAME)
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=$(PKG_NAME)
endef

MAKE_FLAGS += \
    CFLAGS="$(TARGET_CFLAGS)" \
    CXXFLAGS="$(TARGET_CXXFLAGS) -fno-builtin -fno-rtti -nostdinc++" \
    CPPFLAGS="$(TARGET_CPPFLAGS) -I$(STAGING_DIR)/usr/include/uClibc++ -I$(LINUX_DIR)/include" \
    LDFLAGS="$(TARGET_LDFLAGS) $(LDFLAGS)" \
    LIBS="$(TARGET_LIBS) -nodefaultlibs -luClibc++ -lm" \
    DESTDIR="$(PKG_INSTALL_DIR)"

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) $(PKG_SOURCES)/* $(PKG_BUILD_DIR)/
endef

define Build/Compile
    $(MAKE) -C $(PKG_BUILD_DIR) $(MAKE_FLAGS);
endef

define Package/$(PKG_NAME)/install
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/bin/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

make menuconfig
# under "utilities" select helloworld
# exit (save)

3. create normal project:

mkdir ~/dev/helloworld
# create ~/dev/helloworld/Makefile with the contents:

EXECUTABLE=helloworld
SOURCES=helloworld.cpp

OBJECTS=$(patsubst %.c,%o,$(patsubst %.cpp,%.o,$(SOURCES)))
LDFLAGS += -s

all: $(SOURCES) $(EXECUTABLE)
    
clean:
    rm -f *.o $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
ifeq (,$(findstring .cpp,$(SOURCES)))
    $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
else
    $(CXX) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
endif

.c.o:
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

.cpp.o:
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

# create ~/dev/helloworld/helloworld.cpp with the contents:

#include <cstdio>

int main()
{
    std::printf("hello, std::world!\n");
    return 0;
}

4. build and run:

make package/helloworld/clean && make package/helloworld/compile

# copy & run executable
scp build_dir/target-*/helloworld/helloworld root@pranzas.homedns.org:
ssh root@192.168.1.1
./helloworld

# or instead copy & install package & run executable:
scp bin/*/packages/helloworld*.ipk root@openwrt:/tmp
ssh root@192.168.1.1
opkg install /tmp/helloworld*.ipk
helloworld

5. when developing, it can also easily be compiled for the host platform:

cd ~/dev/helloworld
make clean && make all
./helloworld

Hi,

The source I downloaded doesnt have any makefile. I ran ./autogen.sh and ./configre but all kinds of error showed up.

Also, Can i compile on the router itself??

Thx

The openwrt build process is designed to also work with software that come with a configure script. But I don't know how that exactly is handled. Maybe you can find out by looking at some openwrt makefiles that compile packages that are shipped with a configure script. Check out the Makefiles in the package directory in the openwrt subversion repository, they serve as examples.

If the configure script fails (even when using for the host system), then it normally means you have to install some additional software in order to compile the package. Please read the error message to understand what the problem is.

I think it might even be possible to compile on the router directly. But if anything I bet its much more complicated. I would not recommend it, as long as you even have trouble compiling it on a real computer.

Hi, I just wanted to try your example program, but...

here is my problem: I just made all files from your guide, I copied the code into them and I am able to see the helloworld in the Utilities submenu of the configuration screen, but if I run the "make package/helloworld/clean && make package/helloworld/compile" there is an "missing separator" error in the makefile, if I fix this I m no longer able to see the helloworld in the Utilities submenu. And even if I try to run "make package/helloworld/clean && make package/helloworld/compile" I m getting the ERROR: please fix package/helloworld/Makefile and *** No rule to make target `package/helloworld/clean`. Stop.

Please, what did I do wrong? I have a Ubuntu 10.10 and I was able to build the environment in first step normally

thanks for help...

I will try to help you during my free time.. i have some posts made earlier which give you example of how to build packages.
https://forum.openwrt.org/viewtopic.php?id=26028  <- I have built a package for some small tool
try this link..
if you still have problems post here.. either me or somebody else will surely try to help you out.

hope this helps

can you post your make file here ?

The discussion might have continued from here.