I failed to reproduce your problem. Can you provide a snippet reproducing the problem?
By the way, can you specify your environment (platform, compiler type and version, CMake version)?
Arch Linux
cmake version 3.31.1
ninja 1.12.1
gcc (GCC) 14.2.1 20240910
LLD 18.1.8 (compatible with GNU linkers)
Project to Reproduce
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(test_proj)
include(CheckIPOSupported)
check_ipo_supported(RESULT C_COMPILER_HAS_IPO_SUPPORT LANGUAGES C)
message(STATUS "LTO: ${C_COMPILER_HAS_IPO_SUPPORT}")
add_executable(main main.c)
set_property(TARGET main PROPERTY INTERPROCEDURAL_OPTIMIZATION ${C_COMPILER_HAS_IPO_SUPPORT})
main.c:
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
Command Line Outputs
$ cmake -B build -G Ninja -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -D CMAKE_LINKER_TYPE=LLD
-- The C compiler identification is GNU 14.2.1
-- The CXX compiler identification is GNU 14.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc - 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: /usr/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- LTO: YES
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/tmp.J0m6gKPUMX/build
$ ninja -C build -v
ninja: Entering directory `build'
[1/2] /usr/bin/gcc -flto=auto -fno-fat-lto-objects -MD -MT CMakeFiles/main.dir/main.c.o -MF CMakeFiles/main.dir/main.c.o.d -o CMakeFiles/main.dir/main.c.o -c /tmp/tmp.J0m6gKPUMX/main.c
[2/2] : && /usr/bin/gcc -flto=auto -fno-fat-lto-objects -fuse-ld=lld -Wl,--dependency-file,CMakeFiles/main.dir/link.d CMakeFiles/main.dir/main.c.o -o main && :
FAILED: main
: && /usr/bin/gcc -flto=auto -fno-fat-lto-objects -fuse-ld=lld -Wl,--dependency-file,CMakeFiles/main.dir/link.d CMakeFiles/main.dir/main.c.o -o main && :
ld.lld: error: undefined symbol: main
>>> referenced by /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib/Scrt1.o:(_start)
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The problem comes from your CMake version specified through cmake_minumum_required() command. The policy CMP0138 is not defined, so flags are propagated to the IPO check.
Update cmake_minumum_required() to version to 3.24 or set explicitly CMP0138 to NEW with cmake_policy() command.