Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symbol resolution misbehaior for GCC and Clang on MacOS #158

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions qiskit/providers/aer/backends/wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand Down