Xcode 15 duplicate libraries warning
Hi!
It turned out that starting with XCode 15 (macOS Sonoma latest), the linker warns about duplicate libraries and so even a simple CMake project fails to compile when -Wl,-fatal_warnings
option is used:
% cat main.cpp
extern "C" int mypow();
int main() {
return mypow();
}
% cat lib.cpp
extern "C"
int mypow() {
return 1;
}
% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(link_test)
# Enabled by default:
link_libraries(m)
add_library(mylib STATIC lib.cpp)
add_executable(link_test main.cpp)
target_link_libraries(link_test PRIVATE mylib -Wl,-fatal_warnings)
configure & build:
% mkdir build && cd build
% cmake ..
-- The C compiler identification is AppleClang 15.0.0.15000040
-- The CXX compiler identification is AppleClang 15.0.0.15000040
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: .../link_test/build
% make VERBOSE=1
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -S.../link_test -B.../link_test/build --check-build-system CMakeFiles/Makefile.cmake 0
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_progress_start .../link_test/build/CMakeFiles .../link_test/build//CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/depend
cd .../link_test/build && /opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_depends "Unix Makefiles" .../link_test .../link_test .../link_test/build .../link_test/build .../link_test/build/CMakeFiles/mylib.dir/DependInfo.cmake "--color="
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/build
[ 25%] Building CXX object CMakeFiles/mylib.dir/lib.cpp.o
/Library/Developer/CommandLineTools/usr/bin/c++ -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -MD -MT CMakeFiles/mylib.dir/lib.cpp.o -MF CMakeFiles/mylib.dir/lib.cpp.o.d -o CMakeFiles/mylib.dir/lib.cpp.o -c .../link_test/lib.cpp
[ 50%] Linking CXX static library libmylib.a
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -P CMakeFiles/mylib.dir/cmake_clean_target.cmake
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_link_script CMakeFiles/mylib.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/ar qc libmylib.a CMakeFiles/mylib.dir/lib.cpp.o
/Library/Developer/CommandLineTools/usr/bin/ranlib libmylib.a
[ 50%] Built target mylib
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/link_test.dir/build.make CMakeFiles/link_test.dir/depend
cd .../link_test/build && /opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_depends "Unix Makefiles" .../link_test .../link_test .../link_test/build .../link_test/build .../link_test/build/CMakeFiles/link_test.dir/DependInfo.cmake "--color="
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/link_test.dir/build.make CMakeFiles/link_test.dir/build
[ 75%] Building CXX object CMakeFiles/link_test.dir/main.cpp.o
/Library/Developer/CommandLineTools/usr/bin/c++ -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -MD -MT CMakeFiles/link_test.dir/main.cpp.o -MF CMakeFiles/link_test.dir/main.cpp.o.d -o CMakeFiles/link_test.dir/main.cpp.o -c .../link_test/main.cpp
[100%] Linking CXX executable link_test
/opt/homebrew/Cellar/cmake/3.27.6/bin/cmake -E cmake_link_script CMakeFiles/link_test.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/link_test.dir/main.cpp.o -o link_test -lm libmylib.a -Wl,-fatal_warnings -lm
ld: warning: ignoring duplicate libraries: '-lm'
ld: fatal warning(s) induced error (-fatal_warnings)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [link_test] Error 1
make[1]: *** [CMakeFiles/link_test.dir/all] Error 2
make: *** [all] Error 2
CMake 3.27.6
Xcode 15
Clang Apple 15.0.0
macOS Sonoma 14.0