MadThane
MadThane

Reputation: 23

Why using find_package?

Why using find_package() ?

Why not just

include_directories(./extlibs/include/)
link_directories(./extlibs/lib/)

Then you link with your program:

target_link_libraries(${YOURPROJECT} libA libB)

?

I wonder because I see several reason to NOT use find_package:

Upvotes: 2

Views: 223

Answers (1)

usr1234567
usr1234567

Reputation: 23294

Because I don't have a folder extlib!

Ok, seriously, the setup from your computer is the setup from your computer only. If you want to develop just for your setup or a limited number of systems you can configure accordingly, you don't need CMake and its find_package mechanism. You could also use plain Makefiles or type compile commands to your terminal and use bash history.

find_package is handy to write general, cross-platform configure scripts. I don't even know where headers are installed on Windows systems. But when I write them the way CMake suggests it, it will just work and a Windows user does know how to tell your software where to look for headers, libraries and whatever needs to be done.

Concerning your reasons not use it:

  • The idea is, that more and more libraries offer either FindMyLib.cmake files or even better MyLib-config.cmake files. At least for C++, the number of libraries offering these files is growing.
  • Many basic libraries are so simple that you find their headers and libraries within a dozen lines of CMake code.
  • The author of the software writes his code once, then all users can benefit from it. It might be inconvenient for the author, but if it's working, you barely have to touch it again.
  • One downside: There are conventions how the variables should be named, but they are not enforced and a lot of bad legacy code is still around.

Upvotes: 5

Related Questions