我尝试使用CMake和GCC(G++)在Ubuntu上构建我的共享库,但每次都出现链接器错误。但是当我把我的库编译成静态库时,它构建得很好。此外,当我在Windows上构建它时,它可以作为共享库和静态库编译得很好。
Linker error看起来像这样:
usr/bin/ld: Dependencies/GLAD/libGLAD.a(gl.c.o): warning: relocation against glad_glNamedFramebufferReadBuffer' in read-only section .text'
/usr/bin/ld: CMakeFiles/Soul-Byte-Engine-Core-Window-Manager-obj.dir/Sources/WindowManager.cpp.o: relocation R_X86_64_PC32 against symbol `_ZSt4cout@@GLIBCXX_3.4' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Soul-Byte-Engine-Core-Window-Manager.dir/build.make:89: libSoul-Byte-Engine-Core-Window-Manager.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:164: CMakeFiles/Soul-Byte-Engine-Core-Window-Manager.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
CMakeLists.txt
我写的是这样的:
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(Soul-Byte-Engine-Core-Window-Manager VERSION 1.0.0 LANGUAGES CXX)
option(BUILD_WINDOW_MANAGER_AS_STATIC "" OFF)
option(BUILD_WINDOW_MANAGER_WITH_GLFW_SOURCES "" OFF)
option(INCLUDE_LOGGING_SYSTEM_SUPPORT "" OFF)
string(TIMESTAMP BuildDate "%Y-%m-%d")
set(BUILT_BY "Unknown" CACHE STRING "The name of build author")
get_directory_property(hasParent PARENT_DIRECTORY)
if(hasParent)
set(WINDOW_MANAGER_MAJOR_VERSION PARENT_SCOPE)
set(WINDOW_MANAGER_MINOR_VERSION PARENT_SCOPE)
set(WINDOW_MANAGER_PATCH_VERSION PARENT_SCOPE)
else()
set(WINDOW_MANAGER_MAJOR_VERSION 1)
set(WINDOW_MANAGER_MINOR_VERSION 0)
set(WINDOW_MANAGER_PATCH_VERSION 0)
endif()
include_directories(Include)
add_library(${PROJECT_NAME}-obj
OBJECT
Sources/WindowManager.cpp
Sources/META.cpp
Include/WindowManager/WindowManager.hpp
Include/WindowManager/META.hpp
)
add_subdirectory(Dependencies/GLAD)
target_include_directories(${PROJECT_NAME}-obj PUBLIC Dependencies/GLAD/include)
if(BUILD_WINDOW_MANAGER_AS_STATIC)
add_library(${PROJECT_NAME}
STATIC
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
)
else()
if(WIN32)
set ( CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
add_library(${PROJECT_NAME}
SHARED
$<TARGET_OBJECTS:${PROJECT_NAME}-obj>
)
endif()
if(WIN32)
if(NOT BUILD_WINDOW_MANAGER_WITH_GLFW_SOURCES)
message(WARNING "When using Windows OS Window Manager will be built with GLFW sources regardless of options !")
set(BUILD_WINDOW_MANAGER_WITH_GLFW_SOURCES ON)
endif()
endif()
if(BUILD_WINDOW_MANAGER_WITH_GLFW_SOURCES)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(Dependencies/GLFW)
target_include_directories(${PROJECT_NAME}-obj PUBLIC Dependencies/GLFW/include)
else()
find_package(glfw3 REQUIRED)
target_include_directories(${PROJECT_NAME}-obj PUBLIC ${GLFW_INCLUDE_DIRS})
endif()
include_directories(${CMAKE_CURRENT_BINARY_DIR}/META)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(BUILT_FOR "${CMAKE_SYSTEM_NAME} x86_64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(BUILT_FOR "${CMAKE_SYSTEM_NAME} x86")
endif()
configure_file(
"META/BuildInfo.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/META/BuildInfo.h"
)
target_link_libraries(${PROJECT_NAME} GLAD glfw)
Git Repo源代码:https://github.com/sp1cklerBoy/Soul-Byte-Engine-Core/tree/main
1条答案
按热度按时间ulydmbyx1#
修好了。我只是使用cmake add_definitions()函数添加了compiler -fPIC标志。
CMakeLists.txt