首页 > 解决方案 > 是否可以避免每次都链接 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

标签: c++cmakelinkerg++openmp

解决方案


推荐阅读