ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.64k stars 399 forks source link

Final link failed: Bad value #893

Closed ZeroIntensity closed 1 month ago

ZeroIntensity commented 1 month ago

I've been trying to integrate FTXUI into Python, through scikit-build-core. CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.15...3.26)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C CXX)

# Source code
file(GLOB lumberjack_C_SRC
     ${CMAKE_CURRENT_SOURCE_DIR}/src/_lumberjack/*.c
)
file(GLOB lumberjack_CXX_SRC
    ${CMAKE_CURRENT_SOURCE_DIR}/src/_lumberjack/*.cc
)

# Find Python
find_package(
  Python
  COMPONENTS Interpreter Development.Module
  REQUIRED)

# Install FTXUI
include(FetchContent)
set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
FetchContent_Declare(ftxui
  GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
  GIT_TAG main
)
FetchContent_MakeAvailable(ftxui)

# Link Python
python_add_library(_lumberjack MODULE ${lumberjack_C_SRC} ${lumberjack_CXX_SRC} WITH_SOABI)

# Link FTXUI
target_link_libraries(_lumberjack
  PRIVATE ftxui::screen
  PRIVATE ftxui::dom
  PRIVATE ftxui::component
)

# Add include directories
target_include_directories(_lumberjack PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/)
target_include_directories(_lumberjack PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/lumberjack/)

# Install extension module
install(TARGETS _lumberjack DESTINATION .)

At the end of building, the linker fails with this error:

eps/ftxui-build/libftxui-component.a  _deps/ftxui-build/libftxui-dom.a  _deps/ftxui-build/libftxui-screen.a && :
      /usr/bin/ld: _deps/ftxui-build/libftxui-component.a(event.cpp.o): warning: relocation against `_ZN5ftxui5Event4AltEE' in read-only section `.text.startup'
      /usr/bin/ld: _deps/ftxui-build/libftxui-dom.a(border.cpp.o): relocation R_X86_64_PC32 against symbol `_ZZNSt19_Sp_make_shared_tag5_S_tiEvE5__tag' can not be used when making a shared object; recompile with -fPIC
      /usr/bin/ld: final link failed: bad value

(I'm new-ish to CMake - if I made some obvious mistake please let me know. I haven't run into this issue with any other library before, so there's likely some incompatibility with FTXUI)

ZeroIntensity commented 1 month ago

After a lot of debugging, it seems the fix is to add -mcmodel=large to your compiler flags. In short, that messes with some 32-bit to 64-bit integration (at least, that's my understanding of it). I'm not sure why FTXUI needs this for this case.

ArthurSonzogni commented 1 month ago

Cool stuff! I don't know much about python, outside of minors usages from time to time. So, I might not be useful.

Please don't hesitate to share your projects. We woudl be curious to know how FTXUI is being used.

ZeroIntensity commented 1 month ago

FTXUI is very cool! Generally, the main choice for a TUI, especially in Python, is Textual, but I chose FTXUI for performance, and to get around the GIL (global interpreter lock -- Python multithreading shenanigans, pretty much).

Namely, I'm using FTXUI to power a logging library (which is still private), which will then be used under the hood in a web framework.