我使用emacs做一些编码和文本编辑.当我创建一个新的编码项目时,我只需创建一个新文件夹,并在其中添加源代码.
问题是,对于多文件夹,很难更改回顶部,并运行makefile.
有没有什么好方法可以像eclipse或其他IDE一样进行项目管理?
我知道你的问题.如果Makefile与源文件夹在同一个文件夹中,并且您在源缓冲区中,那么'compile'将正确构建.
但是如果您的源位于不同的文件夹中,则emacs无法找到Makefile.
一种解决方案是通过将'default-directory'变量设置为每个源文件中的文件变量来指定Makefile的位置.
您可以通过在文件顶部添加这样的行(并重新加载)来完成此操作.
// -*- mode: C++; default-directory: "c:/somewhere/yourmakefiledirectory/" -*-
Run Code Online (Sandbox Code Playgroud)
下面是;; 我的.emacs文件的编译部分.我使用CTRL + F7进行make,使用F7进行make clean.它将在当前目录中搜索,然后在...中依次查找名为"Makefile"的文件以运行make.
也不是F8将源窗口跳转到第一个错误而CTRL + F8会将您带到上一个错误.(顺便说一下,如果你认为这很棒,你应该看看我为GDB集成做了什么)...... :)
;; Compilation
(setq compilation-scroll-output 1) ;; automatically scroll the compilation windo
w
(setq compilation-window-height 10) ;; Set the compilation window height...
(setq compilation-finish-function ;; Auto-dismiss compilation buffer...
(lambda (buf str)
(if (string-match "exited abnormally" str)
(message "compilation errors, press F6 to visit")
; no errors, make the compilation window go away after 2.5 sec
(run-at-time 2.5 nil 'delete-windows-on buf)
(message "No compilation errors!"))))
(require 'cl) ; If you don't have it already
(defun* get-closest-pathname (&optional (file "Makefile"))
"This function walks up the current path until it finds Makefile and then retu
rns the path to it."
(let ((root (expand-file-name "/")))
(expand-file-name file
(loop
for d = default-directory then (expand-file-name ".." d)
if (file-exists-p (expand-file-name file d))
return d
if (equal d root)
return nil))))
(defun my-compile-func ()
"This function does a compile."
(interactive)
(compile (format "make -C %s" (file-name-directory (get-closest-pathname)))))
(defun my-compile-clean-func ()
"This function does a clean compile."
(interactive)
(compile (format "make -C %s clean" (file-name-directory (get-closest-pathname
)))))
(defun my-compile-package-func ()
"This function builds an Endura package."
(interactive)
(compile (format "make -C %s package" (file-name-directory (get-closest-pathna
me)))))
(global-set-key [f7] 'my-compile-clean-func)
(global-set-key [C-f7] 'my-compile-func)
(global-set-key [S-f7] 'my-compile-package-func)
(global-set-key [f8] 'next-error)
(global-set-key [C-f8] 'previous-error)
Run Code Online (Sandbox Code Playgroud)
只需M-x compile从根目录一次.这将创建一个*compilation*缓冲区,它将记住调用它的目录和参数.
然后当你想重新编译时,只需发布M-x recompile.这适用于任何地方.它会恢复原始*compilation*缓冲区并使用存储在该缓冲区中的目录来查找Makefile.
还有其他方法可以从项目的根目录外部发出编译,但我想我会指出这一点,因为它开箱即用,没有自定义.很多其他的反应使解决方案听起来比现在更复杂.
如果C-c C-f在编译缓冲区中键入,它将启用next-error-follow-minor-mode,这样当您在编译缓冲区的错误之间导航时,第二个窗口将在其原始源缓冲区中显示错误.
M-n并将M-p在编译缓冲区的错误之间移动.
如果您已经在源缓冲区中,并希望在那里导航错误,请键入M-g n或M-g p.
键入时M-x flymake-mode即时进行语法检查.它将以红色突出显示语法错误.将鼠标悬停在上方会显示错误消息.
要使flymake工作,您必须在makefile中添加check-syntax规则.
C++示例:
check-syntax:
g++ -o nul -S ${CXXFLAGS} ${CHK_SOURCES}
Run Code Online (Sandbox Code Playgroud)
此规则检查文件的语法,但不编译它,因此它很快.
我通常不再在emacs中编译,但为什么不能在缓冲区中运行shell只是为了运行make.将该shell保留在顶级目录中.
至于项目管理,您在寻找什么功能?
| 归档时间: |
|
| 查看次数: |
3813 次 |
| 最近记录: |