{
  "id": "cmake/add-executable-same-name-as-imported-target",
  "signature": "CMake Error: add_executable cannot create target \"mytarget\" because an imported target with the same name already exists.",
  "signature_zh": "CMake 错误：add_executable 无法创建目标 \"mytarget\"，因为同名导入目标已存在。",
  "regex": "CMake Error: add_executable cannot create target \"[^\"]+\" because an imported target with the same name already exists",
  "domain": "cmake",
  "category": "config_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "之前的 find_package 或 add_library 调用创建了同名导入目标，或者该目标已在子目录中定义并导入。",
  "versions": [
    {
      "version": "CMake 3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CMake 3.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CMake 3.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CMake 3.26",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This works but may break downstream dependencies that expect the original target name. Also, the imported target remains unused.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If the imported target is needed for linking, removing it causes undefined references.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "add_library also fails with the same error if the imported target exists.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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)",
      "success_rate": 0.95,
      "how": "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)",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.75,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a namespace for the imported target (e.g., find_package(MyPackage) creates MyPackage::mytarget). Then your target can be named mytarget without conflict.",
      "success_rate": 0.85,
      "how": "Use a namespace for the imported target (e.g., find_package(MyPackage) creates MyPackage::mytarget). Then your target can be named mytarget without conflict.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "为可执行文件使用不同的目标名称，例如 mytarget_exe 代替 mytarget。相应更新所有引用。例如：add_executable(mytarget_exe main.cpp) target_link_libraries(mytarget_exe PRIVATE mytarget)",
    "在创建自己的目标之前移除导入目标：if(TARGET mytarget) add_library(mytarget ALIAS mytarget_import) endif() 然后使用不同的名称创建目标。",
    "为导入目标使用命名空间（例如 find_package(MyPackage) 创建 MyPackage::mytarget）。那么你的目标可以命名为 mytarget 而不会冲突。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://cmake.org/cmake/help/latest/command/add_executable.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-11-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}