Skip to content

Write_a_simple_executable_program

Xipeng Wang edited this page Apr 7, 2017 · 1 revision

Write a simple executable program

This is a tutorial for writing an executable program. But actually, you probably don't want to create these files and type in all these every time. So I think the best practice is copying files in the example folders and modifying them.


  • Go to examples folder: cd ./robotx/src/examples
  • Make a new folder called test: mkdir test && cd test
  • Create source file, Rules.mk, Build.mk: touch main.c Rules.mk Build.mk
  • Modify Rules.mk at ./robotx/src/examples. Add: include $(ROOT_PATH)/src/examples/test/Rules.mk
  • Goto to ./robotx, and type: make

1. main.c is your main program. You need include library header files in main.c

//This includes print_util functions in common library.
#include "common/print_util.h" 

//This includes time_util functions in common library. 
//Now you can use all functions in the time_util.h
#include "common/time_util.h" 

//This include all rpi library functions such as pwm, i2c, spi, uart, etc.
#include "rpi/rpi_all.h"


int main(int argc, char **args)
{
    print_marker("XRobot","Hello World!");
    if(rpi_init() != -1){
        print_version();
    }
    return 0;
}

2. Build.mk is used to compile your code.

* `print-test` is the name of executable file. You will find it in `./robotx/bin/`
* If you need common library, you need add `$(CFLAGS_COMMON) $(LDFLAGS_COMMON)` 
* If you need RPI library, you need add `$(CFLAGS_RPI) $(LDFLAGS_RPI)`
SRCS = $(shell ls *.c)
OBJS = $(SRCS:%.c=%.o)
TARGET = $(BIN_PATH)/print-test

CFLAGS := $(CFLAGS_STD) $(CFLAGS_COMMON) $(CFLAGS_RPI)
LDFLAGS := $(LDFLAGS_STD) $(LDFLAGS_COMMON) $(LDFLAGS_RPI)
DEPS := $(DEPS_STD) $(DEPS_COMMON) $(DEPS_RPI)

include $(BUILD_COMMON)

all: $(TARGET)
	@/bin/true

$(TARGET): $(OBJS) $(DEPS)
	@$(LD) -o $@ $^ $(LDFLAGS)

clean:
	@rm -rf *.o $(TARGET)

3. Rules.mk is used to include your Build.mk

* `common` and `rpi` are the libraries you need.
* `print_test` could be changed to any names you want. BUT, you cannot use a same name in other Rules.mk files
* `$(ROOT_PATH)/src/examples/print_test` is the folder path which contains your `Build.mk` file
.PHONY: print_test print_test_clean

print_test: common rpi

print_test:
	@echo $@
	@$(MAKE) -C $(ROOT_PATH)/src/examples/print_test -f Build.mk

print_test_clean:
	@echo $@
	@$(MAKE) -C $(ROOT_PATH)/src/examples/print_test -f Build.mk clean

all: print_test

clean: print_test_clean

Clone this wiki locally