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