# CMake Error: add_executable cannot create target "mytarget" because an imported target with the same name already exists.

- **ID:** `cmake/add-executable-same-name-as-imported-target`
- **Domain:** cmake
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

A previous find_package or add_library call created an IMPORTED target with the same name, or the target is defined in a subdirectory that already imported it.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CMake 3.10 | active | — | — |
| CMake 3.16 | active | — | — |
| CMake 3.20 | active | — | — |
| CMake 3.26 | active | — | — |

## Workarounds

1. **Use a different target name for your executable, e.g., mytarget_exe instead of mytarget. Update all references accordingly. Example: add_executable(mytarget_exe main.cpp) target_link_libraries(mytarget_exe PRIVATE mytarget)** (95% success)
   ```
   Use a different target name for your executable, e.g., mytarget_exe instead of mytarget. Update all references accordingly. Example: add_executable(mytarget_exe main.cpp) target_link_libraries(mytarget_exe PRIVATE mytarget)
   ```
2. **Remove the imported target before creating your own: if(TARGET mytarget) add_library(mytarget ALIAS mytarget_import) endif() then create your target with a different name.** (75% success)
   ```
   Remove the imported target before creating your own: if(TARGET mytarget) add_library(mytarget ALIAS mytarget_import) endif() then create your target with a different name.
   ```
3. **Use a namespace for the imported target (e.g., find_package(MyPackage) creates MyPackage::mytarget). Then your target can be named mytarget without conflict.** (85% success)
   ```
   Use a namespace for the imported target (e.g., find_package(MyPackage) creates MyPackage::mytarget). Then your target can be named mytarget without conflict.
   ```

## Dead Ends

- **** — This works but may break downstream dependencies that expect the original target name. Also, the imported target remains unused. (40% fail)
- **** — If the imported target is needed for linking, removing it causes undefined references. (80% fail)
- **** — add_library also fails with the same error if the imported target exists. (100% fail)
