5 ms·
How to convince any C developer to dump gcc and use clang
- kvs 17y agoPart of being a C developer (or C++ template) developer is in fact understanding what the compiler spits out. So, this did't convine me enough because "two or more data types" made me skip right a head to the definition of Point. :-) My point is Clang is good but friendly error messages is not the only reason I would change to Clang.
- sad 17y agoBut it is a compelling reason. Imagine all of the brain cells you can reclaim by forfeiting that C++ error message intuition that you now have. First we need full C++ coverage in clang. Onward!
- jules 17y ago> Imagine all of the brain cells you can reclaim by forfeiting that C++ intuition that you now have. Orders of magnitute more than just for the error messages.
- pohl 17y agoWould you be interested in a compiler where the error messages are more difficult to decipher, then? That way you could feel even more like you're a developer.
- mrshoe 17y agoAn even better reason to use clang: it supports Apple's new syntax for blocks. I'm trying to think of a new project to write in C now just to take advantage of the simple anonymous functions and closures that are possible.
- alayne 17y agoProprietary extensions are one reason that gcc is so hard to unseat. You're always better off writing standard code if you want it to work in ten years.
- jws 17y agoHardly proprietary. Apple gave the "block" patches to the gcc team. Gcc isn't interested in them, they are holding off for some future C++ syntax, though I don't see how that helps C programmers. Meanwhile, I got seduced by gcc's nested functions which are not supported by clang. Apparently they are non-trivial in clang so won't appear soon if at all. Two incompatible mechanisms for function decomposition are annoying. ("blocks" does a lot more, but for the afflicted project I don't need that. Just nested functions would be spiffy, it saves the plethora of arguments being passed to each function if you keep them at file scope.)
- jrockway 17y agoI think he meant to say "non-standard".
- alayne 17y agoYes, nonstandard. I apologize for my sloppy wording. By all means play with closures. I don't mean to discourage the original poster from experimenting. I am just pointing out some irony in the context of gcc and clang. Clang is forced to implement nonstandard gcc extensions. I do not see this as a good thing for languages that are essential building blocks and which people have put a lot of work into standardizing. Software development doesn't need to be so ephemeral.
- deleted 17y ago[deleted]
- deleted 17y ago[deleted]
- CrLf 17y ago
- lzm 17y agoI've had 40% speed-ups on some math-heavy C code simply by switching from gcc to clang.
- deleted 17y ago[deleted]
- cperciva 17y agoWhich version of gcc? I had a 2x speedup a few years ago on math-heavy code (FFTs) by switching from gcc 4.2 to gcc 3.4 -- apparently there was a known issue in gcc's optimizer where it would flag too many values as "keep this in a register" and end up spending lots of time moving data to and from the "register overflow space" on the stack. (I don't know if this has been fixed -- as I said, it was a few years ago that I ran into this issue.)
- alecco 17y agoI second that on gcc version. On many math related code gcc-4.4 is amazing. Specially if vectorization and/or SIMD is possible.
- deleted 17y ago[deleted]
- viraptor 17y agoI'm not sure this screenshot is convincing. Do you see the last error message? In the second error message clang found that `horisontal` doesn't exist and suggested `horizontal`. That's fair enough. The last error message actually assumes the code said `horizontal`, which is just horribly wrong. I think that the compiler should never ever report an error on code that is not really there. What would happen if the programmer got a list of error and started fixing from the end? What if the guessed name was wrong? The programmer would spend time chasing some bug which doesn't exist outside of compiler's "that's what you probably meant" version of code.
- weaksauce 17y agothe struct point holds a def for the correct spelling so I would guess that they are using known good variables and suggests the one that is closest to it.
- philwelch 17y ago"What would happen if the programmer got a list of error and started fixing from the end?" I always fix the first 1 or 2 reported errors and try to recompile. I assume everyone did. A traditional "report on errors which are actually there each time they happen" compiler will give you tons of completely spurious errors which happily disappear once you fix the first few. In the instance you point out, it could just uselessly say "no member named horisontal" but it knows it already gave you that error. So instead of repeating itself or shutting up, it reasons in the alternative--even if the name was corrected, the type is wrong. I would actually find that far more useful for fixing all the compiler errors at once.
- ximeng 17y agoI agree, but it might be helpful if it put something in saying that it had made that assumption. For example they could say [...] has type 'double' (assuming argument horizontal was intended) With you on the reported errors and trying to recompile; more helpful error messages later in the build would remove some of those compile-fix-compile cycles.
- nostrademons 17y ago
- azsromej 17y agoI use it as a static analyzer for iPhone projects and it creates a nice summary of issues (as linked to) on a web page
- hannibalhorn 17y agoSame here - for missing retain/release calls (i.e., stupid memory leaks) it works great.
- eordano 17y agoyou're saying I should port all my code just because the ui is more user-friendly? what about performance, options, special warnings, builtins, and everything else? I've been using gcc for a while now, and every time I use it I found a new feature. It's rock solid and had never let me down.
- steveklabnik 17y agoYou shouldn't need to port your code; C is C, right? Or do you use a lot of gcc extensions?
- ssp 17y agoClang is compatible with the most used GCC extensions. See this: http://clang.llvm.org/features.html#gcccompat http://clang.llvm.org/features.html#gcccompat
- deleted 17y ago[deleted]
- steveklabnik 17y agoEven better!
- pgbovine 17y agojust because the ui is more user-friendly? Those better warnings aren't just due to a simple UI patch ... clang is doing better and deeper static analysis than gcc in order to even be able to produce those more intelligent warnings.
- brl 17y agoSince when is it hard to find and correct syntax errors that are preventing your C code from compiling?
- matthavener 17y agoyeah, I'm thinking this might be more useful for c++ :)
- chancho 17y agoIn the meantime before clang fully supports C++ I would appreciate a flag for GCC that just gives me abbreviated error info: file.cpp:65: error: please have a look at this line and see if anything jumps out at you. as that is basically all GCC C++ errors are good for.
- nitrogen 17y agoSomething like this might work: g++ [options] | sed -e 's/^(.*: (error|warning):).*/\1 please have a look at this line and see if anything jumps out at you/'
- j_baker 17y agoI wouldn't hold out for any miracles. I think that there are several aspects of the language itself that will make improving error messages difficult. In particular, template error messages are probably without much hope.
- hartror 17y agoI can think of one example, heavy use of macros can create a syntax error nightmare. One of the projects I've worked on (I had inherited it without documentation) had some seriously obtuse nested macros that threw a spanner in the works at least once.
- alecco 17y agoIt's better not to use macros. Let the compiler optimize. If you really need more control use __inline. Debugging code generated from complex macros is close to impossible. If you have to use someone else's macros you can use a wrapper function with exactly the same effects. Also avoid #define constants not related to compilation or pre-processing themselves. Readable code > unreadabe code. Same counts for compiled binaries.
- richcollins 17y agoBy this logic it should be easy to convince people using gcc to switch to Xcode.
- liuliu 17y agoI am currently switching between gcc and clang for my codebase. The performance is mixed. For some programs (2-level hashtable, for example), the clang version got about 50% speed up against gcc and icc. But other programs (sobel filter for example), clang version slows down about 25% (against gcc 4.4). The result certainly machine specific, but I think clang can do better in a long run.
- raeez 17y agoAlthough cryptic, I'd say that gcc's error messages are manageable. I'd hold other factors, such as performance of compiled code - across platforms - to be more important. Alternatively, they could improve on the C++ template error reporting (when they get around to taking C++ support beyond alpha) and that may be enough to sell me. a quick search yields http://clang.llvm.org/performance.html http://clang.llvm.org/performance.html (favourable, but somewhat skewed results). http://forums.cocoaforge.com/viewtopic.php?f=10&t=21016 http://forums.cocoaforge.com/viewtopic.php?f=10&t=21016 indicates improved compile times, but it's purely anecdotal. That said, does anyone else have any experience moving a performance-centric or cross platform project to clang?
- voidpointer 17y agoIf clang could come up with better C++ template error messages that would be really sweet. STL related error messages in g++ (and MSVC) are really hard to figure out, especially when C++ is not your main language; so you're not exposed to those problems on a daily basis.
- megaduck 17y agoAs a dynamic language guy, I can't help but look at the static analysis here and feel a twinge of jealousy. There really isn't anything like this in the Ruby world. Some stuff is starting to emerge, but we're still largely dependent on tests to catch stupid mistakes. When I'm chasing down a bug, it would be nice to have a variety of tools to throw at the problem.
- sruffell 17y agoI spend most of my time writing C code, but like to work with Python when I get the chance. There isn't anything like pylint for Ruby? A dynamic runtime doesn't necessarily mean you have to give up static analysis.
- riffraff 17y agoroody, rufus, reek, flay, flog, probably more. Sadly, in the current state of the ruby community it's even hard to find a library that will run with ruby's own warnings enabled.
- eru 17y agoIsn't clean code valued in the Ruby community? As a contrast, I had the impression that the Python guys care more about this in their code. They also avoid eval and other runtime code generation. (Though I do not if that's related.) It seems these two languages are further apart than in their culture than anything else.
- riffraff 17y agoclean code, yes, but some of the warnings are actually generated in situations where the code won't be cleaner (e.g. lazy initializing an instance variable not declared in the initialization method) and since quite a lot of the most common libraries have _a lot_ of warnings, it becomes unlikely that you will have them enabled in your app, and so the thing self propagates
- mfukar 17y agoclang has a nice interface, but also a long way to go; gcc has earned people's trust because it's been around for a while..
- ndl 17y agoAccording to the clang home page as of March 7th 2010, "Clang's C++ support is currently alpha quality at best, but is evolving rapidly." Obviously, clang does not mean to attract C++ developers. For a while, I'd been adamant about developing in C and not C++. One day I discovered that I'd hand-coded a vtable in order to allow polymorphism in structures of function pointers. The next day, I halved the project complexity by writing 3 classes and changing a flag in the make configuration. I use Gnu make through the Eclipse CDT on Ubuntu. It's a clunky and ugly toolchain/IDE combo that took days to configure. It compiles C, C++, CUDA and Brook+ in the same project across 3 different computers. A better C frontend would be nice, but I do not look forward to getting that working with everyone else in this chain.
- deleted 17y ago[deleted]
- cema 17y agoWhat does it mean? I do not get your formula.
- wtallis 17y agoThe clang developers fully intend to support C++. It's just a more complex language, and as an Apple-supported project, C and Obj-C were higher priorities. clang itself is written in C++ and is self-hosting. I expect that by the end of next year, it will be able to handle Boost and any other reasonable C++ code.
- nitrogen 17y agoOne day I discovered that I'd hand-coded a vtable in order to allow polymorphism in structures of function pointers. I've found myself in a similar situation. I am writing embedded software which needs to be as portable as possible, but I've found myself starting to implement polymorphism and objects in C. I am tempted to ignore platforms that lack a C compiler and switch to C++, but the reduced predictability of runtimes has kept me from doing so thus far.
- bhiggins 17y agobetter error messages are really sweet, but ultimately the most important things are 1. better performing code and 2. shorter compile times.
- yason 17y agoYou don't have to convince me — you have to convene me. I'm excited about clang but it's not that important: gcc is good enough. I'll just wait until I can install it from Ubuntu repositories, and then I'll happily try out and hopefully switch over to clang.
- antirez 17y agoI think for many large projects (> 100k lines of code at least, I guess) compilation speed can be critical. GCC is not perfect, but works well enough, is stable enough, that to switch without a strong argument is going to be a slow process at best.
- jules 17y agoClang is supposed to be faster than gcc.
- Aegean 17y agoIt's intuitive and shows new-world thinking. Most gcc developers are too old to care about doing such enhancements to gcc. They wouldn't change it just for the sake of status quo. As of 2010 we do need stuff like this, though gcc still has enough many traits not to switch to another compiler.
- alecco 17y agoNow, now. LLVM and clang are very interesting projects but changing compilers just because of one feature isn't smart. GCC is good enough in that area and the code it generates is amazing, it often beats every other C compiler out there. And that helps keeping readable code optimized. http://lambda-the-ultimate.org/node/3674 http://lambda-the-ultimate.org/node/3674
- TwoBit 17y ago>> often beats every other C compiler out there Microsoft and Intel are well-known to generate better code than GCC. So I don't know what you are referring to with "every other C compiler out there."
- winter_blue 17y agoWow, the clang error messages are really good. I've never seen a compiler produce such user-friendly error messages.
- nitrogen 17y agoThat is pretty impressive. I use colorgcc to make gcc's messages easier to spot and sort, and I always thought gcc had the best error messages (better than MSVC last time I tried it). I might just try clang to see if its messages are better in all the cases I typically encounter.