macOS: duplicate symbol errors with blender_test

  • Sample of the error:
duplicate symbol 'blender::bke::tests::mat3_vec_to_roll_Rotationmatrix_Test::TestBody()' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'blender::bke::tests::vec_roll_to_mat3_normalized_Rotationmatrix_Test::TestBody()' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'blender::bke::tests::mat3_vec_to_roll_UnitMatrix_Test::TestBody()' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'vtable for blender::bke::tests::mat3_vec_to_roll_Rotationmatrix_Test' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'typeinfo name for blender::bke::tests::mat3_vec_to_roll_Rotationmatrix_Test' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'typeinfo for blender::bke::tests::mat3_vec_to_roll_Rotationmatrix_Test' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
duplicate symbol 'vtable for blender::bke::tests::vec_roll_to_mat3_normalized_Rotationmatrix_Test' in:
    /Users/t/Documents/repositorys/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    ../../../lib/libbf_blenkernel_tests.a(armature_test.cc.o)
diff --git a/tests/gtests/runner/CMakeLists.txt b/tests/gtests/runner/CMakeLists.txt
index 85b80979074..92b65910031 100644
--- a/tests/gtests/runner/CMakeLists.txt
+++ b/tests/gtests/runner/CMakeLists.txt
@@ -66,7 +66,7 @@ elseif(APPLE)
     # We need -force_load for every test library and target_link_libraries will
     # deduplicate it. So explicitly set as linker option for every test lib.
     target_link_libraries(blender_test ${_lib})
-    target_link_options(blender_test PRIVATE "LINKER:-force_load,$<TARGET_FILE:${_lib}>")
+    #target_link_options(blender_test PRIVATE "LINKER:-force_load,$<TARGET_FILE:${_lib}>")
   endforeach()
 endif()
  • Error happens in Debug mode, not in Release mode. Others not tested.
  • I can’t redo the error on macOS 10.14. People who can redo it: @izo @FabianSchempp

update:
I can redo it now with full version (I had previously tested with lite) by running make debug developer ninja

duplicate symbol __ZN7blender3bke5tests32mat3_vec_to_roll_UnitMatrix_Test10test_info_E in:
    /Users/ankitkumar/blender-build/build_darwin_debug/lib/libbf_blenkernel_tests.a(armature_test.cc.o)
    lib/libbf_blenkernel_tests.a(armature_test.cc.o)

Removing force_load may mean the tests are not actually in the binary and do not run. So that would need to be checked before considering this hotfix. And even then the conditions for when this happens are unclear.

I’m not seeing this error when building on macOS 11 and Xcode 12.4 with ARM and both Release/Debug build. The blenkernel test passes. I do see the libraries duplicated in the linker command, but there is no error.

What I guess is happening is that CMake is unable to deduplicate the libraries (as mentioned in the comment), due to one being an absolute and another a relative path.

I think the Xcode version is more likely to be what makes the difference than the macOS version.

I just finished building the full version to test some patch (as opposed to lite + tests where the error didn’t happen) and I can redo the error. make debug developer ninja

That was my guess too. the absolute path is from TARGET_FILE and relative one from CMake. Will check by changing one of them now that I can redo it.

Thanks for creating the topic @ankitm and thanks @brecht for the feedback.
I haven’t started a topic myself because I didn’t update xcode yet. I will report back after updating.

Better diff, which allows linking of blender_test. This extracts dependencies of a library, instead of linking against the library itself + dependencies. There might be nuances in PUBLIC/PRIVATE/INTERFACE that I am missing… so be careful (:

diff --git a/source/blender/io/common/CMakeLists.txt b/source/blender/io/common/CMakeLists.txt
index 7e39af32f11..96dc831112e 100644
--- a/source/blender/io/common/CMakeLists.txt
+++ b/source/blender/io/common/CMakeLists.txt
@@ -59,9 +59,9 @@ if(WITH_GTESTS)
     ../../blenloader
   )
   set(TEST_LIB
-    bf_blenloader_tests
     bf_io_common
   )
   include(GTestTesting)
   blender_add_test_lib(bf_io_common_tests "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB};${TEST_LIB}")
+  add_dependencies(bf_io_common_tests bf_blenloader_tests)
 endif()
diff --git a/tests/gtests/runner/CMakeLists.txt b/tests/gtests/runner/CMakeLists.txt
index 85b80979074..3b12362abbc 100644
--- a/tests/gtests/runner/CMakeLists.txt
+++ b/tests/gtests/runner/CMakeLists.txt
@@ -65,8 +65,12 @@ elseif(APPLE)
   foreach(_lib ${_test_libs})
     # We need -force_load for every test library and target_link_libraries will
     # deduplicate it. So explicitly set as linker option for every test lib.
-    target_link_libraries(blender_test ${_lib})
     target_link_options(blender_test PRIVATE "LINKER:-force_load,$<TARGET_FILE:${_lib}>")
+    get_target_property(DEPENDENCIES ${_lib} INTERFACE_LINK_LIBRARIES)
+    if(DEPENDENCIES)
+      target_link_libraries(blender_test "${DEPENDENCIES}")
+    endif()
   endforeach()
 endif()

Keeping @sybren in loop for bf_blenloader_tests changes.

Interesting read: https://discourse.cmake.org/t/wl-force-link-flags-not-working-correctly/322

1 Like

fixed in https://developer.blender.org/rBdcb2821292f962951e88f146cb304160f21f73da

1 Like

thank you @ankitm for looking into this issue!