gsl 究竟是如何实现替代 cblas 链接功能的?

How exactly gsl achieved alternative cblas linking feature?

您知道我如何设计与 gsl 相同的概念以允许用户在各种 cblas 实现之间切换吗?

据我所知,gsl 依赖的 cblas 依赖项应该在 gsl 本身构建时硬编码到 gsl 库中。

2.2.2 Linking with an alternative BLAS library

The following command line shows how you would link the same application with an alternative CBLAS library libcblas.a,

$ gcc example.o -lgsl -lcblas -lm

For the best performance an optimized platform-specific CBLAS library should be used for -lcblas. The library must conform to the CBLAS standard. The ATLAS package provides a portable high-performance BLAS library with a CBLAS interface. It is free software and should be installed for any work requiring fast vector and matrix operations. The following command line will link with the ATLAS library and its CBLAS interface,

$ gcc example.o -lgsl -lcblas -latlas -lm

If the ATLAS library is installed in a non-standard directory use the -L option to add it to the search path, as described above.

For more information about BLAS functions see BLAS Support.

这里没有特殊的概念 – 只是实现相同 API 并具有相同 ABI 的多个库。

换句话说,您编写的程序使用 #include foo.h 中的函数 int do_stuff(char*)Libfoo.so 是一个导出符号 int do_stuff(char*) 的共享库对象,因为它是从包含 int do_stuff(char*).

实现的程序生成的

如果您现在编写第二个库来实现 libfoo 具有的所有相同符号,那么您就可以在 libfoo 的地方使用一些东西。

这就是这里发生的一切。 GSL 使用 BLAS 符号。 BLAS 定义了这些符号的确切含义(阅读:它们的 C 函数签名),因此您可以使用任何您想要的 BLAS 实现。 (假设所有都是用兼容 compilers/linkers 构建的)

Blas 库

gcc -c -Wall -Werror -fpic blas.cpp

gcc -shared -o libblas.so blas.o

GSL 库,它实际上使用了 blas,但我们 link 不使用它

gcc -c -Wall -Werror -fpic gsl.cpp

gcc -shared -o libgsl.so gsl.o

link同时针对 gsl 和 blas 的应用示例

gcc -Wall -o main main.cpp -L../mygsl -lgsl -L../mycblas -lblas