Real-time linting of C code - c

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.
What is the best way to get feedback about the quality of my C code? 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.
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
Navigate to this user's fork of SublimeLinter and proceed to download the 'c.py' module from the modules folder
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)
Restart 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.

Take a look at the Clang Static Analyzer and Gimpel's PC-lint and FlexeLint

Also, please have a look at cppcheck

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. All of these are plugins for SublimeLinter. I recommend using Package Control as a package manager for Sublime Text (as do the plugin authors).
First, install Sublime Linter if you don't already have it (it's a pretty popular linting framework for multiple languages). It is most easily installed through package control, as the authors recommend, but more info is on the github site. Once Sublime Linter is installed, there are two to four different accessory packages that now exist for linting C and C++ code.
Two of these use the C/C++ compiler itself for checking; these are SublimeLinter-gcc and SublimeLinter-clang. Both can be installed via Package Control, and provide SublimeLinter with an interface to the relevant underlying compiler. 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.
The other two are interfaces to cpplint and cppcheck, respectively. These two are also available on Package Control, and despite the names it seems that both will lint C and C++ code.
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.

Passing it through your compiler with full warnings is a pretty good basic 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.

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? e.g., Eclipse. I dev in Eclipse/Java and it's pretty aggressive regarding errors/warnings, certainly more than I've seen a compiler be.

Related

How to setup a mixed C / Z80-assembler toolchain in Visual Studio Code

I want to use Visual Studio Code for writing code for my homebrew Z80 Computer.
I use z88dk and its tools to compile C, assemble the Z80 asm files etc. I will write parts in assembler and other parts in C. I also need to edit .m4 files. Would be nice if these file types would have syntax coloring.
I am totally new to vscode and I have seen some languages for the z80 but I have no idea how to continue from there - or if these will even work at all.
I don't want to have to write a language extension because that looks like just too much work - from what I understand. I am also not a typescript/javascript developer.
Is there a tutorial out there on how to set this up, or any other helpful resources?
I'm looking this up as I type so it may not be sufficiently complete to warrant a tick but I hope it'll be useful.
Per Adding Language Colorization you can use TextMate-format colourisation rules so jumping through those rules with something like this 'Z80 Asm' rule set should get you syntax highlighting. It doesn't look like it's a lot of work given that somebody else has already written the highlighting rules.
Microsoft's documentation on Tasks also guides you through using tasks.json to incorporate into your workspace "a task runner like gulp or grunt or any command line tool like a compiler or linter". So you should be able to create a z88dk running task fairly easily.

Cloud9 IDE and C Programming

How can I write a C program in the IDE, cloud9? Whenever I start to write its showing errors, I even tried to change the file extension to ".C", but it doesn't work.
Does the cloud9 IDE support C programming?
Short answer
Yes Cloud9 does support C and C++ programming
Long Answer
Cloud9 has a varying level of support for different programming languages and frameworks.
Cloud9's ACE editor currently supports highlighting for about 60 programming languages including C/C++. The list is growing and you can get the full list from the "View -> Syntax" menu.
The editor has extensive support for JavaScript with error highlighting and code completion. It also has live syntax error checks for CSS, CoffeeScript, PHP, Python, JSON and Lua.
The run menu supports running
JavaScript (node.js)
Python
Ruby
PHP (using apache)
Each project is a unix sandbox with the standard tools like bash, make, gcc, vim, java SDK or perl installed. The terminal in Cloud9 is a full xterm compatible terminal emulator and you can use this to compile and run your C programs.
Cloud9 also has a package manager called c9pm which currently has about 120 packages. These packages include e.g. different versions of python and ruby, mono, lua or groovy.
In case you still need additional tools or libraries you can always download them to your workspace and compile and install them there.
As you see it is hard to draw the line of what is actually supported but usually you will have at least syntax highlighting in the editor and running from the console.
It claims support on the web page for "Javascript and Node.js applications as well as HTML, CSS, PHP, Java, Ruby and 23 other languages".
Yet it's very light on regarding details of those other languages. It does state that the editor, ACE, supports many different languages although whether that's natural languages or computer languages it doesn't specify. Additionally, an editor supporting a language is not the same as an IDE supporting it. It makes little sense having syntax coloring for C but no compiler.
Certainly C++ is possible as shown here but the fact that you simply use the editor then switch to a terminal session to compile the code seems to reduce the attractiveness of the so-called "IDE". It doesn't seem to give you anything you can't get from vim/gcc under Linux, or MinGW under Windows. It certainly isn't integrated in the sense that Visual Studio (even Express) or Code::Blocks is.
Regardless, the proper place for this sort of query is most likely on their support page here. I suspect they'd know more about what's supported than anyone here on SO.
Though I wouldn't be hopeful for a response. There's a very similar question asked back on Feb 28 which has still had no response. You have to wonder whether you want to entrust your precious code to a company that takes over four months to answer a simple support query.
In fact, to be brutally honest, I'm not sure I see the value proposition in using such a service. Given that you can get very good local IDEs already (for zero cost), the only possible advantage that springs to mind is collaboration. And, if you're working in a shop that has geographically dispersed developers, they'd probably have a fit if you told them you wanted to store your code where someone outside the company can get at it :-)
I'm not trying to turn you off the idea, just stating that (based on my experience), it doesn't seem as good as a substantial number of other solutions.
Cloud 9 does support C ( and C++ ) using gcc and g++
gcc --version
(GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
You can use their integrated editor, but the run button still acts like it trying to execute Javascript, which is the default, and seems to be their the target audience.
They have a fine C hello world example -- http://support.cloud9ide.com/entries/23541348-C-hello-Cloud9
Typically I just maximize the terminal, and run vim ( they have 7.2 installed ), and it works just like any passable unix terminal. They also have gdb installed, if you need a debugger.
There are comparisons against installed compiler solutions, but that isn't what Cloud9 is competing against. Their own description is Google Docs for code, and that seems to pretty fairly accurate. It certainly isn't all things to all people, but it is a pretty amazing service.
With Cloud9 I can write code on my Chromebook without having to install a bunch of stuff to almost make Linux run on it. This would be a good solution for students, since it would allow them to work in a computer lab without needing any software installed. I have found that using github and c9, I can easily switch back and forth between c9 while I am remote, and a local setup when I am on a real computer.

How to build executable from c written source files?

There is few files with .c anf .h extensions (cmdline.c cmdline.h core.c core.h and so on) in src directory, also there is one file "MakeFile" without extension. Is there any possibility to build these source files into some executable file on Windows 7 (64bits) ? I think i need to download compilers for C or some sdks right?
Yes.
You need to:
download and install a C/C++ compiler (I recommend TDragon's distribution of MinGW ),
add the compiler to your PATH (the installer can do it for you most of the cases); verify it's done by opening cmd.exe and typing gcc -v and mingw32-make -v, both should give you half a screenful of version information if your path is set correctly,
via cmd.exe, navigate to the folder in which the Makefile resides and call mingw32-make.
From now on everything should compile automatically. If it doesn't, post the errors.
Update:
First of all, it'd be useful for you to get the MSys package. Install it and you'll have a more recent version of make (use it instead of mingw32-make from now on).
About the CreateProcess bug, it has to do with the system PATH variable being too long. You'd need to do something like this:
open cmd
execute set PATH=c:/mingw32/bin;c:/msys/1.0/bin (change the paths here to reflect your own installation if it's different)
then as before: navigate to your project's directory, run make. Everything should be smooth now if you're not missing any external libraries.
BTW- remember not to install MinGW or MSys in directories with spaces.
I am not a Windows Developer..
But still as per my knowledge. Visual Studio (i.e 2008, I guess) has the ability to read the Makefile.
Please have a look at it..and if needed change this makefile to their format..
There are many opensource product which are platform independent..and they get compiled on both OS with the just Makefile they provided.
Or else use 'cygwin'
Developer C++ works in windows but it is actually GCC code bought into Windows, Is anyone familiar about the procedure they used to convert the linux ( .sh) to executables ??
I think i need to download compilers for C or some sdks right?
A compiler certainly, but what additional libraries you may need will depend entirely on the code itself. A successful build may also depend on the intended target of the original code and makefile. The makefile may be a GNU makefile, but there are other similar but incompatible make utilities such as Borland Make and MS NMake. If it is a simple build, you may be able to avoid the makefile issue altogether and use the project management provided by an IDE such as Visual C++ 2010 Express.
If you do not know what this code is or what it does and what it needs to build, you are taking a risk building it at all. Maybe you should post a link to the original source so that you can get more specific advice on how to build it.
[EDIT]
Ok, now looking at the code you are attempting to build, it is a very simple build, so if you wanted to avoid using GNU make, then you could just add all the *.c files in the src folder to a project in your IDE and build it.
However there is one serious gotcha, it uses the BSD sockets API and Linux system headers. You will need to first port the code to Windows APIs such as WinSock (very similar to BSD Sockets), or build it under Cygwin (a sledgehammer for a nut somewhat). There may be other Linux dependencies that need sorting, I have not looked in detail, but it looks fairly simple. That said, if you did not have the first clue regarding compiling this stuff, then perhaps this is not a task you could do?
Of course compiling the code may only be half teh problem, if it was designed to run on Linux, there may be run-time dependencies that prevent it running on Windows. Again I have not looked in detail.
Also looking at the code, I would suggest some caution, this may not be the best quality code. That may be unfair, but one obvious flaw and an indication if inexperience is the lack of include guards in the headers.

Refactoring C/C++ in Vim (e.g. method extraction like in Eclipse)

Are there any plugins or built-in methods in Vim for performing refactoring on C or C++ code, something like the refactoring tools in Eclipse?
I'm especially keen on the extract method refactoring tool from Eclipse that will determine parameters from new methods and typically also guess a variable to use as the return value.
No, although Vim is a good environment for editing, and can be customised in a lot of ways (code folding, syntax colouring, macro expansion etc.) most of these are done on the syntax level, rather than the semantic level. Even the code folding just matches up opposing braces.
To do a proper refactoring, you have to have a lot of semantic knowledge about the AST, what variables are declared in which scope, and so on. IDEs like Eclipse build up a cache of the variables defined in each lexical scope, so that they can quickly refer back to where they are used in terms of determining what to rename and where.
That's not to say that you can't do some things syntactically; after all, one can just take out a block of code and put it into a separate function easily enough. You might even be able to guess at some parameters (e.g. find a list of the variables, find out which ones have local declarations, remove them and what's left are your parameters. But Eclipse also does other things—like figuring out whether any variables are modified in the function, and ensuring they're passed back by the return value. It also checks for any thrown exceptions, and add them to the list.
The net effect is that whilst you may be able to approximate some of these in Vim, you really aren't going to be able to get this working in a Vim-only enviornment. You could either use a Vim-like keybinding in Eclipse proper, or look at eclim. From the home page:
The primary goal of eclim is to bring
Eclipse functionality to the Vim
editor. The initial goal was to
provide Eclipse’s java functionality
in vim, but support for various other
languages (c/c++, php, python, ruby,
css, html, xml, etc.) have been added
and several more are planned.
Eclim is less of an application and
more of an integration of two great
projects. The first, Vim, is arguably
one of the best text editors in
existence. The second, Eclipse,
provides many great tools for
development in various languages. Each
provides many features that can
increase developer productivity, but
both still leave something to be
desired. Vim lacks native Java support
and many of the advanced features
available in Eclipse. Eclipse, on the
other hand, still requires the use of
the mouse for many things, and when
compared to Vim, provides a less than
ideal interface for editing text.
That is where eclim comes into play.
Instead of trying to write an IDE in
Vim or a Vim editor in Eclipse, eclim
provides an Eclipse plug-in that
exposes Eclipse features through a
server interface, and a set of Vim
plug-ins that communicate with Eclipse
over that interface.
This not only gives an Eclipse-like environment, it is Eclipse. But you still get the navigation and text editing features of vim. It sounds like this might suit your needs, although the documentation on refactoring support doesn't indicate that it provides an extract method functionality.
I've written a generic refactoring plugin. C++ is one of the primary languages handled (as it's my primary language at work). Method extraction is supported.
For C++, the plugin is able (thanks to ctags) to deduce most (but unfortunately not always all -- thanks to ctags...) of the variables coming in and out of the extracted function.
I still have to write a little dialog box to select how the in/out variables shall be exchanged (const ref, rvalue ref, copy, pointer, tuples, struct, and so on) (BTW, help is welcome as GUIs are not my thing ^^').
After searching high and low for vim with refactoring for C++, this is the best solution I have come up with.
Visual Studio 2013 and higher - Great IDE for C++ development and debugging, but does not have sufficient refactoring or vim
Install the vsvim plugin - Now, you have all the vim navigation, searching, string replacement, etc. (Some advanced features are not supported)
Install Resharp C++ - Great for refactoring at the cost of speed. It makes it a bit clunky, but if you want to infer auto generate methods/variables, rename local/global/method variables or functions, Search for usages in the solution, and more features, makes it extremely worth it.
This is the best combination I found for C++ development, debugging and refactoring. Makes me at least 3x - 5x faster. Hope it helps you as well.

Eclipse CDT and unknown tags

Currently we are looking at moving our development environment from CodeWright (which has long since been abandonned by Borland) and moving to Eclipse. We are using Eclipse for Perl, PHP and some Windows C++ development with much success.
The issue comes up however with our embedded environment which uses - in the makefile - some defines that Excel does not seem to understand. Here's an example (whith values changed to protect the guilty):
F_FOO unsigned char foo(ptr *this)
{
}
where F_FOO is defined in the makefile as
F_FOO=#foobar
Eclipse will highlight the F_FOO and flag it as a syntax error. That causes all kinds of grief with traceability.
To some exctent this also happens with some code in Builder C++ where Eclipse does not know how to handle the keyword __fastcall.
Any help on this (I know we can't be the only ones running into this issue) would be highly appreciated.
We tried the #defines in the header directive but because of the size of the project as well as the sheer number of files, that turned out to be a not too practical approach.
Tooling around Eclipse we found a solution that works for us and it is detailed below:
Right click on the project in question.
Select Properties at the bottom of the resulting menu.
Expand C/C++ Geneeral
Select Paths and Symbols
Under Symbols add the offending symbol (leave Value blank)
That did the trick for us. We have now restored full traceability for our software.
You could make a header file that is included with #defines for the things Eclipse doesn't know about. Then as part of your build have it use an empty version of that header file so it doesn't actually use those defines.

Resources