Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

conan doesn't recieve CC/CXX variables from CLion #40

Closed
kees-jan opened this issue Mar 28, 2020 · 8 comments · Fixed by aminya/project_options#33 or #169
Closed

conan doesn't recieve CC/CXX variables from CLion #40

kees-jan opened this issue Mar 28, 2020 · 8 comments · Fixed by aminya/project_options#33 or #169

Comments

@kees-jan
Copy link
Contributor

kees-jan commented Mar 28, 2020

Testcase:

  • In CLion, define a profile with an alternative compiler (for me, it's clang-10)
  • Clear the conan cache
  • Have CLion regenerate the project

Expected result

  • This should complete without errors, and allow the project to be built
    • All conan packages should be build

Actual result

  • CLion calls something along the lines of
    /home/kees-jan/opt/clion-2019.3.5/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/bin/clang-10 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-10 -G "CodeBlocks - Unix Makefiles" /home/kees-jan/src/cpp_starter_project
  • This triggers conan to build the dependencies
  • That fails (among others in docopt) with
CMake Error at conanbuildinfo.cmake:553 (message):
  Incorrect 'clang', is not the one detected by CMake: 'GNU'
Call Stack (most recent call first):
  conanbuildinfo.cmake:136 (conan_check_compiler)
  CMakeLists.txt:5 (CONAN_BASIC_SETUP)

Analysis

  • Conan expects the CC and CXX variables to be set, in addition to the compiler settings, and they are not.
  • I'm not sure whether to blame CLion, CMake, Conan, or this starter project for that shortcoming
@ddalcino
Copy link
Collaborator

ddalcino commented Mar 28, 2020

Can you build the project from the command line? That would be an easy way to rule out clion as the culprit.

@kees-jan
Copy link
Contributor Author

I'm surprised to say that yes, I can. Running the command-line quoted above succeeds without problems.

So now I'm speculating that CLion somehow messes up the environment and in that way triggers the failure? Odd...

@kees-jan
Copy link
Contributor Author

I'm surprised to say that yes, I can. Running the command-line quoted above succeeds without problems.

So now I'm speculating that CLion somehow messes up the environment and in that way triggers the failure? Odd...

And indeed, to trigger the failure I'm observing from the commandline, I have to execute

CC=cc CXX=c++ /home/kees-jan/opt/clion-2019.3.5/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/bin/clang-10 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-10 -G "CodeBlocks - Unix Makefiles" /home/kees-jan/src/cpp_starter_project

I.e. set CXX and CMAKE_CXX_COMPILER to conflicting values

@ddalcino
Copy link
Collaborator

Ok, then we know for certain that CLion is the problem, and not this project or conan.

You will need to set up a new toolchain and a new cmake profile. To do that, you go to:
'File > Settings > Build, Execution, Deployment > Toolchains'
'File > Settings > Build, Execution, Deployment > CMake'

The documentation you want should be here: https://www.jetbrains.com/help/clion/how-to-switch-compilers-in-clion.html. This page should be able to help you a lot better than I can.

@kees-jan
Copy link
Contributor Author

kees-jan commented Apr 6, 2020

I have now been able to reproduce the problem from the commandline

Preparation:

cd cpp_starter_project
mkdir build
cd build/
cmake -DCMAKE_C_COMPILER=/usr/bin/clang-10 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-10 ..

Trigger the problem:

conan remove -f '*'
cmake -DCMAKE_C_COMPILER=/usr/bin/clang-10 -DCMAKE_CXX_COMPILER=/usr/bin/clang++-10 ..

The final cmake invocation fails with nonzero exit status, because conan detects GCC instead of clang.

Instead of that final cmake invocation, you might also call make. That too makes conan attempt to build its packages, and fails with the same error.

@ddalcino
Copy link
Collaborator

According to conan-io/conan#1211, conan runs the compiler specified by the CC and CXX environment variables, not the cmake variables CMAKE_C_COMPILER and CMAKE_CXX_COMPILER. That thread suggests using conan profiles to set these variables, but that requires some modifications to cmake/Conan.cmake (see #32 if you want to do this).

I think it's easier to just set CC and CXX via command line. Try this:

rm -rf build/
mkdir build && cd build
CC=clang CXX=clang++ cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ..
cmake --build .          # Conan targets were already built using clang
conan remove -f '*'
cmake --build .          # Builds conan targets using system default --> failure
CC=clang CXX=clang++ cmake --build .   # Builds conan targets using clang --> success

If this works, you will need to convince CLion to inject those environment variables into the build command. The documentation linked in my last post explains how to do that.

ddalcino added a commit to ddalcino/cpp_starter_project that referenced this issue Apr 14, 2020
patrickelectric pushed a commit to patrickelectric/cpp_starter_project that referenced this issue Oct 5, 2020
1aam2am1 pushed a commit to 1aam2am1/cpp_starter_project that referenced this issue May 5, 2021
This fixes cpp-best-practices#40 when building using an alternate compiler like `clang`.
@LtdSauce
Copy link
Contributor

I got it to work applying the fix from #122 to the conan_cmake_install-call:

diff --git a/cmake/Conan.cmake b/cmake/Conan.cmake
index b57566f..44d1968 100644
--- a/cmake/Conan.cmake
+++ b/cmake/Conan.cmake
@@ -54,7 +54,10 @@ macro(run_conan)
       cpp_starter_use_imgui=${CPP_STARTER_USE_IMGUI}
       cpp_starter_use_sdl=${CPP_STARTER_USE_SDL}
       SETTINGS
-      ${settings})
+      ${settings}
+      ENV
+      CC=${CMAKE_C_COMPILER}
+      CXX=${CMAKE_CXX_COMPILER})
   endforeach()
 
 endmacro()

But i am not convenient enough about this to say that it is good and the right way to do it.

@aminya
Copy link
Contributor

aminya commented Dec 4, 2021

This is fixed in ProjectOptions v0.11.2
https://github.com/aminya/ProjectOptions

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants