I am compiling a simple c++ file in Mac OS Majave:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake .
current folder contains a file:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
This is the CMakeList.txt:
cmake_minimum_required(VERSION 3.14)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled main.cpp)
And the error output is:
dolphins-MacBook-Air:untitled dolphin$ /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake .
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
The CMAKE_C_COMPILER:
/Users/dolphin/Desktop/Xcode.app/Contents/Developer/usr/bin/gcc
is not a full path to an existing compiler tool.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
The cc environment variable is:
dolphins-MacBook-Air:untitled dolphin$ env |grep gcc
CC=/Users/dolphin/Desktop/Xcode.app/Contents/Developer/usr/bin/gcc
Where is going wrong?
You need to set the path for CXX also. Like below:
export CC=/path/to/gcc
export CXX=/path/to/g++
# Then cmake will find the compilers
Under linux, my compilers are stored here:
/usr/bin/gcc
/usr/bin/g++
You can use which gcc and which g++ to find the paths of the compilers, provided they can be found in your path.
Related
I have been using CMake for years to build my Xcode projects. Suddenly, maybe after upgrading the OSX (11.6) or maybe after upgrading the Xcode (13.0, commandline tools 13.0), cmake does not work anymore.
First let me give some of the things I have already tried so I am just not referred to an answered question and forgotten:
sudo xcode-select --reset This has no effect.
I have tried resetting the cmake variable manually with
cmake .. -DCMAKE_C_COMPILER="/usr/bin/cc" and to other paths. It still gives a "Compiling the C compiler identification source file "CMakeCCompilerId.c" failed." in the CMakeError.log. No c compiler that I can find in my system seems to work including clang in various locations.
I have tried reseting the environmental variables CC, CXX, CLANG, etc to different paths.
This might have something to do with Anaconda which I have installed.
I created a test project to pinpoint the problem. In it is a main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
and a CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled main.cpp)
The in a build subdirectory I run
>>> cmake .. -G Xcode
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:2 (project):
No CMAKE_CXX_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also "../Test/build/CMakeFiles/CMakeOutput.log".
See also "../Test/build/CMakeFiles/CMakeError.log".
The beginning of CMakeFiles/CMakeError.log is:
Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler:
Build flags: -march=core2;-mtune=haswell;-mssse3;-ftree-vectorize;-fPIC;-fPIE;-fstack-protector-strong;-O2;-pipe;-isystem;/opt/anaconda3/include
Id flags:
The output was:
65
Command line invocation:
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild
User defaults from command line:
IDEPackageSupportUseBuiltinSCM = YES
note: Using new build system
note: Planning
Analyze workspace
.
.
.
clang-10: error: invalid Darwin version number: macos11.3
clang-10: error: invalid version number in '-target x86_64-apple-macos11.3'
.
.
.
I manage to workout with a similar problem setting some variables inside CMakeLists.txt. Is not a deffinitive solution, however. It needs a followup.
cmake_minimum_required(VERSION 3.14)
project(untitled)
set(CMAKE_C_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc")
set(CMAKE_CXX_COMPILER "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++")
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled main.cpp)
==== EDIT =====
Following this idea I managed to fix this problem (which was one problem on my side too).
There's a problem with the variable CC and CXX created by Xcode. They try to do something like:
xcrun -find cc
The result of that, on my side is:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
This is consistent and, if I look for that compiler, it exists. But, it doesn't work within CMake.
I manage to make it work setting those two variables to where they are pointing at:
export CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
export CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
After that, I managed to compile with CMake without setting the variable inside of the CMakeLists.txt file.
You should add those exports to the end of your .bash_profile
Asked this question:
CMake cannot indentify path to compiler mentioned by set(CMAKE_C_COMPILER "path") rule
received an incorrect rules order response(it was obviously correct).
Aplied changes and tried again:
cmake .
output:
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: nmake /nologo cmTC_fc6d3\fast &&
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe - broken
But I need not nmake but mingw make file ,found this topic:
CMake Error : execution of make failed on Windows
The 100% same problem I have .Tried provided solution:
You need to specify a generator in CMake command line like -G "MinGW
Makefiles" and - depending on how many compilers you have installed -
also the full paths to the compilers.
cmake -G "MinGW Makefiles"
output:
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
CMake Error: Error: generator : MinGW Makefiles
Does not match the generator used previously: NMake Makefiles
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
and next cmake . output gives (after deleting CMakeCache.txt file and CMakeFiles directory as mentioned above):
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
CMake Error: CMake was unable to find a build program corresponding to "NMake Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:44 (try_compile):
Failed to configure test project build system.
Call Stack (most recent call first):
CMakeLists.txt:9 (project)
-- Configuring incomplete, errors occurred!
See also "CMakeFiles/CMakeOutput.log".
CMake Error: Generator: execution of make failed. Make command was: cmTC_4b6e5/fast &&
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe - broken
The 3 things i cannot understand:
How to ... specify ... the full paths to the compilers or needed source or binary directory.
Why compilers are connected to make.exe in any way.
Why CMAKE_MAKE_PROGRAM is not set if there is proper rule.
Changing rules order :
set( CMAKE_MAKE_PROGRAM FORCE ) set(CMAKE_C_COMPILER ) set(CMAKE_CXX_COMPILER ) and
set(CMAKE_C_COMPILER ) set(CMAKE_CXX_COMPILER ) set( CMAKE_MAKE_PROGRAM FORCE ) has the same result after call.
If I remove set( CMAKE_MAKE_PROGRAM ) rule configuring with CMake GUI is possible but with cmd manual cmake . call is not .Same output as provided above.
Inside CMakeCache.txt generated by CMake GUI call's are lines :
//Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=C:/Strawberry/c/bin/mingw32-make.exe
so for it was possible to find mingw32-make.exe and path provided for manual call is also right.
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.1)
#set(CMAKE_MAKE_PROGRAM C:/Strawberry/c/bin/make.exe FORCE )
set( CMAKE_MAKE_PROGRAM C:/Strawberry/c/bin/mingw32-make.exe FORCE )
set(CMAKE_C_COMPILER "C:/Strawberry/c/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/Strawberry/c/bin/g++.exe")
project("Client")
enable_testing()
set(CMAKE_CXX_FLAGS "-std=c++17 " )
add_executable(Client
main.cpp
client.cpp
client.h
logmsg.cpp
includes.h
)
target_link_libraries(Client wsock32 ws2_32)
add_test(NAME test COMMAND Client )
I am trying to setup cmake using Windows 10 using MinGW. I have included the path c:/MinGW/bin in my system path and environment path settings. I have removed sh.exe from my path (although, i would love to be able to keep this if possible).
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/gcc.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Output
C:\School\athabascua\data structures\ass1>cmake -g "MinGW Makefiles" .
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_b3144\fast"
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: C:/School/athabascua/data structures/ass1/CMakeFiles/CMakeTmp
Run Build Command:"nmake" "/NOLOGO" "cmTC_b3144\fast"
Generator: execution of make failed. Make command was: "nmake" "/NOLOGO"
"cmTC_b3144\fast"
It seems that the GNU compilers are identified but don't seem to work. Any help would be much appreciated. I am trying to avoid using Cygwin.. but almost ready to go that route in a sec here.
To fix the problem I was having, I had to do two things.
use the command cmake -G "MinGW Makefiles" . (capital -G on windows)
update my CMakeLists.txt file to use gcc for C compiler and g++ for C++ compiler
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")
project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Is there a way to use the Clang compiler while mixing C++ and Fortran?
Until now I use cmake with
project(mixing CXX Fortran)
but this triggers the use of g++.
-- The CXX compiler identification is GNU 6.2.0
CMakeLists.txt of my project with Fortran mixing:
cmake_minimum_required(VERSION 3.7.0)
project(mixing CXX Fortran)
# SET UP ROOT https://root.cern.ch/how/integrate-root-my-project-cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /opt/local/libexec/root6/etc/root/cmake)
find_package(ROOT REQUIRED COMPONENTS MATH MINUIT2)
include(${ROOT_USE_FILE})
include_directories(Experiment Theory ${ROOT_INCLUDE_DIRS})
add_executable(mixing main.cpp)
target_link_libraries(mixing ${ROOT_LIBRARIES})
Not working, because g++ cannot use the needed Clang flag -stdlib=libc++ of the ROOT library.
You can always override c/c++ compiler by changing CMAKE_<LANG>_COMPILER, where <LANG> in your case is C or CXX.
You can set your CC and CXX environment variables to override defaults for CMAKE_C_COMPILER / CMAKE_CXX_COMPILER:
CC=clang CXX=clang++ cmake
You can set these on command line when invoking cmake:
cmake -D CMAKE_C_COMPILER=clang -D CMAKE_CXX_COMPILER=clang++
You can also set these directly in your cmake file:
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
make sure however that you do that at the very top of your cmake file before any project/enable_language directive is used.
I have a simple C++ test project and wrote my CMakeLists.txt file as follows;
cmake_minimum_required(VERSION 2.8)
set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++")
project(simpleTest)
add_executable(main main.cpp)
When i try to run CMake GUI an set generator to MinGW i get the following error message:
The C compiler identification is GNU 4.6.1
The CXX compiler identification is GNU 4.6.1
Check for working C compiler: C:/MinGW/bin/gcc
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: Internal CMake error, TryCompile configure of cmake failed
Check for working C compiler: C:/MinGW/bin/gcc -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test
program.
It fails with the following output:
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:7 (project)
CMake Error: your C compiler: "C:/MinGW/bin/gcc" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "C:/MinGW/bin/g++" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
Configuring incomplete, errors occurred!
I'm on Windows 7 64 bit and i confirmed on cmd that;
G++ --version gives G++ (GCC) 4.6.1.
What am I doing wrong?
Sorry but this looks like the compiler is not installed where you specified. Also see here why you should avoid setting the compiler in CMakeLists.txt
So I'd remove those, clear the cmake cache, set the environment variables CC and CXX before calling CMake and try again.
I think your problem lies in the path string. Try this:
set(CMAKE_C_COMPILER "C:\\MinGW\\bin\\gcc")
set(CMAKE_CXX_COMPILER "C:\\MinGW\\bin\\g++")
Alternatively, you can also set your compilers in the CMake GUI. After you click on generate, choose the option "Specify native compilers" and set or browse to the correct location.