2

I have a very large project that I'm trying to migrate from GNU make to cmake. As such, I think having intermediary files containing various variables is a good way to go.

Since I'm just starting out, this is what I have for top_directory/vars/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

set(VARIABLE "value")

This way, in another folder, I can add this line to that CMakeLists.txt:

add_subdirectory(top_directory/vars)

However, when trying to run cmake on top_directory/vars/CMakeLists.txt, I receive the following error:

me@host:/tmp>cmake .
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 7.3.1
-- Check for working C compiler: /usr/bin/g++
-- Check for working C compiler: /usr/bin/g++ -- broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "/usr/bin/g++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /tmp/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/bin/gmake" "cmTC_607e9/fast"
    /usr/bin/gmake -f CMakeFiles/cmTC_607e9.dir/build.make CMakeFiles/cmTC_607e9.dir/build
    gmake[1]: Entering directory '/tmp/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_607e9.dir/testCCompiler.c.o
    /usr/bin/g++    -o CMakeFiles/cmTC_607e9.dir/testCCompiler.c.o   -c /tmp/CMakeFiles/CMakeTmp/testCCompiler.c
    /tmp/CMakeFiles/CMakeTmp/testCCompiler.c:2:3: error: #error "The CMAKE_C_COMPILER is set to a C++ compiler"
     # error "The CMAKE_C_COMPILER is set to a C++ compiler"
       ^~~~~
    gmake[1]: *** [CMakeFiles/cmTC_607e9.dir/build.make:66: CMakeFiles/cmTC_607e9.dir/testCCompiler.c.o] Error 1
    gmake[1]: Leaving directory '/tmp/CMakeFiles/CMakeTmp'
    gmake: *** [Makefile:126: cmTC_607e9/fast] Error 2




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt


-- Configuring incomplete, errors occurred!
See also "/tmp/CMakeFiles/CMakeOutput.log".
See also "/tmp/CMakeFiles/CMakeError.log".

Now I can force it by adding -DCMAKE_C_COMPILER=gcc, but that really defeats the portability aspect.

So my question is: is this the right way to be making "variable" CMake files and why does CMake incorrectly detect my C compiler?

Helpful Information:

cmake --version
cmake version 3.11.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).

gcc --version
gcc (SUSE Linux) 7.3.1 20180323 [gcc-7-branch revision 258812]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If there is any additional information I can provide, then please let me know.

SailorCire
  • 548
  • 1
  • 7
  • 24
  • As specified in the referenced answer **Do not overwrite `CMAKE_C_COMPILER`, but export `CC` (and `CXX`) before calling `cmake`**. – David C. Rankin May 24 '18 at 02:58
  • 1
    Actually, the error "The CMAKE_C_COMPILER is set to a C++ compiler" seems to be self-explanatory: someone assign C++ compiler (`/usr/bin/g++`) to the variable, denoted for C compiler. Instead, C++ compiler should be assigned to `CMAKE_CXX_COMPILER` variable, and `CMAKE_C_COMPILER` variable should be set to C compiler. – Tsyvarev May 24 '18 at 08:50
  • No, because when I have a program as output, everything works. @DavidC.Rankin cc is soft linked to gcc. – SailorCire May 24 '18 at 14:02
  • To be specific, when I have a CMakeLists.txt like this https://pastebin.com/PLG0gePL, everything works perfectly. – SailorCire May 24 '18 at 14:10
  • @SailorCire - I've reopened the question. From what you describe, you were describing the exact problem already answered in the question I listed, if that isn't the case, then there is another issue, but you should not be overwriting the `CMAKE_C_COMPILER` macro, you should be exporting the proper values for `CC` and `CXX`. – David C. Rankin May 24 '18 at 16:27
  • I think it might be a bug. I messed up on another CMakeLists.txt and got the same error; however, when forced CMAKE_C_COMPILER _(yes I know you said not to)_ then it wound up spitting useful information. – SailorCire May 24 '18 at 17:05

0 Answers0