GoogleTest crash when using CMake/XCode/arm64

This is a really weird problem and I am not even sure it is a CMake issue… I was able to reproduce the issue with the simplest project possible and it’s 100% reproducible. The project contains 1 file with 1 GoogleTest:

#include <gtest/gtest.h>

TEST(Fft, compute)
{
  ASSERT_EQ(4, 2 + 2);
}

I am on macOS 12.3.1 with the latest version of XCode and running on the M1 CPU.

If I use the “Unix Makefiles” generator (which is the default generator for CMake) and build and run the test… it just works:

> file ./gtestcrash_test
./gtestcrash_test: Mach-O 64-bit executable arm64

> ./gtestcrash_test
Running main() from /Users/ypujante/tmp/gtestcrash/external/google/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Fft
[ RUN      ] Fft.compute
[       OK ] Fft.compute (0 ms)
[----------] 1 test from Fft (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

If I use the XCode generator, then it just crashes

> file ./gtestcrash_test
gtestcrash_test: Mach-O 64-bit executable arm64
>./gtestcrash_test
[1]    85280 killed     ./gtestcrash_test

Note that this problem does not exist if I compile on a x86_64 machine or if I specify x86_64 as the architecture!

I am not sure what is the actual problem. Is it CMake? Is it GoogleTest? Is it XCode? Is it me not using CMake properly? I do not understand why the “Xcode” generator builds an executable that crashes…

For reference this is what the CMakeLists.txt looks like

cmake_minimum_required(VERSION 3.19)
project(gtestcrash)
enable_testing()
set(CMAKE_CXX_STANDARD 17)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
option(INSTALL_GTEST "" OFF)
set(BUILD_GMOCK OFF)
add_subdirectory("../googletest" ${CMAKE_BINARY_DIR}/googletest EXCLUDE_FROM_ALL)

include(GoogleTest)

set(target_test "gtestcrash_test")
add_executable("${target_test}" "test.cpp")
target_link_libraries("${target_test}" gtest_main ${target})
gtest_discover_tests("${target_test}")

Can you post the verbose build log for both? Use cmake --build --verbose for building both. You can also post the output of otool -L gtestcrash_test. There must be some difference in how they are being compiled.

@bill.hoffman thank you for your suggestion.

Here is the output.

First for Unix Makefiles

> cmake ..                                                                                                                                    
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin/python3.8 (found version "3.8.9") found components: Interpreter
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ypujante/tmp/gtestcrash/build-unix-makefiles

> cmake --build . --verbose
/Applications/CMake.app/Contents/bin/cmake -S/Users/ypujante/tmp/gtestcrash -B/Users/ypujante/tmp/gtestcrash/build-unix-makefiles --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles /Users/ypujante/tmp/gtestcrash/build-unix-makefiles//CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f googletest/googletest/CMakeFiles/gtest.dir/build.make googletest/googletest/CMakeFiles/gtest.dir/depend
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ypujante/tmp/gtestcrash /Users/ypujante/tmp/googletest/googletest /Users/ypujante/tmp/gtestcrash/build-unix-makefiles /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest.dir/DependInfo.cmake --color=
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest.dir/DependInfo.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest.dir/depend.internal".
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest.dir/depend.internal".
Scanning dependencies of target gtest
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f googletest/googletest/CMakeFiles/gtest.dir/build.make googletest/googletest/CMakeFiles/gtest.dir/build
[ 16%] Building CXX object googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/Users/ypujante/tmp/googletest/googletest/include -I/Users/ypujante/tmp/googletest/googletest -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk  -DGTEST_HAS_PTHREAD=1   -std=c++17 -o CMakeFiles/gtest.dir/src/gtest-all.cc.o -c /Users/ypujante/tmp/googletest/googletest/src/gtest-all.cc
[ 33%] Linking CXX static library ../../lib/libgtest.a
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/CMake.app/Contents/bin/cmake -P CMakeFiles/gtest.dir/cmake_clean_target.cmake
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/gtest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc ../../lib/libgtest.a CMakeFiles/gtest.dir/src/gtest-all.cc.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib ../../lib/libgtest.a
[ 33%] Built target gtest
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f googletest/googletest/CMakeFiles/gtest_main.dir/build.make googletest/googletest/CMakeFiles/gtest_main.dir/depend
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ypujante/tmp/gtestcrash /Users/ypujante/tmp/googletest/googletest /Users/ypujante/tmp/gtestcrash/build-unix-makefiles /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest_main.dir/DependInfo.cmake --color=
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest_main.dir/DependInfo.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest_main.dir/depend.internal".
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest/CMakeFiles/gtest_main.dir/depend.internal".
Scanning dependencies of target gtest_main
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f googletest/googletest/CMakeFiles/gtest_main.dir/build.make googletest/googletest/CMakeFiles/gtest_main.dir/build
[ 50%] Building CXX object googletest/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -isystem /Users/ypujante/tmp/googletest/googletest/include -isystem /Users/ypujante/tmp/googletest/googletest -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk  -DGTEST_HAS_PTHREAD=1   -std=c++17 -o CMakeFiles/gtest_main.dir/src/gtest_main.cc.o -c /Users/ypujante/tmp/googletest/googletest/src/gtest_main.cc
[ 66%] Linking CXX static library ../../lib/libgtest_main.a
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/CMake.app/Contents/bin/cmake -P CMakeFiles/gtest_main.dir/cmake_clean_target.cmake
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/googletest/googletest && /Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/gtest_main.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc ../../lib/libgtest_main.a CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib ../../lib/libgtest_main.a
[ 66%] Built target gtest_main
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/gtestcrash_test.dir/build.make CMakeFiles/gtestcrash_test.dir/depend
cd /Users/ypujante/tmp/gtestcrash/build-unix-makefiles && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ypujante/tmp/gtestcrash /Users/ypujante/tmp/gtestcrash /Users/ypujante/tmp/gtestcrash/build-unix-makefiles /Users/ypujante/tmp/gtestcrash/build-unix-makefiles /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles/gtestcrash_test.dir/DependInfo.cmake --color=
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles/gtestcrash_test.dir/DependInfo.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles/gtestcrash_test.dir/depend.internal".
Dependee "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles/gtestcrash_test.dir/depend.internal".
Scanning dependencies of target gtestcrash_test
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/gtestcrash_test.dir/build.make CMakeFiles/gtestcrash_test.dir/build
[ 83%] Building CXX object CMakeFiles/gtestcrash_test.dir/test.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -isystem /Users/ypujante/tmp/googletest/googletest/include -isystem /Users/ypujante/tmp/googletest/googletest -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=gnu++17 -o CMakeFiles/gtestcrash_test.dir/test.cpp.o -c /Users/ypujante/tmp/gtestcrash/test.cpp
[100%] Linking CXX executable gtestcrash_test
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/gtestcrash_test.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/gtestcrash_test.dir/test.cpp.o -o gtestcrash_test  lib/libgtest_main.a lib/libgtest.a
/Applications/CMake.app/Contents/bin/cmake -D TEST_TARGET=gtestcrash_test -D TEST_EXECUTABLE=/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/gtestcrash_test -D TEST_EXECUTOR= -D TEST_WORKING_DIR=/Users/ypujante/tmp/gtestcrash/build-unix-makefiles -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D NO_PRETTY_TYPES=FALSE -D NO_PRETTY_VALUES=FALSE -D TEST_LIST=gtestcrash_test_TESTS -D CTEST_FILE=/Users/ypujante/tmp/gtestcrash/build-unix-makefiles/gtestcrash_test[1]_tests.cmake -D TEST_DISCOVERY_TIMEOUT=5 -D TEST_XML_OUTPUT_DIR= -P /Applications/CMake.app/Contents/share/cmake-3.19/Modules/GoogleTestAddTests.cmake
[100%] Built target gtestcrash_test
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/ypujante/tmp/gtestcrash/build-unix-makefiles/CMakeFiles 0

> ./gtestcrash_test
Running main() from /Users/ypujante/tmp/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Fft
[ RUN      ] Fft.compute
[       OK ] Fft.compute (0 ms)
[----------] 1 test from Fft (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

> otool -L gtestcrash_test
gtestcrash_test:
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1300.23.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)

Now the Xcode generator:

> cmake .. -G Xcode
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin/python3.8 (found version "3.8.9") found components: Interpreter
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ypujante/tmp/gtestcrash/build-xcode
> cmake --build . --verbose
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project gtestcrash.xcodeproj build -target ALL_BUILD -parallelizeTargets -configuration Debug -hideShellScriptEnvironment

User defaults from command line:
    HideShellScriptEnvironment = YES
    IDEPackageSupportUseBuiltinSCM = YES

note: Using new build system
note: Planning
Analyze workspace

Create build description
Build description signature: 82ceecaa08ed2eafc8d1dd0b3fbb0661
Build description path: /Users/ypujante/tmp/gtestcrash/build-xcode/build/XCBuildData/82ceecaa08ed2eafc8d1dd0b3fbb0661-desc.xcbuild

note: Build preparation complete
note: Building targets in dependency order
CreateBuildDirectory /Users/ypujante/tmp/gtestcrash/build-xcode
    cd /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.xcodeproj
    builtin-create-build-directory /Users/ypujante/tmp/gtestcrash/build-xcode

CreateBuildDirectory /Users/ypujante/tmp/gtestcrash/build-xcode/Debug
    cd /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.xcodeproj
    builtin-create-build-directory /Users/ypujante/tmp/gtestcrash/build-xcode/Debug

CreateBuildDirectory /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest
    cd /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.xcodeproj
    builtin-create-build-directory /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest

CreateBuildDirectory /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug
    cd /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.xcodeproj
    builtin-create-build-directory /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/ZERO_CHECK.build/Script-3C6B370C86AB42CE912522CA.sh (in target 'ZERO_CHECK' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/ZERO_CHECK.build/Script-3C6B370C86AB42CE912522CA.sh

PhaseScriptExecution Generate\ CMakeFiles/ZERO_CHECK /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/ZERO_CHECK.build/Script-3C6B370C86AB42CE912522CA.sh (in target 'ZERO_CHECK' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /bin/sh -c /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/ZERO_CHECK.build/Script-3C6B370C86AB42CE912522CA.sh
make: `/Users/ypujante/tmp/gtestcrash/build-xcode/CMakeFiles/cmake.check_cache' is up to date.

CompileC /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest-all.o /Users/ypujante/tmp/googletest/googletest/src/gtest-all.cc normal arm64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target 'gtest' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -target arm64-apple-macos12.3 -fmessage-length\=225 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -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/MacOSX12.3.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -g -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/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/include -I/Users/ypujante/tmp/googletest/googletest/include -I/Users/ypujante/tmp/googletest/googletest -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/DerivedSources-normal/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/DerivedSources/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/DerivedSources -F/Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug -DGTEST_HAS_PTHREAD\=1 -std\=c++17 -MMD -MT dependencies -MF /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest-all.d --serialize-diagnostics /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest-all.dia -c /Users/ypujante/tmp/googletest/googletest/src/gtest-all.cc -o /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest-all.o

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest.LinkFileList (in target 'gtest' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest.LinkFileList

CompileC /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.o /Users/ypujante/tmp/googletest/googletest/src/gtest_main.cc normal arm64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target 'gtest_main' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -target arm64-apple-macos12.3 -fmessage-length\=225 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -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/MacOSX12.3.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -g -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/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/include -isystem /Users/ypujante/tmp/googletest/googletest/include -isystem /Users/ypujante/tmp/googletest/googletest -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/DerivedSources-normal/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/DerivedSources/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/DerivedSources -F/Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug -DGTEST_HAS_PTHREAD\=1 -std\=c++17 -MMD -MT dependencies -MF /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.d --serialize-diagnostics /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.dia -c /Users/ypujante/tmp/googletest/googletest/src/gtest_main.cc -o /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.o

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/DerivedSources/Entitlements.plist (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/DerivedSources/Entitlements.plist

ProcessProductPackaging "" /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/gtestcrash_test.xcent (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash

    Entitlements:

    {
    "com.apple.security.get-task-allow" = 1;
}

    builtin-productPackagingUtility -entitlements -format xml -o /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/gtestcrash_test.xcent

CompileC /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/test.o /Users/ypujante/tmp/gtestcrash/test.cpp normal arm64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -target arm64-apple-macos12.3 -fmessage-length\=225 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -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/MacOSX12.3.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -g -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/ypujante/tmp/gtestcrash/build-xcode/Debug/include -isystem /Users/ypujante/tmp/googletest/googletest/include -isystem /Users/ypujante/tmp/googletest/googletest -I/Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/DerivedSources-normal/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/DerivedSources/arm64 -I/Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/DerivedSources -F/Users/ypujante/tmp/gtestcrash/build-xcode/Debug -std\=gnu++17 -MMD -MT dependencies -MF /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/test.d --serialize-diagnostics /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/test.dia -c /Users/ypujante/tmp/gtestcrash/test.cpp -o /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/test.o

Libtool /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest.a normal (in target 'gtest' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only arm64 -D -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -L/Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug -filelist /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest.LinkFileList -dependency_info /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest.build/Objects-normal/arm64/gtest_libtool_dependency_info.dat -o /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest.a

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.LinkFileList (in target 'gtest_main' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.LinkFileList

Libtool /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest_main.a normal (in target 'gtest_main' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static -arch_only arm64 -D -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -L/Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug -filelist /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main.LinkFileList -dependency_info /Users/ypujante/tmp/gtestcrash/build-xcode/googletest/googletest/gtestcrash.build/Debug/gtest_main.build/Objects-normal/arm64/gtest_main_libtool_dependency_info.dat -o /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest_main.a

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/gtestcrash_test.LinkFileList (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/gtestcrash_test.LinkFileList

Ld /Users/ypujante/tmp/gtestcrash/build-xcode/Debug/gtestcrash_test normal (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -target arm64-apple-macos12.3 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -L/Users/ypujante/tmp/gtestcrash/build-xcode/Debug -F/Users/ypujante/tmp/gtestcrash/build-xcode/Debug -filelist /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/gtestcrash_test.LinkFileList -Xlinker -object_path_lto -Xlinker /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/gtestcrash_test_lto.o -Xlinker -no_deduplicate -Wl,-search_paths_first -Wl,-headerpad_max_install_names /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest_main.a /Users/ypujante/tmp/gtestcrash/build-xcode/lib/Debug/libgtest.a -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Objects-normal/arm64/gtestcrash_test_dependency_info.dat -o /Users/ypujante/tmp/gtestcrash/build-xcode/Debug/gtestcrash_test

WriteAuxiliaryFile /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Script-B688B0A3A57347DB9D2DAEF8.sh (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    write-file /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Script-B688B0A3A57347DB9D2DAEF8.sh

PhaseScriptExecution CMake\ PostBuild\ Rules /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Script-B688B0A3A57347DB9D2DAEF8.sh (in target 'gtestcrash_test' from project 'gtestcrash')
    cd /Users/ypujante/tmp/gtestcrash
    /bin/sh -c /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Script-B688B0A3A57347DB9D2DAEF8.sh
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:77 (message):
  Error running test executable.

    Path: '/Users/ypujante/tmp/gtestcrash/build-xcode/Debug/gtestcrash_test'
    Result: Subprocess killed
    Output:


Call Stack (most recent call first):
  /Applications/CMake.app/Contents/share/cmake-3.19/Modules/GoogleTestAddTests.cmake:173 (gtest_discover_tests_impl)


Command PhaseScriptExecution failed with a nonzero exit code

** BUILD FAILED **


The following build commands failed:
	PhaseScriptExecution CMake\ PostBuild\ Rules /Users/ypujante/tmp/gtestcrash/build-xcode/gtestcrash.build/Debug/gtestcrash_test.build/Script-B688B0A3A57347DB9D2DAEF8.sh (in target 'gtestcrash_test' from project 'gtestcrash')
(1 failure)

> ./Debug/gtestcrash_test
[1]    86167 killed     ./Debug/gtestcrash_test

>otool -L ./Debug/gtestcrash_test
./Debug/gtestcrash_test:
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1300.23.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)

Note that the XCode build fails because it tries to execute the test (probably to discover which tests are available due to gtest_discover_tests) but it still builds the test executable

Not sure, there are a few differences going on in the build the compiler driver is different:

Also, the xcode one uses gnu++17. I guess if I had access to the machine I would run the command lines by hand since it is one file and two compile lines (one to build the .o and one to link the main). Then when I would remove flags until I find the one that is causing the trouble. Also you could try running in gdb to see where it crashes. My guess is that one of the flags in the xcode one is pulling in a different system library or ABI that is not compatible with the gtest you are linking against.

Thank you for the feedback. I guess I can try to remove options and figure out where the problem is, but clearly the problem is not in my code, nor in the way I use CMake. So in your opinion, who should be in charge of this issue? Is it GoogleTest? Is it CMake?

There is clearly something going on here and I want to, at the very minimum, report it to the proper channel…

Well, I actually have not seen your CMakeLists.txt code. It is possible the issue is there. My guess is that it has something to do with the gtest you are using. Is that compiled? Where did it come from?

The CMakeLists.txt is included in my original post. GoogleTest is a clone of the github repo (GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework). Besides the gtest_force_shared_crt option, which makes no difference if you remove it, there is nothing I do on my side.

And I also tried with the latest “official” release (tag v1.11.0) which gives the same result.

Yup, sorry about that… :slight_smile: Seems like a CMake/Xcode issue. If you have the time and you can figure out what flag is causing the trouble that would be helpful. But sounds like you should create a CMake issue to track this.

I suspect this is CMake Issue 21845. On macOS arm64, all binaries have to be signed before running. In local development, the linker adds an ad-hoc placeholder signature. In Xcode however, the ad-hoc signature is delayed until after the “Run Script” build phase, so the POST_BUILD command added by gtest_discover_tests cannot run.

Fortunately, gtest_discover_tests supports delaying test discovery until test time:

-gtest_discover_tests("${target_test}")
+gtest_discover_tests("${target_test}" DISCOVERY_MODE PRE_TEST)

I confirm that this change actually fixes the issue!

Should I still open a CMake issue for it?

No, the issue I linked is sufficient, thanks.

Thanks Brad.

For reference, this is how I ended up fixing it in my project.

set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)

which I set only in the case of Apple M1 build with XCode generator.