Skip to content

Conversation

hjmallon
Copy link
Contributor

@hjmallon hjmallon commented Nov 9, 2023

Prevent target with the same name already exists error

It can easily happen in larger composed CMake projects that the CURL config is included more than once, the normal targets have code to prevent multiple inclusion (in CURLTargets.cmake), but the ALIAS needs a guard as well as it is in CURLConfig.cmake

You can test with a basic CMake

cmake_minimum_required(VERSION 3.27)

project(curl)

set(CURL_DIR "example/lib/cmake/CURL/")
find_package(CURL CONFIG REQUIRED)
find_package(CURL CONFIG REQUIRED) # fails

add_executable(main main.cpp)
target_link_libraries(main CURL::libcurl)

Prevent target with the same name already exists error
@vszakats
Copy link
Member

vszakats commented Nov 9, 2023

Duplicate of #11913

Attempts to reproduce this issue so far failed.

@hjmallon hjmallon closed this Nov 9, 2023
@vszakats
Copy link
Member

vszakats commented Nov 9, 2023

I finally managed to replicate, thanks for your example.

The key seems to be that cmake_minimum_required(VERSION ...) must be 3.18 or higher. Odd behaviour!

@hjmallon
Copy link
Contributor Author

hjmallon commented Nov 9, 2023

I commented on the original issue, this appears to be CMP0107 policy (introduced 3.18)

vszakats pushed a commit that referenced this pull request Nov 10, 2023
Fixes errors on second `find_package(CURL)`. This is a frequent case
with transitive dependencies:
```
CMake Error at ...:
  add_library cannot create ALIAS target "CURL::libcurl" because another
  target with the same name already exists.
```

Test to reproduce:
```cmake
cmake_minimum_required(VERSION 3.27)  # must be 3.18 or higher

project(curl)

set(CURL_DIR "example/lib/cmake/CURL/")
find_package(CURL CONFIG REQUIRED)
find_package(CURL CONFIG REQUIRED)  # fails

add_executable(main main.c)
target_link_libraries(main CURL::libcurl)
```

Ref: https://cmake.org/cmake/help/latest/release/3.18.html#other-changes
Ref: https://cmake.org/cmake/help/v3.18/policy/CMP0107.html
Ref: #12300
Assisted-by: Harry Mallon
Closes #11913
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants