Why is clang used for autocompletion in vim and emacs? - gcc

Why isn't gcc used for that? Where is the difference between them and why does almost any autocomplete plugin require clang?

The simple answer is that clang was designed to support completion while gcc was not.
Clang has a command line option that prints out possible completions at a given point in a source file, which makes it easy to use in scripts: Just shell out to clang, parse its output, done. Gcc has nothing comparable.
As for why, see this list of differences between gcc and clang:
[...]
Clang is designed as an API from its inception, allowing it to be reused by source analysis tools, refactoring, IDEs (etc) as well as for code generation. GCC is built as a monolithic static compiler, which makes it extremely difficult to use as an API and integrate into other tools. Further, its historic design and current policy makes it difficult to decouple the front-end from the rest of the compiler.

Related

Determining control flow of GCC

I want to understand how GCC works, therefore would like to know which functions are executed in which sequence. For me, it is hard to understand it by only looking at the GCC source code.
Is there any tool to track the internal functions that are being called by GCC when compiling a c file?
You can use debugger to execute gcc step-by-step, but gcc it is quite complicated piece of software.

Clang or GCC compiler for c++ 11 compatibility programming on Windows?

I was wondering which compiler is better to use on Windows OS (8.1) in temrs of compatibility to c++11's (and later 14) functions, liberies and features (like lambdas) and is also comfortable to use (less bugs).
I am a university student hence I'm not looking at the subject product-wise (even though I do like to code a bit more than just projects for my studies).
I am currently using eclipse luna IDE if it matters.
Notice that compiler != IDE.
VC++ is one of the most populars on Windows and depending on its version it has a good support for C++11 features. Check the list on the msdn blog to find out if there's everything you need.
Gcc is also ported to Windows and you can install MinGW to use it (4.8.1.4 at the moment of writing this). It is pretty complete on C++11.
Clang is also available for the Windows platform and it is also complete on C++11 support (plus it has good diagnostic messages), but notice that you will have to use another linker since clang doesn't ship with one (although there is an ongoing effort to write it: http://lld.llvm.org/)
All the compilers I cited above are pretty stable but, based on my experience, if you're looking for latest and greatest C++11/14/17 features, you might just want to go for gcc or clang (VC++ is slower in adding support for newest features and the compiler is undergoing a huge update to modernize). Just keep in mind that these are compilers and not just IDEs, an IDE is a front-end supporting program that uses a compiler undercover to compile files.
To set up a C++11 compiler, I suggest installing MSYS2, it has a package manager (pacman) that can install fresh versions of GCC, GDB, Clang and many libraries like SDL, Lua etc. Very easy to use too.
As far as GCC vs CLang goes - I really tried hard to make CLang work (which is presumably faster and more friendly than GCC - produces better warnings, etc.), but failed. Issues were that CLang (which comes with MSYS2) is hard-coded to use GCC linker which produces some strange linker errors when using libstdc++ (std implementation from GCC). libc++ (a new implementation designed to work with CLang) didn't worked for me on Windows either.
So you either try build CLang from sources and hope that some configuration will work with C++11 library, OR just stick with GCC which works just fine out of the box.
As IDE, I suggest to take a look at CLion. It is very comfortable (infinitely more user-friendly and intuitive than Visual Studio, IMO). Just install it and point it to the mingw64 (or mingw32) folder of MSYS2, it will auto-detect everything for you.
It only works with CMake projects though.

How to call a C compiler through library instead of via system call?

In a project that I have been asked to revise, there is a segment of code that is tantamount to just generating a set of assembly instructions, writing them to a file, and then compiling it with the gcc compiler.
My question is, is there any way to link in a library that would do this work for me via an exposed API call? I need 1-1 equivalence to the following command:
gcc -m32 -c -o objfile generated_asm.asm -masm=intel
You cannot do exactly that. You might fork the GCC compilation command. It is probably operating system specific, I'm supposing you are on Linux (or some other POSIX system).
However, there are alternatives:
using asmjit to generate x86 machine code in memory
using tinycc and its libtcc library (which can compile a string containing C or asm code; beware, the compiled machine code is slow since unoptimized)
using a JIT library like libjit, or LLVM, or GNU lightning
coding in a metaprogramming language like Common Lisp (e.g. SBCL) or MetaOcaml
Also, you could simply fork a GCC compilation of some generated C file genfoo.c into a shared object (gcc -Wall -O -fPIC genfoo.c -shared -o genfoo.so) then dynamically loading with dlopen the ./genfoo.so file (see also this)
PS. Next GCC 5.0 release will have a JIT library (libgccjit).
Generally, no, because Unix insisted we think of everything as text strings (including command lines). And we all accepted Unix.
Technically GCC is just giant subroutine with a big parameter list.
The fact that you get to it though a text string is a stupid design decision we all made by dumping Multics and/or LISP machines. On these systems, subroutines (even big ones) are all called natively, and thus really compose.
With Multics, the compiler is (well was, Multics is pretty dead) a subroutine you can call. (Yes, there was a command line interface that could also make that call, so people could invoke it).

How should I disable C++0x and/or C++11 on the command line with Intel's Windows compiler?

The build system on my cross-platform project has a command line for Intel's Windows C++ that may or may not have /Qstd=c++0x as a result of detecting the compiler feature set. For most of the code base, this works well, however for a small number of CUDA files, I need to disable the more recent dialects of C++ to suit the constraints of the nvcc wrapper compiler.
How should I phrase something like /Qstd=c++98 or /Qnostd=c++0x at the end of the command line so that it overrides any earlier specifications of C++ dialect?
Edit: Having been educated that these flags are actually for the Intel compiler, I have found that appending /Qstd=c++98 is probably the right approach.
You can't for MSVC. Each MSVC version expects its own interpretation of something between two or three standards, and you're stuck with it.
The options you quote are for the Intel Compiler (see here). If possible, I'd suggest using the Intel Compiler then.
I do fail to see how disabling the recent dialects in the C++ compiler will please the nvcc wrapper compiler... Just don't write C++11 code, and you'll be fine right?

Portable method to package C++11 program sources

so, C++11 has been around for a while and, given there already are compilers supporting it on most platforms, it would be nice to use it in some real software -- e.g. one that can be packaged in as-portable-as-possible package, preferably providing ./configure and so.
Because both Clang and GCC currently need -std=c++11 flag to compile c++11 source, and both sometimes require specific flags to work correctly (see for example How to compile C++11 with clang 3.2 on OSX lion? or C++11 Thread not working ), I'm quite afraid that the package won't work on some platforms that already support c++11 because of wrong invocation of compiler.
Q: Is there some standard how to correctly and portably compile c++11? E.g. autotools/autoconf check or some list of compiler/platform directives that describe all possible needed options? Or does the situation come from the fact that c++11 standard implementations are currently marked as "experimental" and the standard will eventually stabilize and become the default choice, not needing any usage of extra compiler flags?
Thanks
-exa
Well, if you`re trying to write portable code, i would recommend using cmake
a very powerful cross-platform, open-source build system.
Using cmake you should be able to identify the compilers available in your current machine and then generate your makefiles using the flags that you want in each case.
I have been using cmake for almost a year by now and it has significantly reduced the time consumed when trying to get a project compiling in different platforms.
I`m using CMake to generate Makefiles of C++11 projects. The only change in CMakeLists.txt I need to do is add the following:
ADD_DEFINITIONS("-std=gnu++11")
ADD_DEFINITIONS("-D_GLIBCXX_USE_C99_STDINT_TR1")
ADD_DEFINITIONS("-D_GLIBCXX_HAS_GTHREADS")
However, as I use Qt, I re-compile QtSDK with a new gcc version 4.8 and get a complete mingw system that use gcc in version 4.8.
Makings these changes, the project compile and run in Windows XP, Windows 7 and linux both 32 and 64 bits. I didn`t test it in OSX yet.

Resources