Andrew Cheong
Andrew Cheong

Reputation: 30283

CMake: What is the advantage of using -D (command line) over add_compile_definitions or target_compile_definitions or set?

New to CMake. When adding libraries I see a lot of instructions saying do this:

cmake -D OPENSSL_ROOT_DIR=/usr/local/opt/openssl

Why is that preferred over putting it in the actual CMakeLists.txt? e.g.

set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")

Upvotes: 0

Views: 294

Answers (1)

ixSci
ixSci

Reputation: 13708

  • You do not commit the first one to the repository while you either commit the second one or have to be careful to not commit it with other stuff.
  • The second method is usually used to allow users of some project to modify some aspect of that project. So whatever pertains to the project itself (not customizable) goes straight to the CMake files everything else might be provided from the command line.

Note, also, that the CMake presets add an ability to customize the build outside the CMake files so the method of providing variables directly via a console call is something I'd expect going away and getting replaced with presets. But the way they work is still the same: variables defined in the preset get to CMake cache with generated console call (with an IDE it might be different).

Upvotes: 1

Related Questions