包括一个C++图书馆进入另一个?

发布于 2025-02-01 08:53:03 字数 1254 浏览 3 评论 0原文

我正在尝试构建一个C ++库,该库将自己使用另一个库。 我想在末尾输出一个.so文件,因此在任何其他项目中都很容易复制和使用。

在此库中,我使用了另一个库Glfw。

现在,我可以很好地创建库,但是当我使用它时,我会收到链接错误,其中未定义GLFW函数。这使我认为GLFW Lib并未被我的图书馆导出。

我已经看过 /a>似乎是一个解决方案,但我给了我很多重复的符号错误。

我是Cmake的初学者,所以Maye很明显我没有看到。这是我的cmakelists.txt:

cmake_minimum_required(VERSION 3.22)
project(MyLib)

set(CMAKE_CXX_STANDARD 23)

# define folders path
get_filename_component(ROOT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
set(HEADER "${ROOT_DIR}/include")
set(SRCS_PATHS  "${ROOT_DIR}/src")
set(TESTS_SRC   "${ROOT_DIR}/tests")
# add dependencies
set(DEP_HEADERS "${ROOT_DIR}/dependencies/GLFW/include")

# set the project sources and headers files
include_directories(${HEADER})
include_directories(${DEP_HEADERS})

set(SRCS [...])


add_library(MyLib SHARED ${SRCS})
# set the project property linker language
set_target_properties(MyLib PROPERTIES LINKER_LANGUAGE CXX)


# target tests
add_executable(window ${TESTS_SRC}/window.cpp)
target_link_libraries(window MyLib)

我已经看到我不是唯一一个有这个问题的人,但是我尝试过的大多数答案都无法正常工作,并导致同样的问题。

I'm trying to build a c++ library, which will be using itself another library.
I would like to output at the end a single .so file, so it is easily copied and used in any other project.

In this library I am using another library, GLFW.

Now, I can create my library fine, but when I am using it I am getting linking errors, where the GLFW functions are not defined. This makes me think that the GLFW lib is not exported with my library.

I've seen this that seemed to be a solution, but i gave me lot of duplicate symbol errors.

I'm quite a beginner with cmake, so maye there is something obvious I'm not seeing. Here is my CMakeLists.txt :

cmake_minimum_required(VERSION 3.22)
project(MyLib)

set(CMAKE_CXX_STANDARD 23)

# define folders path
get_filename_component(ROOT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
set(HEADER "${ROOT_DIR}/include")
set(SRCS_PATHS  "${ROOT_DIR}/src")
set(TESTS_SRC   "${ROOT_DIR}/tests")
# add dependencies
set(DEP_HEADERS "${ROOT_DIR}/dependencies/GLFW/include")

# set the project sources and headers files
include_directories(${HEADER})
include_directories(${DEP_HEADERS})

set(SRCS [...])


add_library(MyLib SHARED ${SRCS})
# set the project property linker language
set_target_properties(MyLib PROPERTIES LINKER_LANGUAGE CXX)


# target tests
add_executable(window ${TESTS_SRC}/window.cpp)
target_link_libraries(window MyLib)

I've seen I'm not the only one with this issue, but most of the answers I've tried won't work and lead to the same problem.

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

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

发布评论

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

评论(1

巴黎夜雨 2025-02-08 08:53:03

根据我可以从您的cmakelists.txt中推论的内容,您应该做这样的事情(我不喜欢供应商,而不是这种方法的专家,所以也许有一些更优雅的东西):

cmake_minimum_required(VERSION 3.20)
project(MyLib)

# glfw static PIC
set(CMAKE_POSITION_INDEPENDENT_CODE_SAVED ${CMAKE_POSITION_INDEPENDENT_CODE})
set(BUILD_SHARED_LIBS_SAVED ${BUILD_SHARED_LIBS})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/GLFW EXCLUDE_FROM_ALL)
set(CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE_SAVED})
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVED})

# MyLib
add_library(MyLib SHARED [...])
target_include_directories(MyLib PUBLIC 
lt;BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)

# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)

但是老实说在Cmakelists中进行所有这些信息是不好的,您应该拥有一名通用的CMakelists并避免供应商:

cmake_minimum_required(VERSION 3.20)
project(MyLib)

# MyLib
find_package(glfw3 REQUIRED)
add_library(MyLib [...])
target_include_directories(MyLib PUBLIC 
lt;BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)

# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)

然后您会决定在建立时间如何构建每个LIB:

// build & install glfw once, as static PIC (glfw is not vendored in MyLib source code here)
cd <glfw_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=<glfw_install_dir>
cmake --build build
cmake --build build --target install

// build MyLib as shared
cd <mylib_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_PREFIX_PATH=<glfw_install_dir>
cmake --build build

From what I can deduce from your CMakeLists.txt, you should do something like this (I don't like vendoring, not an expert of this approach, so maybe there is something more elegant):

cmake_minimum_required(VERSION 3.20)
project(MyLib)

# glfw static PIC
set(CMAKE_POSITION_INDEPENDENT_CODE_SAVED ${CMAKE_POSITION_INDEPENDENT_CODE})
set(BUILD_SHARED_LIBS_SAVED ${BUILD_SHARED_LIBS})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/GLFW EXCLUDE_FROM_ALL)
set(CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE_SAVED})
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVED})

# MyLib
add_library(MyLib SHARED [...])
target_include_directories(MyLib PUBLIC 
lt;BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)

# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)

But honestly it's bad to hardcode all these informations in a CMakeLists, you should have a generic CMakeLists and avoid vendoring:

cmake_minimum_required(VERSION 3.20)
project(MyLib)

# MyLib
find_package(glfw3 REQUIRED)
add_library(MyLib [...])
target_include_directories(MyLib PUBLIC 
lt;BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)

# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)

And then you would decide at build time how to build each lib:

// build & install glfw once, as static PIC (glfw is not vendored in MyLib source code here)
cd <glfw_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=<glfw_install_dir>
cmake --build build
cmake --build build --target install

// build MyLib as shared
cd <mylib_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_PREFIX_PATH=<glfw_install_dir>
cmake --build build
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文