Reputation: 77
I am trying to make a portable framework library that works between Linux and Windows, however whenever I compile my code on Linux I keep running into an error:
"/usr/bin/ld: ../extern/SDL2_ttf/libSDL2_ttf.a(SDL_ttf.c.o): relocation R_X86_64_PC32 against symbol `TTF_CloseFont' can not be used when making a shared object; recompile with -fPIC"
This error doesn't occur on Windows only on Linux. I am totally lost on how to fix it as nothing has worked except for removing SHARED from add_library which removed DLLs and makes my code unusable. If you have any ideas on how to fix this please comment below. Thanks in advance!
Main CMakeList.txt
add_subdirectory(extern)
cmake_minimum_required(VERSION 3.7)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(PROJECT_NAME Project)
project(${PROJECT_NAME} VERSION 0.1.0)
add_subdirectory(src)
Source CMakeList.txt
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
file(GLOB EXTERN_SOURCES "${CMAKE_SOURCE_DIR}/extern/stb/include/*.h" "${CMAKE_SOURCE_DIR}/extern/stb/src/*.cpp" )
include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include/Chassis2D" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include/" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2_ttf" PUBLIC "${CMAKE_SOURCE_DIR}/extern/stb/include/")
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADER_FILES} ${EXTERN_SOURCES})
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2 SDL2_ttf box2d soloud)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
External Libraries CMakeList.txt
set(SDL_STATIC OFF)
add_subdirectory(SDL2)
add_library(SDL2::SDL2 ALIAS SDL2)
add_subdirectory(freetype)
add_library(Freetype::Freetype ALIAS freetype)
add_subdirectory(SDL2_ttf)
set(SOLOUD_BACKEND_COREAUDIO OFF)
set(SOLOUD_BACKEND_OPENSLES OFF)
set(SOLOUD_BACKEND_XAUDIO2 OFF)
set(SOLOUD_BACKEND_WINM OFF)
set(SOLOUD_BACKEND_WASAPI OFF)
set(SDL2_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/extern/SDL2/include")
add_subdirectory(SoLoud/contrib)
add_subdirectory(Box2D)
Upvotes: 1
Views: 1206
Reputation: 5510
CMAKE_POSITION_INDEPENDENT_CODE
only influences targets defined after it has been set. However, you're setting CMAKE_POSITION_INDEPENDENT_CODE
to ON
after including the extern
subdirectory, which is why the external libraries are not built with position-independent code.
Upvotes: 2