Makefile简介

网络上关于 makefile的教程有很多,由于我日常不是写c/c++的, 不常使用makefile,需要用的时候总是要重新Google搜索makefile的语法。 索性整理出来这篇 makefile 教程,备忘。 教程 Makefile简易教程: 基本语法 1 2 3 4 target: dependency1 dependency2 ... [TAB] action1 [TAB] action2 ... 下面的makefile摘抄自GNU Make in Detail for Beginners,这篇入门文章把makefile的语法写的非常透彻。 推荐大家多读几遍 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ##### makefile for compile C programs # Compiler to use CC = gcc # -g for debug, -O2 for optimise and -Wall additonal messages OPTIONS = -O2 -g -Wall # Directory for header file INCLUDES = -I . # List of objects to be build OBJS = main.o module.o .PHONY: all list clean all: ${OBJS} @echo "Building..." # print "Building..." message ${CC} ${OPTIONS} ${INCLUDES} ${OBJS} -o main_bin %.o: %.c # '%' pattern wildcard matching ${CC} ${OPTIONS} ${INCLUDES} -c %.c list: @echo $(shell ls) # print output of command `ls` clean: @echo Cleaning up... -rm -rf *.0 # '-' prefix for ignoring errors and continue execution -rm main_bin #### makefile for img manage FILES = $(shell find imgs -type f -iname "*.jpg" | sed 's/imgs/thumb/g') CONVERT_CMD = convert -resize "100x100" $< $@ MSG = "\nUpdating thumbnail" $@ all_thumb: ${FILES} thumb/%.jpg: imgs/%.jpg ${MSG} ${CONVERT_CMD} thumb/%.JPG: imgs/%.JPG ${MSG} ${CONVERT_CMD} clean_all: @echo Cleaning up files... -rm -rf thumb/*.{jpg,JPG} 变量 赋值 Simple assignment (:=) We can assign values (RHS) to variables (LHS) with this operator, for example: CC := gcc. With simple assignment (:=), the value is expanded and stored to all occurrences in the Makefile when its first definition is found. ...

July 27, 2018 · datewu

事件驱动

在shell脚本里使用mkfifo命令创建named pipes可以实现简单的事件驱动, 避免poll(轮询)带来的时延(not real-time)和资源消耗的问题。 mkfifo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ❯ man mkfifo | head -n 12 MKFIFO(1) General Commands Manual MKFIFO(1) NAME mkfifo – make fifos SYNOPSIS mkfifo [-m mode] fifo_name ... DESCRIPTION mkfifo creates the fifos requested, in the order specified. By default, the resulting fifos have mode 0666 (rw-rw-rw-), limited by the current umask(2). ~ consumer 消费者以blocked的状态监听事件的发生,然后handle: ...

June 27, 2018 · datewu

vim配置

使用vim有7,8年了,整理一下自己用到的vim配置,方便自查。 vim basic 基本设置,语法高亮,行号,等等: 1 2 3 4 execute pathogen#infect() syntax on filetype plugin indent on set number paste 复制粘贴: disable auto indent 1 set paste Notice the “– INSERT (paste) –” at the bottom of the Vim window. fold 代码折叠: Folding wiki 1 2 " close zc " open zo font 安装字体: install powerline font 1 2 3 wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf open . # double click the otf file you've just downloaded. kubectl eidt issue 使用kubectl edit时,当yaml文件的annotation行过大会报错: ...

May 14, 2018 · datewu

配置vscode eslint

离职一段时间了,需要自己写点前端代码。 奈何vim的js插件对jsx的支持不太友好,所以转向vscode写点jsx。 写了些react app代码后,IDE到处飘红色的波浪线〰️〰️〰️,很是恼人。 全局配置react eslint好多了, 记录下配置的过程备查。 配置 基本上是用了airbnb的配置: 具体的步骤很简单,两步就好了: npm安装eslint和要用到plugin; 根据需求配置全局的eslintrc文件 plugin 1 2 3 4 5 6 7 8 9 10 11 12 13 npm install -g jshint npm install -g eslint eslint-config-airbnb-base eslint-plugin-import vi .eslintrc.js ls -alh /usr/local/bin/npm ls /usr/local/lib/node_modules/eslint-config-airbnb-base npm link eslint-config-airbnb-base ls node_modules npm link eslint-plugin-import npm i -g eslint-plugin-react npm i -g eslint-plugin-jsx-a11y npm link eslint-plugin-jsx-a11y eslint-plugin-react vi .eslintrc.js .elinttc.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // ~/.eslintrc.js module.exports = { parser: "babel-eslint", "plugins": ["react"], "extends": [ "airbnb-base", "eslint:recommended", "plugin:react/recommended", ], "rules": { // "no-unused-vars":0, "no-console": 'off', "max-len": [1,120,2,{ignoreComments: true}] // "prop-types": [2] }, "env": { "browser": true, "node": true, "jasmine": true } }; 参考 react eslint webpack babel ...

April 16, 2018 · datewu

分页打印日志

默认配置命令git log会在新的窗口打印日志内容,需要敲一下键盘q 才能返回当前目录,不方便连续查看: 1 2 3 4 5 6 7 8 9 10 11 12 ➜ lgthw_orign git:(otherbranch) git log --oneline --decorate --all --graph ## NOTE content below will be displayed on new window/buff * 40303b7 (HEAD -> otherbranch) thirdcommit | * 3e6e2f7 (master) secondcommit |/ * f40475e (tag: firstcommittag) firstcommit (END) ## press `q` to exist ➜ lgthw_orign git:(master) git log --no-pager fatal: unrecognized argument: --no-pager 可以把默认的分页改为inline模式,可以更快的查看连续的日志: ...

April 14, 2018 · datewu

init优先级

假设一个golang项目的三个源文件a.go,b.go, c.go,都定义了function inint(){}函数, 其中c.go文件初始化了一个全局变量globalVar,同时a.go 或者b.go的init func 引用了这个全局变量globalVar。 那么这个时候就会出现一个问题,在a.go和 b.go的init func中 globalVar的引用是空值。 示例 文件结构 1 2 3 4 5 6 7 8 9 ❯ tree . ├── a.go ├── b.go ├── c.go ├── go.mod └── main.go 0 directories, 5 files 源代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 // file `a.go` package main import "fmt" func init() { // globalVar is empty fmt.Println("globalVar in a.go:", globalVar) } // file `b.go` package main import "fmt" func init() { // globalVar is empty fmt.Println("globalVar in b.go:", globalVar) } // file `c.go` package main import ( "fmt" "time" ) func init() { globalVar = initVar() fmt.Println("globalVar in c.go:", globalVar) } func initVar() string { time.Sleep(20 * time.Millisecond) return "late is better than never" } // file `main.go` package main import "fmt" var globalVar = "" func main() { fmt.Println("vim-go") } result 1 2 3 4 5 6 ❯ go build -o demo ❯ ./demo globalVar in a.go: globalVar in b.go: globalVar in c.go: late is better than never vim-go 解决办法 简单的解决办法可以是重命名c.go为0a.go保证0a.go中的init最早执行完成, ...

January 23, 2018 · datewu

脚本注入

使用opkg安装软件时,常常需要对候软件包进行初始化或者自定义化操作,这种开发需求一般写给shell脚本就可以对付了。 现在的问题是当这些脚本多了之后,原作者也不愿意修改安装包,我们怎么分发这些自定义的脚本,能不能把自定义的这些脚本编译到opkg包里? 位置 把本地的shell脚本放在openwert 仓库的这个目录,编译openwrt的时候就会被打包到对应opkg二进制文件中: 1 2 /barrier_breaker/package/package-abc # package makefile文件所在 package-abc/files # shell脚本放置目录 步骤 修改Makefile 在package目录下任意找一个package目录,比如chinadns, 然后修改Makefile文件。 在install语句后添加 : 1 $(INSTALL_BIN) ./files/your_script.sh $(1)/etc/config/your_script.sh 放置脚本 将脚本your_script.sh放置在files目录下; 选择opkg 在make menuconfig 图像界面中选择修改过的包(chinadns) 附 package 和注入文件相关的部分makefile代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ## include $(TOPDIR)/rules.mk PKG_NAME:=xxx PKG_RELEASE:=1 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=https://github.com/xxx/releases/download/v$(PKG_VERSION) PKG_MD5SUM:=f772a750580243cfxcsfd2xc39d7b9171b1 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) include $(INCLUDE_DIR)/package.mk ## define Package/xxx SECTION:=net CATEGORY:=Network TITLE:=xxx endef ### define Package/xxx/description button haha upgrade. endef ### #define Package/xxx/conffiles #/etc/config/system #/etc/hotplug.d/button/00-button #endef ### define Package/wps_button/install #$(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_BIN) ./files/xxx $(1)/etc/config/xxx #$(INSTALL_DATA) ./files/system.conf $(1)/etc/config/system endef ### $(eval $(call BuildPackage,xxx)) ##

June 18, 2015 · datewu

增加固件大小

很多路由器的flash容量只有4m大,所以绝大多数openwrt固件也是4m大小。 当我们手动改造路由器加大flash容量后,可以调整openwrt默认设置使得编译出来的factory可以有8m的大小, 从而安装更多的内置软件。 本文以wr703n路由器 为例子,简单介绍一下如何加大固件的容量,让我们预安装更多内置软件。 查看 1 2 3 # ./tools/firmware-utils/src/mktplinkfw.c fw_max_len为0xfc0000,16M flash fw_max_len为0x7c0000,8M flash 修改 1 2 3 # ./target/linux/ar71xx/image/Makefile # 将703n的4Mlzma改为8Mlzma或16Mlzma $(eval $(call SingleProfile,TPLINK-LZMA,$(fs_64kraw),TLWR703,tl-wr703n-v1,TL-WR703N,ttyATH0,115200,0x07030101,1,8Mlzma))

May 7, 2015 · datewu

刷机HG255d

交叉编译 在ubuntu下安装编译工具(gcc,xmllib,cmake, git 等); git克隆openwrt仓库:git clone git://git.openwrt.org/14.07/openwrt.git; 自定义kernel target: 源代码做两处修改 : 1 2 3 target/linux/ramips/image/Makefile /base-files/lib/ramips.sh target/linux/ramips/base-files/lib/preinit/06_set_iface_mac 在弹出的make menuconfig 图像界面中选择cpu型号; 打开vpn开始编译固件。 结果 交叉编译完成后,根据上一步选择的安装包的多少,bin目录下会生成对应的opkg包,和固件: factory文件,可以称作底包; sysupgrade文件,可以称作升级包; web/uboot烧录刷机 接通路由器电源,按住WPS按钮不放,然后按电源键开机, power LED快闪即松开WPS键,此时路由器已加入升级模式; 访问路由器web地址(如:http://192.168.1.1), 按照web界面提示选取factory文件完成固件烧录刷机; ssh/ftp 烧录 可以直接执行sysupgrade命令烧录估计: 1 2 3 4 5 ssh-keygen -f "/home/openwrt-qqm/.ssh/known_hosts" -R 192.168.1.1 #可以省略此条命令 scp xxxxxx-squashfs-sysupgrade.bin [email protected]:/tmp/ ssh [email protected] cd /tmp/ sysupgrade -n xxxxxxxxxxx-sysupgrade.bin opkg 烧录完系统固件后,可以使用opkg安装软件包,比如china-dns, shadowsocks, openvpn,等等。

March 17, 2015 · datewu

Google Domain

刚刚在google+上看到google 推出了自己的域名注册服务,截两张图,纪念下。 google推广自己的 com.google 域名注册托管服务,把首页变成了镜像模式 搜索正常,不过搜索结果也使用了镜像效果:

June 17, 2014 · datewu