CMAKE在共享库中使用git-submodule

发布于 2025-02-10 05:10:19 字数 1913 浏览 0 评论 0原文

我尝试将库spdlog添加到dll(.so文件)中。 SPDLOG只是SPDLog库中的GIT子模块。查看文档,建议将其用作静态库。因此,我认为我试图在.so文件中编译它有一个问题。

为了清楚地了解我的工作,这是我的系统的照片:

我的root cmakelists.txt是:

cmake_minimum_required(VERSION 3.23)

# set the project name
project(Test VERSION "0.1.0" LANGUAGES CXX)

configure_file(config/config.h.in config.h)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3")

# Set location for .so files
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")

# Set location for executables
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(Engine)
list(APPEND EXTRA_LIBS Engine)

add_subdirectory(Game)

target_link_libraries(Test PUBLIC ${EXTRA_LIBS})

target_include_directories(Test PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

内部的发动机文件夹:

message(STATUS "BUILD Engine shared library File")

add_subdirectory(spdlog)
add_library(Engine SHARED Log.cpp Log.h)
target_include_directories(Engine
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

target_link_libraries(Engine
spdlog
)
target_include_directories(Engine
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/spdlog/include
          )

内部游戏内部(我认为不相关),

message(STATUS "BUILD .exe File")
# add the executable
add_executable(Test main.cpp)

但是当前,如果我尝试在日志文件中使用SPDLOG,我会有此隐秘错误:

[build] /usr/bin/ld: spdlog/libspdlogd.a(spdlog.cpp.o): relocation R_X86_64_TPOFF32 against `_ZGVZN6spdlog7details2os9thread_idEvE3tid' can not be used when making a shared object; recompile with -fPIC

有人有想法吗?

I try to add the library spdlog to a dll (.so file). The spdlog is just a git submodule from the spdlog library. Looking at the documentation, it's recommended to use it as a static library. So i think i have a problem trying to compile it inside a .so file.

To get a clear view of what i'm doing, here is a pic of my system:
enter image description here

My root CMakeLists.txt is :

cmake_minimum_required(VERSION 3.23)

# set the project name
project(Test VERSION "0.1.0" LANGUAGES CXX)

configure_file(config/config.h.in config.h)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3")

# Set location for .so files
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}")

# Set location for executables
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(Engine)
list(APPEND EXTRA_LIBS Engine)

add_subdirectory(Game)

target_link_libraries(Test PUBLIC ${EXTRA_LIBS})

target_include_directories(Test PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

And inside the Engine folder:

message(STATUS "BUILD Engine shared library File")

add_subdirectory(spdlog)
add_library(Engine SHARED Log.cpp Log.h)
target_include_directories(Engine
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

target_link_libraries(Engine
spdlog
)
target_include_directories(Engine
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/spdlog/include
          )

Inside the Game (not relevant at my opinion)

message(STATUS "BUILD .exe File")
# add the executable
add_executable(Test main.cpp)

But currently, if i try to use the spdlog inside my Log file, i have this cryptic error:

[build] /usr/bin/ld: spdlog/libspdlogd.a(spdlog.cpp.o): relocation R_X86_64_TPOFF32 against `_ZGVZN6spdlog7details2os9thread_idEvE3tid' can not be used when making a shared object; recompile with -fPIC

Does anyone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橪书 2025-02-17 05:10:19

多亏了Fabian在评论中,我

set(CMAKE_POSITION_INDEPENDENT_CODE 1)

刚刚添加SPDLog子目录之前添加了它,并且效果很好。

Thanks to fabian in the comments, I add

set(CMAKE_POSITION_INDEPENDENT_CODE 1)

Just before adding the spdlog subdirectory and it just works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文