# Build failure in Linux when passing flags in add\_custom\_target

**URL:** https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948
**Category:** Usage
**Tags:** os:linux
**Created:** [January 27, 2024, 2:29pm UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948 "2024-01-27T14:29:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [January 27, 2024, 2:29pm UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/1 "2024-01-27T14:29:34Z")

</div>

Having done some investigations to narrow down the issue, this is a simplified form of [this earlier question](https://discourse.cmake.org/t/confusing-error-messages-when-attempting-linux-build/9869).

Essentially I am creating a custom target where the command line requires some flags to be passed using the “-D” command-line option.

[For what it’s worth, the script that I’m running was generated using “configure\_file”, but the context does not allow me to place the flags directly into the script at this point - hence I still need the command-line option.]

So my add\_custom\_target invocation looks like this:

```auto
add_custom_target(bisonparse ALL
                  COMMAND ${CMAKE_COMMAND} "-DFLAG_NAMES=${PROJECT_FB_FLAG_NAMES}"
                                           "-DFLAGS=${PROJECT_FB_FLAGS}"
                                           -P /build/path/GENERATED/bisonparse.cmake
                  BYPRODUCTS ${GENERATED_SOURCE_FILES} ${GENERATED_HEADER_FILES})

```

In Windows, this works perfectly.

In Linux however, the generated makefile fails to build. I get a peculiar error message, namely:

```auto
CMake Warning:
  No source or binary directory provided. Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.

CMake Error: The source directory "/build/path" does not appear to contain CMakeLists.txt.

```

I can easily get rid of the build failure by commenting-out the two “-D” options, viz.

```auto
add_custom_target(bisonparse ALL
                  COMMAND ${CMAKE_COMMAND} #"-DFLAG_NAMES=${PROJECT_FB_FLAG_NAMES}"
                                           #"-DFLAGS=${PROJECT_FB_FLAGS}"
                                           -P /build/path/GENERATED/bisonparse.cmake
                  BYPRODUCTS ${GENERATED_SOURCE_FILES} ${GENERATED_HEADER_FILES})

```

…but then of course I don’t get the flags that I need.

Any problems I have when using CMake are usually connected with when and where to use quotations, so I’ve tried

```auto
-D"FLAG_NAMES=${PROJECT_FB_FLAG_NAMES}"

```

…and no quotes at all, and I find that these build successfully but no flags actually get passed.

What should I do?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [January 27, 2024, 3:24pm UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/2 "2024-01-27T15:24:33Z")

</div>

First, this should be a custom command, not a target unless you want to tun that everytime.  
Second, you probably need the VERBATIM flag.

---

<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: [January 28, 2024, 4:50am UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/3 "2024-01-28T04:50:37Z")

</div>

> [@EosPengwern](#):
>
> Any problems I have when using CMake are usually connected with when and where to use quotations

You may find the [Quoting In CMake](https://crascit.com/2022/01/25/quoting-in-cmake/) blog article I write a while back to be helpful.

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [January 29, 2024, 11:23pm UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/4 "2024-01-29T23:23:39Z")

</div>

Thank you: the VERBATIM flag was the missing piece.

For what it’s worth, I’m using a custom target rather than a custom command because the situation described in the documentation for add\_custom\_command is applicable here:

“_Do not list the output in more than one independent target that may build in parallel or the instances of the rule may conflict. Instead, use the add\_custom\_target() command to drive the command and make the other targets depend on that one._”

In my case, I have some 30 targets that are entirely independent of one another but which all depend on this one.

Anyway, once again, thank you.

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [January 29, 2024, 11:24pm UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/5 "2024-01-29T23:24:14Z")

</div>

Thank you; that’s very helpful.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [January 30, 2024, 10:37am UTC](https://discourse.cmake.org/t/build-failure-in-linux-when-passing-flags-in-add-custom-target/9948/6 "2024-01-30T10:37:04Z")

</div>

This actually means that that you need a custom target that depends on at least one output of that custom command. Then other targets that need those files need a dependency on this target.
