0

I am creating an executable with this:

gcc elliptical.o -Llibs -lhfcal -o elliptical

In my libs sub folder there are:

libhfcal.a  libhfcal.so

files. My purpose is to use dynamic library but still i didnt understand how it choose .so file when i didnt explicitly refer it. I know it tries to use .so because when running executable i got ./elliptical: error while loading shared libraries: libhfcal.so: cannot open shared object file: No such file or directory

Using static library to create executable command as same as dynamics one. I know i need to use LD_LIBRARY_PATH but my question is why compiler picked up dynamics library?

2
  • -lhfcal does exactly that, link to libhfcal.so Commented Apr 10, 2022 at 12:06
  • But i used same command for my .a file and worked. after that i created that .so file for starting dynamic linking. Commented Apr 10, 2022 at 12:11

1 Answer 1

2

From the gcc manual (emphasis mine):

-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.

Sign up to request clarification or add additional context in comments.

1 Comment

wow really appreciate. Now the thing is clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.