View Single Post
Old 15th January 2021, 00:18   #7960  |  Link
LoRd_MuldeR
Software Developer
 
LoRd_MuldeR's Avatar
 
Join Date: Jun 2005
Location: Last House on Slunk Street
Posts: 13,248
Quote:
Originally Posted by LigH View Post
Which are these "build files"?
If a C/C++ application depends on library 'XYZ', the you will need to do two things:
  • At compile-time, you need to ensure that the required header files (usually .h or .hpp) for library XYZ are present in one of the directories that the compiler scans for header files.
    Additional search directories for header files can be specified by adding the -I option to the CFLAGS.
  • At link-time, you need to ensure that library XYZ is actually linked to your binary. This is done by adding the -l option your LDFLAGS.
    Note that -l automatically adds the "lib..." prefix as well as the file extension. So, in order to link against libXYZ.a, you just need to write -lXYZ.

    Furthermore, you have to make sure that the library file (usually .a) is present in one of the directories that the linker scans for library files.
    Additional search directories for library files can be specified by adding the -L option to the LDLAGS.

Of course you will need to build library 'XYZ' before you build your "main" application, because otherwise the required library file libXYZ.a (the "build file") has not been created yet

Quote:
Originally Posted by LigH View Post
Undefined reference usually means that symbol is not reachable. For example, there is no library with such names supplied to the linker.
Yes, "undefined reference" could mean that a required library was not linked at all. Could also mean that you are linking a "wrong" version of the library which doesn't match to the header files that were used.

(If you try to link against a library, but the linker couldn't find the requested library file, it would be a different error message)

Another "fun" thing needs to be considered: Libraries can be compiled as either a "static" library (.a) or a "shared" library (.dll). Specifically on Windows, the actual symbol names differ between the "static" and "shared" libraries! Usually the header files support both variants, via #ifdef's, but default often is the "shared" variant. So, a special #define needs to be set, at compile-time, if you intend to link against the "static" library. Could also be the other way around tough
__________________
Go to https://standforukraine.com/ to find legitimate Ukrainian Charities 🇺🇦✊

Last edited by LoRd_MuldeR; 15th January 2021 at 01:06.
LoRd_MuldeR is offline   Reply With Quote