undefined reference to error?
C04dy opened this issue · 4 comments
C04dy commented
i compiled sdl_ttf like how i always compiled other libraries but for some reason sdl_ttf gives me an error like this
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/libs/SDL/lib/libSDL3.a(SDL_dynapi.c.o): in function `get_sdlapi_entry':
SDL_dynapi.c:(.text+0x167c2): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: CMakeFiles/DearCupid.dir/src/Engine/Engine.cpp.o: in function `Engine::EngineStart()':
/home/cody/Dev/Dear-Cupid/src/Engine/Engine.cpp:27:(.text+0x22b): undefined reference to `TTF_Init'
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/src/Engine/Engine.cpp:55:(.text+0x39e): undefined reference to `TTF_OpenFont'
/usr/bin/ld: CMakeFiles/DearCupid.dir/src/Engine/Scene.cpp.o: in function `Scene::Start()':
/home/cody/Dev/Dear-Cupid/src/Engine/Scene.cpp:26:(.text+0x23c): undefined reference to `TTF_OpenFont'
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/src/Engine/Scene.cpp:73:(.text+0x3bf): undefined reference to `TTF_OpenFont'
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/src/Engine/Scene.cpp:95:(.text+0x87f): undefined reference to `TTF_OpenFont'
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/src/Engine/Scene.cpp:38:(.text+0xcc0): undefined reference to `TTF_OpenFont'
/usr/bin/ld: CMakeFiles/DearCupid.dir/src/Engine/Scene.cpp.o:/home/cody/Dev/Dear-Cupid/src/Engine/Scene.cpp:45: more undefined references to `TTF_OpenFont' follow
/usr/bin/ld: CMakeFiles/DearCupid.dir/src/Objects/Text.cpp.o: in function `Text::SetText(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/cody/Dev/Dear-Cupid/src/Objects/Text.cpp:16:(.text+0x59): undefined reference to `TTF_RenderText_Solid'
/usr/bin/ld: /home/cody/Dev/Dear-Cupid/src/Objects/Text.cpp:16:(.text+0x10f): undefined reference to `TTF_RenderText_Solid'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/DearCupid.dir/build.make:244: DearCupid] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/DearCupid.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
i dont know what causing this i compiled the sdl_image the same way i did but didnt get any errors like this
i tried using a static build but that didnt help too
here is my cmakelist file if anyone needs it
cmake_minimum_required(VERSION 3.27.7)
project(DearCupid)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-O3 -Wall)
if (WIN32)
add_library(SDL STATIC IMPORTED)
set_property(TARGET SDL PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/lib/libSDL3.dll.a)
set_target_properties(SDL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/include)
add_library(SDL_image STATIC IMPORTED)
set_property(TARGET SDL_image PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/lib/libSDL3_image.dll.a)
add_library(SDL_TTF STATIC IMPORTED)
set_property(TARGET SDL_TTF PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/lib/libSDL3_ttf.dll.a)
else()
add_library(SDL STATIC IMPORTED)
set_property(TARGET SDL PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/lib/libSDL3.a)
set_target_properties(SDL PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/include)
add_library(SDL_image STATIC IMPORTED)
set_property(TARGET SDL_image PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/SDL/lib/libSDL3_image.a)
add_library(SDL_TTF STATIC IMPORTED)
set_property(TARGET SDL_TTF PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_library(Box2D STATIC IMPORTED)
set_property(TARGET Box2D PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/Box2D/lib/box2d.lib)
set_target_properties(Box2D PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libs/Box2D/include)
else()
add_library(Box2D STATIC IMPORTED)
set_property(TARGET Box2D PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/Box2D/lib/libbox2d.a)
set_target_properties(Box2D PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libs/Box2D/include)
endif()
file(GLOB SRCS src/*.cpp src/Engine/*.cpp src/Objects/*.cpp)
add_executable(${PROJECT_NAME} ${SRCS})
include_directories(${PROJECT_NAME} src ${CMAKE_CURRENT_SOURCE_DIR}/src/Math)
target_link_libraries(${PROJECT_NAME} SDL SDL_image SDL3_TTF Box2D -static -static-libgcc -static-libstdc++)
madebr commented
Don't add -static
to your link options. -static-libgcc -static-libstdc++
should be enough.
But if you must, you need to:
- disable dynapi in SDL (which requires a code change in SDL/src/dynapi/SDL_dynapi.h)
- build libfreetype (and libharfbuzz) from source as a static library: these are requirements of SDL_ttf
C04dy commented
i tried to compile by removing the -static -static-libgcc -static-libstdc++
because on linux i dont really need them but it didnt work either
madebr commented
Add the path of libfreetype to the link command.
C04dy commented
well that solved the issue thanks