简体   繁体   English

C代码的实时linting

[英]Real-time linting of C code

I am looking for something similar to the JavaScript linting tools JSHint or JSLint for C. My text editor (Sublime Text 2) has a JSHint pluggin that gives me real time feedback to my JavaScript code. 我正在寻找类似于JavaScript linting工具JSHint或JSLint for C的东西。我的文本编辑器(Sublime Text 2)有一个JSHint插件,可以为我的JavaScript代码提供实时反馈。

What is the best way to get feedback about the quality of my C code? 获得有关C代码质量的反馈的最佳方法是什么? Are there any tools that could give me real time linting? 有没有可以让我实时掉毛的工具?

I've concocted a way to drop some user-made linters written for SublimeText 2 into the mix to get the linting working with SublimeLinter and ANSI C. Also note, this is a slightly 'hacky' way of getting it to work. 我已经编造了一种方法,将为SublimeText 2编写的一些用户自制的连接器放入混合中,以使llint与SublimeLinter和ANSI C一起使用。另外请注意,这是让它工作的一种“hacky”方式。

  • You must have clang installed (for OS X you can use Apple's command line tools to install clang/the LLVM compiler, which requires only a developer account, which is free), you also must have SublimeLinter installed in Sublime Text 2 必须安装clang(对于OS X,您可以使用Apple的命令行工具来安装clang / LLVM编译器,这只需要一个开发人员帐户,这是免费的),您还必须在Sublime Text 2中安装SublimeLinter

  • Navigate to this user's fork of SublimeLinter and proceed to download the 'c.py' module from the modules folder 导航到此用户的 SublimeLinter 分支 ,然后从modules文件夹下载'c.py'模块

  • Copy this module into SublimeLinter's working modules directory located under **your SublimeText 2 data directory**\\Packages\\SublimeLinter\\sublimelinter\\modules\\ ( see this for further information on the data directory) 将此模块复制到SublimeLinter的工作模块目录中,该目录位于**your SublimeText 2 data directory**\\Packages\\SublimeLinter\\sublimelinter\\modules\\ (有关数据目录的更多信息, 请参阅 **your SublimeText 2 data directory**\\Packages\\SublimeLinter\\sublimelinter\\modules\\

  • Restart Sublime Text 2 重启Sublime Text 2



†Be sure that the current language in the lower right-hand corner of the window is set to 'C', not 'C++', 'Python', ect. †确保窗口右下角的当前语言设置为“C”,而不是“C ++”,“Python”等。

另外,请看一下cppcheck

Passing it through your compiler with full warnings is a pretty good basic lint. 通过完整警告将其传递给编译器是一个非常好的基本lint。 It will catch things like typoed variables and such. 它会捕获诸如拼写变量之类的内容。 clang with optimizations off is fast enough to use as the basis of a real-time plugin, but I'm not aware of such for sublime text. clang with optimizations off足够快,可以作为实时插件的基础,但我不知道这样的sublime文本。

You have enough rep that I feel this might be too obvious of a suggestion, but it sounds like you would basically benefit from an IDE? 你有足够的代表,我觉得这可能是一个太明显的建议,但听起来你基本上可以从IDE中受益? eg, Eclipse. 例如,Eclipse。 I dev in Eclipse/Java and it's pretty aggressive regarding errors/warnings, certainly more than I've seen a compiler be. 我开发Eclipse / Java并且它在错误/警告方面非常积极,当然比我见过的编译器更多。

Since this question was asked and answered, there are now some options for C/C++ linting in Sublime that are a bit more user friendly than the accepted answer. 由于这个问题被提出并得到了解答,现在Sublime中有一些C/C++ linting的选项比接受的答案更加用户友好。 All of these are plugins for SublimeLinter. 所有这些都是SublimeLinter的插件。 I recommend using Package Control as a package manager for Sublime Text (as do the plugin authors). 我建议使用Package Control作为Sublime Text的包管理器(与插件作者一样)。

First, install Sublime Linter if you don't already have it (it's a pretty popular linting framework for multiple languages). 首先,安装Sublime Linter(如果你还没有它)(它是一种非常流行的多语言linting框架)。 It is most easily installed through package control, as the authors recommend, but more info is on the github site. 如作者所建议的那样,它最容易通过包控制安装,但github网站上有更多信息。 Once Sublime Linter is installed, there are two to four different accessory packages that now exist for linting C and C++ code. 一旦安装了Sublime Linter,就会有两到四个不同的附件包,现在可用于绘制CC++代码。

Two of these use the C/C++ compiler itself for checking; 其中两个使用C/C++编译器本身进行检查; these are SublimeLinter-gcc and SublimeLinter-clang . 这些是SublimeLinter-gccSublimeLinter-clang Both can be installed via Package Control, and provide SublimeLinter with an interface to the relevant underlying compiler. 两者都可以通过Package Control安装,并为SublimeLinter提供相关底层编译器的接口。 The gcc package makes it easy to specify which compiler executable you want to use, in case you might want to check code for cross-compilation. gcc包使您可以轻松指定要使用的编译器可执行文件,以防您需要检查代码以进行交叉编译。

The other two are interfaces to cpplint and cppcheck , respectively. 另外两个分别是cpplintcppcheck的接口。 These two are also available on Package Control, and despite the names it seems that both will lint C and C++ code. 这两个也可以在Package Control上使用,尽管名称似乎都是lint C和C ++代码。

Note that you probably only want one of these options enabled at a time, although the SublimeLinter setup allows you to have multiple options installed and only one enabled via the "linters": {...} option stanza. 请注意,您可能只希望一次启用其中一个选项,尽管SublimeLinter设置允许您安装多个选项,并且只有一个通过"linters": {...}选项节启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM