From c8d18d4489563b2e5dfb90a63d3d7fd02825f96a Mon Sep 17 00:00:00 2001 From: Juan Gomez Date: Thu, 18 Apr 2019 23:51:14 +0200 Subject: [PATCH] Fix symbol resolution misbehaior for GCC and Clang on MacOS --- .../aer/backends/wrappers/CMakeLists.txt | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/qiskit/providers/aer/backends/wrappers/CMakeLists.txt b/qiskit/providers/aer/backends/wrappers/CMakeLists.txt index 812139929f..2c5baf4c32 100644 --- a/qiskit/providers/aer/backends/wrappers/CMakeLists.txt +++ b/qiskit/providers/aer/backends/wrappers/CMakeLists.txt @@ -10,6 +10,23 @@ find_package(PythonLibs REQUIRED) # shared library. string(REPLACE " -static " "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +# Set some general flags +if(APPLE) + message(STATUS "On Mac, we force linking with undefined symbols for Python library, they will be + solved at runtime by the loader") + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(AER_LINKER_FLAGS "-undefined dynamic_lookup") + else() + # -flat_namespace linker flag is needed otherwise dynamic symbol resolution doesn't work as expected with GCC. + # Symbols with the same name exist in different .so, so the loader just takes the first one it finds, + # which is usually the one from the first .so loaded. + # See: Two-Leve namespace symbol resolution + set(AER_LINKER_FLAGS "-undefined dynamic_lookup -flat_namespace") + endif() + unset(PYTHON_LIBRARIES) +endif() + + # QASM Controller add_cython_target(qasm_controller_wrapper qasm_controller_wrapper.pyx CXX) @@ -25,12 +42,7 @@ target_include_directories(qasm_controller_wrapper PRIVATE ${AER_SIMULATOR_CPP_EXTERNAL_LIBS} PRIVATE ${PYTHON_INCLUDE_DIRS}) -if(APPLE) - message(STATUS "On Mac, we force linking with undefined symbols for Python library, they will be - solved at runtime by the loader") - set_target_properties(qasm_controller_wrapper PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -flat_namespace") - unset(PYTHON_LIBRARIES) -endif() +set_target_properties(qasm_controller_wrapper PROPERTIES LINK_FLAGS ${AER_LINKER_FLAGS}) target_link_libraries(qasm_controller_wrapper ${AER_LIBRARIES} @@ -56,16 +68,7 @@ target_include_directories(statevector_controller_wrapper PRIVATE ${AER_SIMULATOR_CPP_EXTERNAL_LIBS} PRIVATE ${PYTHON_INCLUDE_DIRS}) -if(APPLE) - message(STATUS "On Mac, we force linking with undefined symbols for Python library, they will be - solved at runtime by the loader") - # -flat_namespace linker flag is needed otherwise dynamic symbol resolution doesn't work as expected. - # Symbols with the same name exist in different .so, so the loader just takes the first one it finds, - # which is usually the one from the first .so loaded. - # See: Two-Leve namespace symbol resolution - set_target_properties(statevector_controller_wrapper PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -flat_namespace") - unset(PYTHON_LIBRARIES) -endif() +set_target_properties(statevector_controller_wrapper PROPERTIES LINK_FLAGS ${AER_LINKER_FLAGS}) target_link_libraries(statevector_controller_wrapper ${AER_LIBRARIES} @@ -91,12 +94,7 @@ target_include_directories(unitary_controller_wrapper PRIVATE ${AER_SIMULATOR_CPP_EXTERNAL_LIBS} PRIVATE ${PYTHON_INCLUDE_DIRS}) -if(APPLE) - message(STATUS "On Mac, we force linking with undefined symbols for Python library, they will be - solved at runtime by the loader") - set_target_properties(unitary_controller_wrapper PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -flat_namespace") - unset(PYTHON_LIBRARIES) -endif() +set_target_properties(unitary_controller_wrapper PROPERTIES LINK_FLAGS ${AER_LINKER_FLAGS}) target_link_libraries(unitary_controller_wrapper ${AER_LIBRARIES}