# Difference between prefixed EXPORT\_NAME and NAMESPACE?

**URL:** https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251
**Category:** Code
**Created:** [June 3, 2023, 12:38pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251 "2023-06-03T12:38:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![SoilRos](https://discourse.cmake.org/user_avatar/discourse.cmake.org/soilros/32/3502_2.png) [@SoilRos](https://discourse.cmake.org/u/SoilRos)
#### Post date: [June 3, 2023, 12:38pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/1 "2023-06-03T12:38:12Z")

</div>

What is the difference between setting scope of an exported target directly the `EXPORT_NAME` vs setting the scope of an exported target with `NAMESPACE`?

For example, let’s say I have a target named `proj_target` which I want to be consumed as `Proj::Target`. In the first variant I set the scope via the `NAMESPACE` argument in the export sets. This is what is normally advertised in CMake tutorials, so it looks something like this:

```auto
add_library(Proj::Target ALIAS proj_target)
set_target_properties(proj_target PROPERTIES EXPORT_NAME Target)
install(TARGETS proj_target EXPORT proj-export-set DESTINATION ...)
export(EXPORT proj-export-set NAMESPACE Proj)
install(EXPORT proj-export-set NAMESPACE Proj)

```

However, this means that targets with other scopes cannot be exported in the same export set `proj-export-set`. So one has to keep track of a different export sets per namespace. If I want several scopes, I find this restriction quite inconvenient because the target is defined in the same project after all. So I found that one can alternatively set the scope of the target in the `EXPORT_NAME`.

```auto
add_library(Proj::Target ALIAS proj_target)
set_target_properties(proj_target PROPERTIES EXPORT_NAME Proj::Target)
install(TARGETS proj_target EXPORT proj-export-set DESTINATION ...)
export(EXPORT proj-export-set)
install(EXPORT proj-export-set DESTINATION ...)

```

But I could not find any reference whether this usage is correct. I checked the two versions and they seem to generate the same targets file. So, here are my questions:

- Is there anything fundamentally different on just setting the scope in the `EXPORT_NAME` directly? Is there any restriction them that one has to be aware of?
- Is the `NAMESPACE ` argument in the export sets doing something different than a string prefix?

Related:

- [Export targets under the Dune:: namespace (!1247) · Merge requests · Core Modules / dune-common · GitLab](https://gitlab.dune-project.org/core/dune-common/-/merge_requests/1247)
- [[cmake][add\_library] Simplify specification of scoped targets (!1257) · Merge requests · Core Modules / dune-common · GitLab](https://gitlab.dune-project.org/core/dune-common/-/merge_requests/1257)

---

<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: [June 7, 2023, 11:48am UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/2 "2023-06-07T11:48:13Z")

</div>

> [@SoilRos](#):
>
> Is there anything fundamentally different on just setting the scope in the `EXPORT_NAME` directly? Is there any restriction them that one has to be aware of?

I’m…not sure. I think it is just string manipulation. However, other things may want to use the namespace for other reasons (e.g., in VTK these things are controlled and the `NAMESPACE` is used to make in-project aliases of the same pattern as the exported targets). But for core CMake, it seems fine. Any additional behaviors CMake would want for the `NAMESPACE` argument would have to take this into account though.

> [@SoilRos](#):
>
> Is the `NAMESPACE ` argument in the export sets doing something different than a string prefix?

AFAIK, no.

---

<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: [June 7, 2023, 1:17pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/3 "2023-06-07T13:17:20Z")

</div>

Prefer to use `NAMESPACE` rather than putting the namespace in the `EXPORT_NAME`. If and when [issue 22687](https://gitlab.kitware.com/cmake/cmake/-/issues/22687) eventually gets some attention, I’d expect that would attach some additional meaning and behavior to `NAMESPACE`, but not to `EXPORT_NAME`.

---

<div class="post-metadata">

### Author: ![SoilRos](https://discourse.cmake.org/user_avatar/discourse.cmake.org/soilros/32/3502_2.png) [@SoilRos](https://discourse.cmake.org/u/SoilRos)
#### Post date: [June 7, 2023, 2:35pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/4 "2023-06-07T14:35:01Z")

</div>

Thanks for your answers! I am just puzzled that I read two contradictory answers here:

> […] Any additional behaviors CMake would want for the `NAMESPACE` argument would have to take this into account though.

> […] I’d expect that would attach some additional meaning and behavior to `NAMESPACE`, but not to `EXPORT_NAME`.

So this is how I interpret this (please correct me if I am wrong): Prefixed namespaces in `EXPORT_NAME` is “blessed” by the current CMake specification but may not be the case in the future. A proposal like @craig.scott’s would need a new CMake policy to allow the change of that behavior.

---

<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: [June 7, 2023, 2:49pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/5 "2023-06-07T14:49:11Z")

</div>

It’s more like `NAMESPACE` today only has the “prepend to `EXPORT_NAME`”; it may gain more in the future. I don’t think it’s worth a policy as it is not changing behavior, only adding additional semantics to the (preferred) way of getting a desired result.

---

<div class="post-metadata">

### Author: ![SoilRos](https://discourse.cmake.org/user_avatar/discourse.cmake.org/soilros/32/3502_2.png) [@SoilRos](https://discourse.cmake.org/u/SoilRos)
#### Post date: [June 7, 2023, 3:00pm UTC](https://discourse.cmake.org/t/difference-between-prefixed-export-name-and-namespace/8251/6 "2023-06-07T15:00:07Z")

</div>

Great, that clarifies the issue, thanks!
