Archive

Archive for March, 2009

cmake常见问题

March 31st, 2009 No comments

CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,
可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。
文件 CMakeLists.txt 需要手工编写,也可以通过编写脚本进行半自动的生成。CMake 提供了比 autoconfig 更简洁的语法.

之前想用automake autoconf来管理项目,但是看了一天也没有搞明白是怎么回事。最后只好投奔cmake.

经过一天的努力终于把项目从Makefile移植到cmake. 在移植的过程中遇到了些问题,简单记录如下.

网上关于cmake的的资料比较少,官方网站上的资料也特别少,有好多看了还看不懂, 哎。

从网上找到一篇“Cmake Practice(cmake 实践)”国人Cjacker写的。可以从这不下载 http://www.scribd.com/doc/13774019/Cmake-Practicecmake-

1. 在cmake中通过EXEC_PROGRAM来调用命令行来取得一些参数. 比如你要想通过取得gtkmm一些头部文件和库的路径。
[code lang="C"]
pkg-config gtkmm --cflags --libs
[/code]

Use the EXEC_PROGRAM command and then use the CACHE option of the SET
command to save the output to a variable like GTK_PKG_FLAGS.  Then use
the SET command to add the value.  Something like this:

[code lang="C"]
IF(NOT GTK_PKG_FLAGS)
EXEC_PROGRAM(pkg-config ARGS --cflags --libs gtkmm
OUTPUT_VARIABLE GTK_PKG_FLAGS)
SET(GTK_PKG_FLAGS "${GTK_PKG_FLAGS}" CACHE STRING "GTK Flags")
ENDIF(NOT GTK_PKG_FLAGS)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GTK_PKG_FLAGS}")
[/code]

2. 通过execute_process来实现文件, 目录的拷贝
[code lang="C"]
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/path/to/www
${CMAKE_BINARY_DIR}/path/to/www)
[/code]

PS: EXECUTE_PROCESS 和 EXEC_PROGRAM 基本相同,  EXECUTE_PROCESS是用来替代EXEC_PROGRAM的。
参见:  http://www.cmake.org/pipermail/cmake/2006-February/008250.html

参考:  http://www.cmake.org/pipermail/cmake/2009-March/028299.html

3. 常用的几个内置变量

通过set语句可以自定义变量,然而,CMake还包含大量的内置变量,这些变量和自定义变量的用法没有区别,下面就列出一些常用的变量:

* CMAKE_C_COMPILER

指定C编译器,通常,CMake运行时能够自动检测C语言编译器。进行嵌入式系统开发时,通常需要设置此变量,指定交叉编译器。
* CMAKE_CXX_COMPILER

指定C++编译器
* CMAKE_C_FLAGS

指定编译C文件时编译选项,比如-g指定产生调试信息。也可以通过add_definitions命令添加编译选项。
* EXECUTABLE_OUTPUT_PATH

指定可执行文件存放的路径。
* LIBRARY_OUTPUT_PATH

指定库文件放置的路径

4. 常用的命令

除了内置变量,我们还可以通过命令来修改编译选项,现将一些常用的命令列出来:

* include_directories

指定头文件的搜索路径,相当于指定gcc编译器的-I参数
* link_directories

动态链接库或静态链接库的搜索路径,相当于指>定gcc的-L参数
* add_subdirectory

包含子目录,当工程包含多个子目录时,此命令有用
* add_definitions

添加编译参数,比如add_definitions(-DDEBUG)将在gcc命令行添加DEBUG宏定义
* add_executable

编译可执行程序
* target_link_libraries

指定链接库,相同于指定-l参数

* AUX_SOURCE_DIRECTORY 将指定目录中的源文件名称赋值给变量DIR_SRCS

AUX_SOURCE_DIRECTORY(. DIR_SECS)

把当前目录下面的所有源文件名称赋值给变量DIR_SRCS

Cmake Practice(cmake 实践)   http://www.scribd.com/full/13774019?access_key=key-hkil010h7nnglwgk8x5

Links:
1. http://www.cmake.org/pipermail/cmake/2005-January/006051.html
2.  CMake Useful Variables http://www.vtk.org/Wiki/CMake_Useful_Variables
3.  http://www.cmake.org/pipermail/cmake/2009-March/028299.html
4.  http://www.scribd.com/doc/13774019/Cmake-Practicecmake-

Categories: Tech.Notes Tags: , , , ,

Git info like svn info

March 26th, 2009 No comments

之前用 SVN 的时候经常用 svn info 反应当前svn的基本信息,但是在Git 中没有类似的功能。

比如查看当前版本的一些基本信息:

[code lang="c"]
git remote -v
git branch -r
git branch
[/code]

每次敲很多的命令是很麻烦的,但是Hack里面力量是无限的,在 git-info.txt 有些简单的命令组合可以完成我们的需求。

运行的效果如下:

[code lang="c"]

== Remote URL: origin   git://git.kernel.org/pub/scm/linux/kernel/git/rdunlap/linux-docs/.git
== Remote Branches:
origin/HEAD
origin/doc-subdirs
origin/master

== Local Branches:
* doc-subdirs

== Configuration (.git/config)
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git://git.kernel.org/pub/scm/linux/kernel/git/rdunlap/linux-docs/.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "doc-subdirs"]
remote = origin
merge = refs/heads/doc-subdirs

== Most Recent Commit
commit 31c00fc15ebd35c1647775dbfc167a15d46657fd
Author: Randy Dunlap
Date:   Thu Nov 13 21:33:24 2008 +0000

Create/use more directory structure in the Documentation/ tree.

Create Documentation/blockdev/ sub-directory and populate it.
Populate the Documentation/serial/ sub-directory.
Move MSI-HOWTO.txt to Documentation/PCI/.
Move ioctl-number.txt to Documentation/ioctl/.
Update all relevant 00-INDEX files.
Update all relevant Kconfig files and source files.

Signed-off-by: Randy Dunlap

Type 'git log' for more commits, or 'git show' for full commit details.

[/code]

Git-info 脚本如下:

[code lang="c"]

#!/bin/bash

# author: Duane Johnson
# email: duane.johnson@gmail.com
# date: 2008 Jun 12
# license: MIT
#
# Based on discussion at http://kerneltrap.org/mailarchive/git/2007/11/12/406496

pushd . >/dev/null

# Find base of git directory
while [ ! -d .git ] && [ ! `pwd` = "/" ]; do cd ..; done

# Show various information about this git directory
if [ -d .git ]; then
echo "== Remote URL: `git remote -v`"

echo "== Remote Branches: "
git branch -r
echo

echo "== Local Branches:"
git branch
echo

echo "== Configuration (.git/config)"
cat .git/config
echo

echo "== Most Recent Commit"
git log --max-count=1
echo

echo "Type 'git log' for more commits, or 'git show' for full commit details."
else
echo "Not a git repository."
fi

popd >/dev/null

[/code]

这里有些有关git info讨论话题。

Links:

1. Git Info (kinda like ’svn info’)  http://blog.inquirylabs.com/2008/06/12/git-info-kinda-like-svn-info/

2. git info 脚步 http://blog.inquirylabs.com/wp-content/uploads/2008/06/git-info.txt

3. KernelTrap 有关git info讨论的话题

Categories: Tech.Notes Tags: ,

simple logging in C programming

March 18th, 2009 No comments
近来在调试C的时候,经常要打印些调试信息,如果都统一使用printf来调试,有些太累了, 如果调试完成还要去掉, 就写了个简单的宏来进行定义, 主要的代码如下:
[code lang="c"]
#include

#undef PDEBUG             /* undef it, just in case */
#ifdef SCULL_DEBUG
#   define PDEBUG(fmt, args...) printf("[%s-%s:%d]-[Debug] " fmt "\n", __FILE__, __func__, __LINE__, ## args)
#else
#   define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif

int main(void)
{
char * val = "wifihack.net";
PDEBUG("Hello, %s", val);
return 0;
}
[/code]

为了减少修改头文件,我们可以通过Makefile来控制Debug信息的输出:

[code lang="c"]
# Comment/uncomment the following line to disable/enable debugging
DEBUG = y

# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif

CFLAGS += $(DEBFLAGS)

ALL:
gcc $(CFLAGS) -o hello hello.c
[/code]

参考: http://www.makelinux.net/ldd3/chp-4-sect-2.shtml


Bian Jiang

Categories: Tech.Notes Tags: , , ,

对位运算

March 12th, 2009 No comments

近来在做驱动方面的开发, 结果发现对位操作反应比较迟钝, 就从网上搜了些有关位操作的说明复习一下.
[sourcecode language='c']
if (set) {
/* shift 位设置为1 */
val |= (1 << shift);
}
else{
/* shift 位清零 */
val &= ~(1 << shift);
}

[/sourcecode]
Set a bit (where n is the bit number, and 0 is the least significant bit):
[sourcecode language='c']
unsigned char a |= (1 << n);
[/sourcecode]
Clear a bit:
[sourcecode language='c']
unsigned char b &= ~(1 << n);
[/sourcecode]
Toggle a bit: n位的值取反
[sourcecode language='c']
unsigned char c ^= (1 << n);
[/sourcecode]
Test a bit:
[sourcecode language='c']
unsigned char e = d & (1 << n); //d has the byte value.
[/sourcecode]
还有一个对奇偶性进行判断
[sourcecode language='c']
a&1 = 0 // 偶数
a&1 = 1 // 奇数
[/sourcecode]
参考:
1.  http://en.wikipedia.org/wiki/Bit_manipulation

2. 这里面将的比较全也比较详细, 有兴趣的可以看看 http://graphics.stanford.edu/~seander/bithacks.html


Bian Jiang
Blog:  http://www.wifihack.net/

Categories: Tech.Notes Tags: , ,