# Are the commands "cmake -build . --target package" and "cpack ." equivalent?

**URL:** https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612
**Category:** Usage
**Created:** [February 11, 2020, 12:14pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612 "2020-02-11T12:14:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jwuttke](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jwuttke](https://discourse.cmake.org/u/jwuttke)
#### Post date: [February 11, 2020, 12:14pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/1 "2020-02-11T12:14:09Z")

</div>

Are the commands “cmake -build . --target package” and “cpack .” fully equivalent?

If yes, why would one ever use the former?

(One possible reason I know: Under Windows, `cpack` is ambiguous, as Chocolatey has an obsolete `cpack.exe`.)

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [February 11, 2020, 3:18pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/2 "2020-02-11T15:18:14Z")

</div>

They are more or less equivalent, yes. The `package` target exists so you can run `make package` or `ninja package`. Running `cmake --build . --target package` does seem a little silly to me… if you’re using the CMake command line tools anyway, you might as well use `cpack` at that point.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 11, 2020, 8:17pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/3 "2020-02-11T20:17:31Z")

</div>

The `cmake --build . --target package` command is logically equivalent to running `cpack`, but there are important differences:

- I think `cmake --build . --target package` will also build the `all` target first, whereas running `cpack` directly won’t. Perhaps @kyle.edwards can confirm this?
- Running `cpack` directly supports many options that allow you to control the behavior (e.g. specifying which package generators to use), whereas `cmake --build . --target package` can only support the defaults set by the project.
- If you want to create a multi-config package, you can do that by invoking `cpack -C ...`. There is no equivalent for `cmake --build . --target package`, which can only build/package a single configuration.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [February 12, 2020, 6:58am UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/4 "2020-02-12T06:58:51Z")

</div>

Yes, `package` is a build-in target in CMake with dependeny to `ALL`.

I mentioned that here yesterday:

> [@Does the CPack IFW Generator always include the module CPackIFW?](https://discourse.cmake.org/t/does-the-cpack-ifw-generator-always-include-the-module-cpackifw/615/4):
>
> Package is automatically dependent on build, so even if you do not build your project and run `cmake --build . --target package` it is going to build your project first.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 16, 2020, 1:01am UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/5 "2020-02-16T01:01:53Z")

</div>

`cpack .` will also run the `install` target on your project, so it also basically has a dependency on `ALL` as well.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [February 19, 2020, 11:30pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/6 "2020-02-19T23:30:12Z")

</div>

Turns out that’s not accurate. `cpack` will run the `cmake_install.cmake` script, it won’t build the `install` target. I did some local testing to confirm that my original comment was correct: `cmake --build . --target package` will also build the `all` target first, whereas running `cpack` directly won’t.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 21, 2020, 3:08pm UTC](https://discourse.cmake.org/t/are-the-commands-cmake-build-target-package-and-cpack-equivalent/612/7 "2020-02-21T15:08:58Z")

</div>

Hmm, guess I was mistaken then. There was definitely some reason I had split the packaging steps out from the main build in our superbuilds, but maybe that was due to looking to support different packaging layouts with a single build tree (where install dirs depend on the packaging format).
