I’d like to begin by acknowledging that there are many other posts discussing this topic. Nevertheless, after a thorough review, I’ve observed that none of them explicitly explain the specifics of my questions issues that I am encountering. I have several questions that I would greatly appreciate if someone could take the time to explaining how these elements function and their significance in more detail.
My first question involves the use of the 4 following commands:
find_library()
find_package()
target_include_directories()
target_link_libraries()
Whenever I research the topic of “Adding third-party libraries”, I consistently come across one of the commands mentioned above. Each response typically addresses how to resolve issues by altering these commands or solving common small developer mistakes. However, none of them dive deeper into the actual functionality of these commands. While I did consult CMake’s documentation on them, I must admit that the documentation isn’t the most user-friendly resource I’ve encountered.
Here’s what I’ve managed to find about these commands. I start off with the ones I understand better (Or at least I hope I understand better):
target_include_directories()
# Specifies the directories that have header files in them. It will add those directories with their header files to the target.
target_link_libraries()
# Links the library files to the final executable, so it can work after being exported.
find_package()
# Find a package and load its package-specific details
find_library()
# This command is used to find a library
Here are the questions that I have about the above:
-
If “target_include_directories” locates the header files (.h files) and “target_link_libraries” identifies and links the necessary libraries to the final executable (.cpp and *.lib files), then the question arises: what purpose does “find_package” serve? (Aren’t header files and their corresponding implementation libraries all that’s required to utilize a library?)
-
What sets apart “find_package” from “find_library”? (Both appear to serve the purpose of locating a library, yet one is labeled as a “package” and the other as a “library.” What advantages and disadvantages do they each bring to the table?)
In addition to the above commands, I’ve been wondering whether there exists a more direct and hands-on approach. Is there a way to explicitly specify the header and library files required for connection to one’s executable and project by giving the full path to each file, rather than depending on these commands? (I’ve noticed substantial variation between libraries – some, like the Boost library, seamlessly integrate with these commands, while others, like SFML, prove to be challenging to work with due to a lack of standardization and frequent architectural changes)