Skip to content

find_library ignores add_compile_options and add_link_options

find_library behaves as expected when -m32 flag is set with set(CMAKE_C_FLAGS -m32), but not with add_compile_options(-m32) and add_link_options(-m32).

Futhermore, I think the find_librarydocumentation lacks an information that compile flags should be set BEFORE project() call. If you want, I'll open another issue for this specifically.

How to reproduce

To reproduce this behavior, I installed libpng in 64 bits, but not in 32 bits.

$ ll /usr/lib*/libpng.so
lrwxrwxrwx. 1 root root 11 Apr 12  2021 /usr/lib64/libpng.so -> libpng16.so

Correct behavior

cmake_minimum_required(VERSION 3.21)

set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)

project(test C)

find_library(LIBPNG png REQUIRED)
link_libraries(${LIBPNG})
add_executable(foo test.c)

Output:

cmake .. && make
-- The C compiler identification is GNU 8.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:8 (find_library):
  Could not find LIBPNG using the following names: png

Incorrect behavior

cmake_minimum_required(VERSION 3.21)

add_compile_options(-m32)
add_link_options(-m32)

project(test C)

find_library(LIBPNG png REQUIRED)
link_libraries(${LIBPNG})

add_executable(foo test.c)

Output:

rmr * && cmake .. && make VERBOSE=1
-- The C compiler identification is GNU 8.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jerome/cmake/build
/home/jerome/.local/bin/cmake -S/home/jerome/cmake -B/home/jerome/cmake/build --check-build-system CMakeFiles/Makefile.cmake 0
/home/jerome/.local/bin/cmake -E cmake_progress_start /home/jerome/cmake/build/CMakeFiles /home/jerome/cmake/build//CMakeFiles/progress.marks
make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/jerome/cmake/build'
make  -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/depend
make[2]: Entering directory '/home/jerome/cmake/build'
cd /home/jerome/cmake/build && /home/jerome/.local/bin/cmake -E cmake_depends "Unix Makefiles" /home/jerome/cmake /home/jerome/cmake /home/jerome/cmake/build /home/jerome/cmake/build /home/jerome/cmake/build/CMakeFiles/foo.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/jerome/cmake/build'
make  -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/build
make[2]: Entering directory '/home/jerome/cmake/build'
[ 50%] Building C object CMakeFiles/foo.dir/test.c.o
/usr/bin/cc   -m32 -MD -MT CMakeFiles/foo.dir/test.c.o -MF CMakeFiles/foo.dir/test.c.o.d -o CMakeFiles/foo.dir/test.c.o -c /home/jerome/cmake/test.c
[100%] Linking C executable foo
/home/jerome/.local/bin/cmake -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=1
/usr/bin/cc -m32 CMakeFiles/foo.dir/test.c.o -o foo  /usr/lib64/libpng.so
/usr/lib64/libpng.so: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/foo.dir/build.make:98: foo] Error 1
make[2]: Leaving directory '/home/jerome/cmake/build'
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/foo.dir/all] Error 2
make[1]: Leaving directory '/home/jerome/cmake/build'
make: *** [Makefile:91: all] Error 2
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information