Hi,
Not sure if this is the right place to ask this question, but I’ve already tried every SO solution I could find, and thought maybe someone from the CMake community might help.
I’m learning Cpp using exercism, and the tests for this simple program:
#include "leap.h"
namespace leap {
bool is_leap_year(int year) {
return (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0));
}
} // namespace leap
are done using CMake. This is the CMakeLists.txt file:
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)
# Name the project after the exercise
project(${exercise} CXX)
# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})
# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()
# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()
set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 14
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)
set(CMAKE_BUILD_TYPE Debug)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()
# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()
# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()
# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Following the instructions, I did:
mkdir build
cd build
cmake ..
And got:
(base) adamhaber-s-macbookpro1:build adamhaber$ cmake -G Xcode ..
-- The CXX compiler identification is unknown
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - broken
CMake Error at /usr/local/Cellar/cmake/3.19.2/share/cmake/Modules/CMakeTestCXXCompiler.cmake:59 (message):
The C++ compiler
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/xcodebuild -project CMAKE_TRY_COMPILE.xcodeproj build -target cmTC_d7a05 -configuration Debug -hideShellScriptEnvironment && User defaults from command line:
HideShellScriptEnvironment = YES
Prepare build
note: Using legacy build system
=== BUILD TARGET cmTC_d7a05 OF PROJECT CMAKE_TRY_COMPILE WITH CONFIGURATION Debug ===
Check dependencies
Write auxiliary files
/bin/mkdir -p /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64
write-file /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/cmTC_d7a05.LinkFileList
CompileC CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/testCXXCompiler.o testCXXCompiler.cxx normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp
/Users/adamhaber/miniconda3/bin/x86_64-apple-darwin13.4.0-clang -x c++ -target x86_64-apple-macos10.14 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -stdlib=libc++ -Wno-trigraphs -fpascal-strings -O2 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -Wno-semicolon-before-method-body -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug/include -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/DerivedSources-normal/x86_64 -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/DerivedSources/x86_64 -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/DerivedSources -F/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -pipe -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 -MMD -MT dependencies -MF /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/testCXXCompiler.d --serialize-diagnostics /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/testCXXCompiler.dia -c /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx -o /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/testCXXCompiler.o
Ld Debug/cmTC_d7a05 normal x86_64
cd /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target x86_64-apple-macos10.14 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -L/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug -F/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug -filelist /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/cmTC_d7a05.LinkFileList -stdlib=libc++ -Wl,-pie -Wl,-headerpad_max_install_names -Wl,-dead_strip_dylibs -Wl,-search_paths_first -Wl,-headerpad_max_install_names -Xlinker -dependency_info -Xlinker /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/cmTC_d7a05_dependency_info.dat -o /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug/cmTC_d7a05
ld: warning: ignoring file /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_d7a05.build/Objects-normal/x86_64/testCXXCompiler.o, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 )
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
** BUILD FAILED **
The following build commands failed:
Ld Debug/cmTC_d7a05 normal x86_64
(1 failure)
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:8 (project)
-- Configuring incomplete, errors occurred!
After going over many SO answers, I’ve realised that the problem is that CMake is using Clang and not Homebrew’s GCC. I’ve fixed this using a symlink, and now I get a different CMake error:
(base) adamhaber-s-macbookpro1:build adamhaber$ cmake ..
-- The CXX compiler identification is unknown
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/local/bin/g++
-- Check for working CXX compiler: /usr/local/bin/g++ - broken
CMake Error at /usr/local/Cellar/cmake/3.19.2/share/cmake/Modules/CMakeTestCXXCompiler.cmake:59 (message):
The C++ compiler
"/usr/local/bin/g++"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/xcodebuild -project CMAKE_TRY_COMPILE.xcodeproj build -target cmTC_82cfa -configuration Debug -hideShellScriptEnvironment && User defaults from command line:
HideShellScriptEnvironment = YES
Prepare build
note: Using legacy build system
=== BUILD TARGET cmTC_82cfa OF PROJECT CMAKE_TRY_COMPILE WITH CONFIGURATION Debug ===
Check dependencies
Write auxiliary files
/bin/mkdir -p /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64
write-file /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/cmTC_82cfa.LinkFileList
CompileC CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.o testCXXCompiler.cxx normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp
/usr/local/bin/gcc -x c++ -target x86_64-apple-macos10.14 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -stdlib=libc++ -Wno-trigraphs -fpascal-strings -O2 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -Wno-semicolon-before-method-body -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug/include -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/DerivedSources-normal/x86_64 -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/DerivedSources/x86_64 -I/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/DerivedSources -F/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/Debug -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -pipe -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 -MMD -MT dependencies -MF /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.d --serialize-diagnostics /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.dia -c /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx -o /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.o
gcc: error: x86_64-apple-macos10.14: No such file or directory
gcc: error: /Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeTmp/CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.dia: No such file or directory
gcc: error: unrecognized command-line option '-target'
gcc: error: unrecognized command-line option '-fdiagnostics-show-note-include-stack'; did you mean '-fdiagnostics-show-template-tree'?
gcc: error: unrecognized command-line option '-fmacro-backtrace-limit=0'; did you mean '-ftemplate-backtrace-limit='?
gcc: error: unrecognized command-line option '-stdlib=libc++'
gcc: error: unrecognized command-line option '-fpascal-strings'
gcc: error: unrecognized command-line option '-fasm-blocks'
gcc: error: unrecognized command-line option '--serialize-diagnostics'
Command /usr/local/bin/gcc failed with exit code 1
** BUILD FAILED **
The following build commands failed:
CompileC CMAKE_TRY_COMPILE.build/Debug/cmTC_82cfa.build/Objects-normal/x86_64/testCXXCompiler.o testCXXCompiler.cxx normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:8 (project)
-- Configuring incomplete, errors occurred!
See also "/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeOutput.log".
See also "/Users/adamhaber/Exercism/cpp/leap/build/CMakeFiles/CMakeError.log".
Any help would be much appreciated!