gcc 如何决定在创建可执行文件时使用动态库?

How gcc decide to use dynamics library when creating executable?

我正在创建一个可执行文件:

gcc elliptical.o -Llibs -lhfcal -o elliptical

在我的 libs 子文件夹中有:

libhfcal.a  libhfcal.so

个文件。我的目的是使用动态库,但是当我没有明确引用它时,我仍然不明白它是如何选择 .so 文件的。我知道它尝试使用 .so 因为当 运行 可执行文件时我得到了 ./elliptical: error while loading shared libraries: libhfcal.so: cannot open shared object file: No such file or directory

使用静态库创建可执行命令,与动态库相同。我知道我需要使用 LD_LIBRARY_PATH 但我的问题是为什么编译器选择动态库?

来自 gcc manual(强调我的):

-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.