Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: how to add compiler flags to non-default compiler

Hello I want to build a project with intel compiler.

With default gcc I usually run:

cmake -DCMAKE_CXX_FLAGS=-I/some/path  /path/to/project

And this works fine.

cmake -DCMAKE_CXX_COMPILER=icpc -DCMAKE_C_COMPILER=icc  -DCMAKE_CXX_FLAGS=-I/some/path  /path/to/project

When I try to use non-default compiler it does not path CMAKE_CXX_FLAGS variable content to compiler at all.

How to fix this?

Correct Answer is:

  1. You need to specify the type of the CMAKE_CXX_FLAGS variable:

    -DCMAKE_CXX_FLAGS:STRING=-I/some/path
    
  2. You need to provide full path to C and C++ compilers:

    cmake -DCMAKE_C_COMPILER=/opt/intel/bin/icc -DCMAKE_CXX_COMPILER=/opt/intel/bin/icpc -DCMAKE_CXX_FLAGS:STRING=-some-flag
    
like image 520
Artyom Avatar asked Jan 01 '26 18:01

Artyom


1 Answers

The good way to do what you expect is to use:

export CC=icc CXX=icpc cmake -DCMAKE_CXX_FLAGS=-I/some/path  /path/to/project
like image 125
Nadir SOUALEM Avatar answered Jan 03 '26 08:01

Nadir SOUALEM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!