OSX上配置emacs的C/C++开发环境

1 篇文章 0 订阅

我安装的是emacs24,之前在linux上习惯了用emacs读写c/c++的代码,所以换用macbook pro之后也想用到emacs,特别是最近要学习一些开源的代码。

正常安装emacs24,然后配置cscope,至于cscope是什么可以去google一下。

配置cscope


使用brew安装cscope,brew安装的cscope默认没有xcscope脚本,需要从linux下复制一个过来放到~/.emacs.d/elpa/xcscope目录下,在配置文件中添加以下代码

(add-to-list 'load-path "~/.emacs.d/elpa/xcscope")
(require 'xcscope)
;; (add-hook 'c-mode-common-hook '(lambda() (require 'xcscope)
;; ))

(define-key global-map [(control f3)]  'cscope-set-initial-directory)
(define-key global-map [(control f4)]  'cscope-unset-initial-directory)
(define-key global-map [(control f5)]  'cscope-find-this-symbol)
(define-key global-map [(control f6)]  'cscope-find-global-definition)
(define-key global-map [(control f7)]  'cscope-find-global-definition-no-prompting)
(define-key global-map [(control f8)]  'cscope-pop-mark)
(define-key global-map [(control f9)]  'cscope-next-symbol)
(define-key global-map [(control f10)] 'cscope-next-file)
(define-key global-map [(control f11)] 'cscope-prev-symbol)
(define-key global-map [(control f12)] 'cscope-prev-file)
(define-key global-map [(meta f7)]     'cscope-find-called-functions)
(define-key global-map [(meta f8)]     'cscope-find-functions-calling-this-function)
(define-key global-map [(meta f9)]     'cscope-display-buffer)
(define-key global-map [(meta f10)]    'cscope-display-buffer-toggle)


通过brew安装的cscope也没有cscope-indexer脚本,从linux下复制一个吧。

现在使用cscope是会报错的,经过反复的尝试,发现跟cscope的安装目录有关系,cscope安装在/usr/local/bin/,所以在emacs中使用时经常会报错,说各种找不到,解决办法中最直接的就是在/usr/bin中建立软链接,这样在emacs中就能正常使用cscope了。


配置代码格式化工具astyle

有了代码阅读的工具还需要一个帮助我们自动格式化代码的工具,让我们的代码看上去整齐美观,之前在linux下常用的是astyle,的确非常好用,以至于我在vs中也用astyle格式化c/c++代码。

使用brew安装astyle,然后在emacs的配置文件中添加下述代码。

(defvar astyle-command-c "astyle --style=ansi --mode=c -s4 -S -Y -f -p -y -k3 -U -n -w -Y -c -xL")

;; (defvar astyle-command-java "astyle --style=java --mode=java -n")

(defun astyle-region (start end)
"Run astyle on region, formatting it in a pleasant way."
(interactive "r")
(save-excursion
(shell-command-on-region start end astyle-command-c nil t)
))

(defun astyle-buffer ()
"Run astyle on whole buffer, formatting it in a pleasant way."
(interactive)
(save-excursion
(astyle-region (point-min)(point-max))))

(add-hook 'c-mode-common-hook
'(lambda ()
(define-key c-mode-map "\C-cr" 'astyle-region)
(define-key c-mode-map "\C-cb" 'astyle-buffer)
(define-key c++-mode-map "\C-cr" 'astyle-region)
(define-key c++-mode-map "\C-cb" 'astyle-buffer)))

跟cscope一样,需要在/usr/bin下建立软链接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值