c++ - 是否可以避免每次都链接 gsl 和 openmp?
问题描述
我正在开发自己的共享库,该库严重依赖GSL和 OpenMP
最终用户应该能够将我的库导入 C++ 代码
#include "mylib/mylib.h"
理想情况下用这样简单的东西编译它
g++ myprog.cpp -lmylib
现在,就目前而言,我每次都必须链接 GSL 和 OpenMP 库
g++ myprog.cpp -lgsl -fopenmp -lmylib
否则我得到以下 gsl 错误并且没有使用 OpenMP 线程
g++ -o myprog myprog.cpp -lmlip
/usr/bin/ld: /tmp/ccu7Mpg1.o: undefined reference to symbol 'gsl_vector_subvector'
/usr/local/lib/libgsl.so.25: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'myprog' failed
make: *** [myprog] Error 1
下面是我正在使用的 cmake 文件
find_package(GSL)
find_package(OpenMP)
add_library(mylib SHARED mylib.cpp)
target_link_libraries(mylib PUBLIC ${GSL_LIBRARIES} OpenMP::OpenMP_CXX)
target_compile_options(mylib PUBLIC -O3 -Wall)
install(TARGETS mylib DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(DIRECTORY . DESTINATION ${CMAKE_INSTALL_PREFIX}/include/mylib FILES_MATCHING PATTERN "*.h")
mylib 是否有可能以某种方式链接到 gsl?请注意,我对创建和链接库的概念还很陌生。
GSL 更新
我做了一些研究并制作了以下片段,它解决了链接 GSL 的问题。
add_library (GSL_LIB STATIC IMPORTED)
SET_TARGET_PROPERTIES(GSL_LIB PROPERTIES IMPORTED_LOCATION ${GSL_ROOT_DIR}/lib/libgsl.a)
然而,这种方法似乎不适用于 OpenMP。欢迎任何建议。
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/libgomp.a(team.o): relocation R_X86_64_TPOFF32 against hidden symbol `gomp_tls_data' can not be used when making a shared object
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/libgomp.a(oacc-init.o): relocation R_X86_64_TPOFF32 against hidden symbol `goacc_tls_data' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
解决方案
推荐阅读
- python - 使用 for if break 语句在列表中附加值
- javascript - 我如何渲染反应图标
- python - PyTorch svd() 重建失败
- azure - 在 Azure Devops 中构建失败时以编程方式在 BitBucket 存储库中拒绝拉取请求的方法
- magento2 - Magento 2 core_config_data 表无故丢失错误?
- rust - `.map(f)` 和 `.map(|x| f(x))` 有什么区别?
- javascript - Bootstrap 样式在 create-react-app 的生产版本中消失了
- dart - 如何从构造函数初始化程序列表中的常量构造函数访问属性?
- javascript - 打字稿等待返回 Promise 的异步函数
- c# - 表插入后 C# 和 SQL Server Express 和 DataGridView 更新