OpenWrt Forum Archive

Topic: Errors in compiling hello world code

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

Hi all. I am trying to compile the hello world code,
#include <stdio.h>

int main(void)
{
printf("Hell! O' world, why won't my code compile?\n\n");
return 0;
}

with this makefile via using SDK

#
# Copyright (C) 2008 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# $Id$

include $(TOPDIR)/rules.mk

PKG_NAME:=helloworld
PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define Package/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld -- prints a snarky message 
endef

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

define Build/Configure
endef

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

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

$(eval $(call BuildPackage,helloworld))



When i run the command via the SDK directory " makeV=99" . I get on output,

saad@saad-VirtualBox:~/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1$ make V=99
make[1]: Entering directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1'
make[2]: Entering directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1'
make[3]: Entering directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/package/helloworld'
make -C /home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/build_dir/target-mips_r2_uClibc-0.9.30.1/helloworld AR=mips-openwrt-linux-uclibc-ar AS="mips-openwrt-linux-uclibc-gcc -c -Os -pipe -mips32r2 -mtune=mips32r2 -funit-at-a-time -fhonour-copts -msoft-float" LD=mips-openwrt-linux-uclibc-ld NM=mips-openwrt-linux-uclibc-nm CC="mips-openwrt-linux-uclibc-gcc" GCC="mips-openwrt-linux-uclibc-gcc" CXX="mips-openwrt-linux-uclibc-g++" RANLIB=mips-openwrt-linux-uclibc-ranlib STRIP=mips-openwrt-linux-uclibc-strip OBJCOPY=mips-openwrt-linux-uclibc-objcopy OBJDUMP=mips-openwrt-linux-uclibc-objdump SIZE=mips-openwrt-linux-uclibc-size
make[4]: Entering directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/build_dir/target-mips_r2_uClibc-0.9.30.1/helloworld'
mips-openwrt-linux-uclibc-gcc  helloworld.o -o helloworld
/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/staging_dir/toolchain-mips_r2_gcc-4.3.3+cs_uClibc-0.9.30.1/usr/bin/../lib/gcc/mips-openwrt-linux-uclibc/4.3.3/../../../../mips-openwrt-linux-uclibc/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status
make[4]: *** [helloworld] Error 1
make[4]: Leaving directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/build_dir/target-mips_r2_uClibc-0.9.30.1/helloworld'
make[3]: *** [/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/build_dir/target-mips_r2_uClibc-0.9.30.1/helloworld/.built] Error 2
make[3]: Leaving directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/package/helloworld'
make[2]: *** [package/helloworld/compile] Error 2
make[2]: Leaving directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1'
make[1]: *** [/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/staging_dir/target-mips_r2_uClibc-0.9.30.1/stamp/.package_compile] Error 2
make[1]: Leaving directory `/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1'
make: *** [world] Error 2

Can anybody guide me why the code isnot compiling ?

have you got the make file and the source in the correct directory structure?
Compare what you have with other Packages in
/home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/Package

Yes, one makefile is in /home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/Package/helloworld/Makefile  (on the same level with src directory)

Source code is in /home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/Package/helloworld/src

Second makefile is in /home/saad/OpenWrt-SDK-ar71xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/Package/helloworld/src

Thanks for pointing out the error.
The error shows that the cross compiler isnot able to find libgcc library which is required for linking ?

Should i install the required libraries into my helloworld package folder in the SDK?

No. I'm not sure where they should be, but not there. I suspect that nobody is using just the SDK, and that is why paths are still wrong.
The easiest way, though not the fastest, would be to install the required tools and libs via build root:
http://wiki.openwrt.org/doc/howto/buildroot.exigence
Once compiled, you can use the instructions for compiling your own packages.

I am not expert with Openwrt Makefiles, but I wonder if your declared empty configure section voids the underlying default configure section.

And the same for the compile section.

Wiki recommends leaving them empty unless you are doing something specific:
http://wiki.openwrt.org/doc/devel/packa … ge.defines

Build/Configure (optional)
You can leave this undefined if the source doesn't use configure or has a normal config script, otherwise you can put your own commands here or use "$(call Build/Configure/Default,)" as above to pass in additional arguments for a standard configure script.

Build/Compile (optional)
How to compile the source; in most cases you should leave this undefined.

(Last edited by hnyman on 11 Feb 2013, 14:54)

Hi all. I wanted to tell that I have successfully compiled the package and now I have a helloworld.ipk file. Can anybody guide me how to install .ipk file on router ?

opkg install XXXX.ipk
http://wiki.openwrt.org/doc/techref/opkg

Ps. Please tell also others, what you did to make it compile. What was the problem?

(Last edited by hnyman on 15 Feb 2013, 18:00)

I removed spaces and incorrect indentation from my helloworld/Makefile , resultantly i was able to successfully compile the code smile


Thanks for your reply . I tried your command
root@OpenWrt:~# opkg install helloworld_1_ar71xx.ipk
Installing helloworld (1) to root...
Configuring helloworld.
root@OpenWrt:~#
root@OpenWrt:~#
root@OpenWrt:~# helloworld
/bin/helloworld: line 1: syntax error: unexpected "("

The program is running  on terminal as it just has to print a line, but in openwrt, it is giving me error. Any suggestions ?

The discussion might have continued from here.