OpenWrt Forum Archive

Topic: Chaos Calmer Makefile

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

Hello,

I downloaded SDK and build simple package (printing only hello world) for Chaos calmer, that is successfully run.
Now I need to include gpio.h and other header files into main program.

This is how Makefile looks like:

include $(TOPDIR)/rules.mk

# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1


# This specifies the directory where we're going to build the program.  
# The root build directory, $(BUILD_DIR), is by default the build_mipsel 
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk
#include $(INCLUDE_DIR)/autotools.mk
#include $(INCLUDE_DIR)/cmake.mk
#include $(INCLUDE_DIR)/debug.mk
#include $(INCLUDE_DIR)/depends.mk

#include $(INCLUDE_DIR)/toolchain-build.mk
#include $(INCLUDE_DIR)/kernel-defaults.mk
#include $(INCLUDE_DIR)/autotools.mk
#include $(INCLUDE_DIR)/depends.mk
#include $(INCLUDE_DIR)/kernel-defaults.mk
#include $(INCLUDE_DIR)/download.mk
#include $(INCLUDE_DIR)/feeds.mk
#include $(INCLUDE_DIR)/host-build.mk
#include $(INCLUDE_DIR)/host.mk
#include $(INCLUDE_DIR)/image.mk
#include $(INCLUDE_DIR)/kernel-build.mk
#include $(INCLUDE_DIR)/package-bin.mk
#include $(INCLUDE_DIR)/package-defaults.mk
##include $(INCLUDE_DIR)/package-dumpinfo.mk
#include $(INCLUDE_DIR)/package.mk
#include $(INCLUDE_DIR)/package-seccomp.mk
#include $(INCLUDE_DIR)/prereq.mk
#include $(INCLUDE_DIR)/quilt.mk
include $(INCLUDE_DIR)/scan.mk
#include $(INCLUDE_DIR)/scons.mk
#include $(INCLUDE_DIR)/verbose.mk
#include $(INCLUDE_DIR)/subdir.mk
#include $(INCLUDE_DIR)/toplevel.mk
#include $(INCLUDE_DIR)/subdir.mk


# Specify package information for this program. 
# The variables defined here should be self explanatory.
define Package/helloworld
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=Helloworld -- prints a snarky message
endef


# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef


# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one


# Specify where and how to install the program. Since we only have one file, 
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running 
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install 
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the 
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef


# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

Which .mk file should be included?

helloworld.c:

/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <linux/gpio.h>

int main(void)
{
    printf("-----:D\n\n");
    return 0;
}

error after make:

helloworld.c:7:24: fatal error: linux/gpio.h: No such file or directory
 #include <linux/gpio.h>
                        ^
compilation terminated.
make[4]: *** [helloworld.o] Error 1
make[4]: Leaving directory `/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld'
make[3]: *** [/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/build_dir/target-mips_34kc_uClibc-0.9.33.2/helloworld/.built] Error 2
make[3]: Leaving directory `/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/package/helloworld'
make[2]: *** [package/helloworld/compile] Error 2
make[2]: Leaving directory `/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64'
make[1]: *** [/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/staging_dir/target-mips_34kc_uClibc-0.9.33.2/stamp/.package_compile] Error 2
make[1]: Leaving directory `/home/karolina/Documents/CrossCompile/OpenWrt-SDK-15.05-ar71xx-generic_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64'
make: *** [world] Error 2

The purpose of helloworld.c is to one day turn on and off one GPIO pin...

linux/gpio.h is a kernel header file
you don't need to include it to your package, it have to be available for packages after a toolchain building

(Last edited by stas2z on 17 Mar 2016, 11:21)

stas2z wrote:

linux/gpio.h is a kernel header file
you don't need to include it to your package, it have to be available for packages after a building a toolchain

ok...but how can I use some functions from it or from which file should I call functions for setting direction of the GPIO pin, setting the state and reading the state of the pin?
How do you know if it's a kernel header file?

Also can you please please please redo my makefile and helloworld.c, so it can turn on for example pin 18?
I am very new to linux and don't quite understand how things work here hmm...

(Last edited by xorelse on 17 Mar 2016, 11:25)

xorelse wrote:
stas2z wrote:

linux/gpio.h is a kernel header file
you don't need to include it to your package, it have to be available for packages after a building a toolchain

ok...but how can I use some functions from it or from which file should I call functions for setting direction of the GPIO pin, setting the state and reading the state of the pin?
How do you know if it's a kernel header file?

Also can you please please please redo my makefile and helloworld.c, so it can turn on for example pin 18?
I am very new to linux and don't quite understand how things work here hmm...

There are several methods to work with gpio from userspace. The easiest way (if the kernel supports it and it's enabled) is to use sysfs to access gpio registers mapped to /sys/class/gpio/gpio*/* files.

Also you can access to it several other ways but implementation varies depending on hardware.

Anyway, im not sure it's a proper place to discuss basic programming stuff.

stas2z wrote:
xorelse wrote:
stas2z wrote:

linux/gpio.h is a kernel header file
you don't need to include it to your package, it have to be available for packages after a building a toolchain

ok...but how can I use some functions from it or from which file should I call functions for setting direction of the GPIO pin, setting the state and reading the state of the pin?
How do you know if it's a kernel header file?

Also can you please please please redo my makefile and helloworld.c, so it can turn on for example pin 18?
I am very new to linux and don't quite understand how things work here hmm...

There are several methods to work with gpio from userspace. The easiest way (if the kernel supports it and it's enabled) is to use sysfs to access gpio registers mapped to /sys/class/gpio/gpio*/* files.

Also you can access to it several other ways but implementation varies depending on hardware.

Anyway, im not sure it's a proper place to discuss basic programming stuff.

I have already done that part with sysfs and it was working, I have already written small gpio lib to do the same thing. But the thing is why I'm asking so much for that gpio header is because in few days I have to start working with I2c, SPI and other communication protocols so I need to write proper Makefile...

i never used SDK so i can't help you with your trouble, probably u missed something on an installation stage, duno

i have no troubles with kernel headers using compiled buildroot after a tools/toolchain/linux compile

(Last edited by stas2z on 17 Mar 2016, 12:08)

stas2z wrote:

i never used SDK so i can't help you with your trouble, probably u missed something on an installation stage, duno

i have no troubles with kernel headers using compiled buildroot after a tools/toolchain/linux compile


I also have toolchain installed, but somehow never was able to make a package there.

The same helloworld is placed in: openwrt/package/helloworld, when make is run from being in openwrt folder it passes but .ipk is not made...Where should I place helloworld folder?

The discussion might have continued from here.