How to change compiler options of build in Eclipse CDT

How to add compiler flag in Eclipse CDT
How to set compiler flag in Eclipse CDT

Sometimes you may want to add a specific compiler flag to the build commands used to build your code by Eclipse CDT. For example, say you want to add the -std=c++11 flag to build your code. Adding this is easy:

  1. Go to Project > Properties > C/C++ Build > Settings > Cross G++ Compiler > Miscellaneous

  2. In the Other flags entry, append your flag. In this case, we append -std=c++11

That is it! Eclipse CDT will use this flag to compile your code.

Tried with: Eclipse CDT 3.7.2

Advertisements

How to change compiler in Eclipse CDT

Eclipse CDT uses gcc, g++ and as as the C compiler, C++ compiler (and linker) and assembler by default. Sometimes, you may want to use a different version or different type of compiler, linker or assembler to build your code. Changing this in Eclipse CDT for your project is easy:

  • Go to Project > Properties > C/C++ Build > Settings > Tool Settings.

  • In this dialog, to change the C++ compiler, change the entry in Cross G++ Compiler > Command.

  • In this dialog, to change the C compiler, change the entry in Cross GCC Compiler > Command.

  • In this dialog, to change the C++ linker, change the entry in Cross G++ Linker > Command. This is typically the same as the C++ compiler command.

  • In this dialog, to change the assembler, change the entry in Cross GCC Assembler > Command.

Click Apply and OK. Build your code and you will see that the new tools are used.

Tried with: Eclipse 3.7.2

How to set C or C++ compiler for CMake

Sometimes you might need to use a different version of a default compiler (like gcc) or a different compiler (like Intel compiler) to build your code. Specifying this is easy in CMake. Add these lines to your CMakeLists.txt:

SET(CMAKE_C_COMPILER /path/to/c/compiler)
SET(CMAKE_CXX_COMPILER /path/to/cpp/compiler)

Tried with: CMake 2.8.7 and Ubuntu 12.04 LTS

Architecture Identification Macro in CUDA

The __CUDA_ARCH__ macro is defined only for device code. It is a macro that identifies the CUDA architecture version that the NVCC compiler is compiling the code for. When defined, it holds a 3-digit value of the form xy0, which indicates a particular CUDA architecture. For example, sm_20 is stored as 200, sm_13 as 130 and so on.

This macro is useful to compile different code for host and device:

__host__ __device__ void foobar()
{
#ifdef __CUDA_ARCH__
    // Device specific code
#else
    // Host specific code
#endif
}

This macro is also useful to write device code that is specific to particular architectures:

__global__ void fooKernel()
{
#if __CUDA_ARCH__ >= 200
    // Code that uses advanced features
    // introduced in sm_20 and later
#else
    // Fallback code for older architectures
#endif
}

Reference: Section on Virtual Architecture Identification Macro in the NVCC Manual.

Tried with: CUDA 5.5 and Ubuntu 12.04 LTS