cmake 为什么别名目标Boost::header已经存在同名的?

tzdcorbm  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(58)

我的CMakeLists.txt以以下方式包含boost库:

cmake_minimum_required(VERSION 3.28)

set(BOOST_INCLUDE_LIBRARIES filesystem iostreams log program_options system)
set(BOOST_ENABLE_CMAKE ON)
include(FetchContent)
FetchContent_Declare(
    Boost
    URL https://github.com/boostorg/boost/releases/download/boost-1.83.0/boost-1.83.0.7z
    USES_TERMINAL_DOWNLOAD TRUE
    DOWNLOAD_NO_EXTRACT FALSE
    FIND_PACKAGE_ARGS COMPONENTS ${BOOST_INCLUDE_LIBRARIES}
)
FetchContent_MakeAvailable(Boost)

字符串
这样,我得到以下错误:

CMake Error at _deps/boost-src/libs/headers/CMakeLists.txt:11 (add_library):
  add_library cannot create ALIAS target "Boost::headers" because another
  target with the same name already exists.


我做错了什么?

tmb3ates

tmb3ates1#

我无法复制它:

FROM ubuntu

# Base system
RUN apt-get -y update && apt-get -y install vim build-essential git wget

# Avoid encoding issue extracting Boost archive
RUN apt-get install locales
RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US.UTF-8

# recent enough CMake
RUN wget https://apt.kitware.com/kitware-archive.sh && sh kitware-archive.sh
RUN echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ jammy main' | tee /etc/apt/sources.list.d/kitware.list >/dev/nul
RUN apt-get -y update && apt-get -y install cmake

# build
ADD CMakeLists.txt /
RUN cmake -B build .

字符串
docker build .打印:(点击查看完整分辨率)
x1c 0d1x的数据

DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  4.608kB
Step 1/12 : FROM ubuntu
---> 08d22c0ceb15
Step 2/12 : RUN # Base system
---> Running in 0303f897fedd
Removing intermediate container 0303f897fedd
---> 91d67f9a7b3c
Step 3/12 : RUN apt-get -y update && apt-get -y install vim build-essential git wget
---> Running in 56bfea11e745
Get:1  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy InRelease [270 kB]
Get:2  [http://security.ubuntu.com/ubuntu](http://security.ubuntu.com/ubuntu)  jammy-security InRelease [110 kB]
Get:3  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-updates InRelease [119 kB]
Get:4  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-backports InRelease [109 kB]
Get:5  [http://security.ubuntu.com/ubuntu](http://security.ubuntu.com/ubuntu)  jammy-security/universe amd64 Packages [1036 kB]
Get:6  [http://security.ubuntu.com/ubuntu](http://security.ubuntu.com/ubuntu)  jammy-security/multiverse amd64 Packages [44.0 kB]
Get:7  [http://security.ubuntu.com/ubuntu](http://security.ubuntu.com/ubuntu)  jammy-security/restricted amd64 Packages [1512 kB]
Get:8  [http://security.ubuntu.com/ubuntu](http://security.ubuntu.com/ubuntu)  jammy-security/main amd64 Packages [1282 kB]
Get:9  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy/restricted amd64 Packages [164 kB]
Get:10  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy/main amd64 Packages [1792 kB]
Get:11  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy/universe amd64 Packages [17.5 MB]
Get:12  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy/multiverse amd64 Packages [266 kB]
Get:13  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-updates/main amd64 Packages [1576 kB]
Get:14  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-updates/multiverse amd64 Packages [49.8 kB]
Get:15  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-updates/universe amd64 Packages [1304 kB]
Get:16  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-updates/restricted amd64 Packages [1572 kB]
Get:17  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-backports/universe amd64 Packages [32.6 kB]
Get:18  [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)  jammy-backports/main amd64 Packages [78.3 kB]
Fetched 28.8 MB in 1s (19.8 MB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu bzip2 ca-certificates cpp
cpp-11 dirmngr dpkg-dev fakeroot fontconfig-config fonts-dejavu-core g++
g++-11 gcc gcc-11 gcc-11-base gcc-12-base git-man gnupg gnupg-l10n
gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server gpgconf gpgsm less
...
...
...
Removing intermediate container a9c1b3a445ba
---> 8a0ee04eab28
Step 11/12 : ADD CMakeLists.txt /
---> 567d070ea4f1
Step 12/12 : RUN cmake -B build .
---> Running in 0d12728eab7c
CMake Warning (dev) in CMakeLists.txt:
No project() command is present.  The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command.  Add a line of
code such as

project(ProjectName)

 

near the top of the file, but after cmake_minimum_required().

CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Warning (dev) in CMakeLists.txt:
cmake_minimum_required() should be called prior to this top-level project()
call.  Please see the cmake-commands(7) manual for usage documentation of
both commands.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost: static libraries, MPI OFF, Python OFF, testing OFF
-- Boost: libraries included: filesystem;iostreams;log;program_options;system
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE

-- Performing Test BOOST_ATOMIC_TARGET_X86
-- Performing Test BOOST_ATOMIC_TARGET_X86 - Success
-- Performing Test BOOST_ATOMIC_COMPILER_HAS_SSE2
-- Performing Test BOOST_ATOMIC_COMPILER_HAS_SSE2 - Success
-- Performing Test BOOST_ATOMIC_COMPILER_HAS_SSE41
-- Performing Test BOOST_ATOMIC_COMPILER_HAS_SSE41 - Success
-- Boost.Context: architecture x86_64, binary format elf, ABI sysv, assembler gas, suffix .S, implementation fcontext
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/cc
-- Performing Test BOOST_FILESYSTEM_HAS_INIT_PRIORITY
-- Performing Test BOOST_FILESYSTEM_HAS_INIT_PRIORITY - Success
-- Performing Test BOOST_FILESYSTEM_HAS_CXX20_ATOMIC_REF
-- Performing Test BOOST_FILESYSTEM_HAS_CXX20_ATOMIC_REF - Failed
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BLKSIZE
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BLKSIZE - Success
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIM
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIM - Success
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIMENSEC
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIMENSEC - Failed
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIMESPEC
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_MTIMESPEC - Failed
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMENSEC
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMENSEC - Failed
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMESPEC
-- Performing Test BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMESPEC - Failed
-- Performing Test BOOST_FILESYSTEM_HAS_STATX
-- Performing Test BOOST_FILESYSTEM_HAS_STATX - Success
-- Performing Test BOOST_FILESYSTEM_HAS_FDOPENDIR_NOFOLLOW
-- Performing Test BOOST_FILESYSTEM_HAS_FDOPENDIR_NOFOLLOW - Success
-- Performing Test BOOST_FILESYSTEM_HAS_DIRENT_D_TYPE
-- Performing Test BOOST_FILESYSTEM_HAS_DIRENT_D_TYPE - Success
-- Performing Test BOOST_FILESYSTEM_HAS_POSIX_AT_APIS
-- Performing Test BOOST_FILESYSTEM_HAS_POSIX_AT_APIS - Success
-- Boost.Iostreams: ZLIB OFF, BZip2 OFF, LZMA OFF, Zstd OFF
-- Performing Test BOOST_LOG_TARGET_X86
-- Performing Test BOOST_LOG_TARGET_X86 - Success
-- Performing Test BOOST_LOG_HAS_LOCK_FREE_ATOMIC_INT32
-- Performing Test BOOST_LOG_HAS_LOCK_FREE_ATOMIC_INT32 - Success
-- Performing Test BOOST_LOG_COMPILER_HAS_SSSE3
-- Performing Test BOOST_LOG_COMPILER_HAS_SSSE3 - Success
-- Performing Test BOOST_LOG_COMPILER_HAS_AVX2
-- Performing Test BOOST_LOG_COMPILER_HAS_AVX2 - Success
-- Performing Test BOOST_LOG_HAS_NATIVE_SYSLOG
-- Performing Test BOOST_LOG_HAS_NATIVE_SYSLOG - Success
CMake Deprecation Warning at uild/_deps/boost-src/libs/predef/CMakeLists.txt:23 (cmake_minimum_required):
Compatibility with CMake < 3.5 will be removed from a future version of
CMake.

Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.

-- Boost.Thread: threading API is pthread
-- Configuring done (13.9s)
-- Generating done (0.1s)
-- Build files have been written to: /build
Removing intermediate container 0d12728eab7c
---> c5d18a922a01
Successfully built c5d18a922a01

更新

在评论添加context to reproduce问题后,我找到了原因。
原因是FETCHCONTENT_TRY_FIND_PACKAGE_MODE默认为OPT_IN,这意味着find_package被调用,因为FIND_PACKAGE_ARGS存在。这会找到声明::headerstarget的系统Boost包。 有两种方法可以解决这个问题: 1.删除FIND_PACKAGE_ARGS,参考[docs](https://cmake.org/cmake/help/latest/module/FetchContent.html): 此选项适用于FetchContent_MakeAvailable()命令可能首先尝试调用find_package()以满足的依赖关系的情况 1.在内容声明之前将FETCHCONTENT_TRY_FIND_PACKAGE_MODE设置为NEVER`:

SET(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
FetchContent_Declare(
    Boost
    URL https://github.com/boostorg/boost/releases/download/boost-1.83.0/boost-1.83.0.7z
    USES_TERMINAL_DOWNLOAD TRUE
    DOWNLOAD_NO_EXTRACT FALSE
    FIND_PACKAGE_ARGS COMPONENTS ${BOOST_INCLUDE_LIBRARIES}
)
FetchContent_MakeAvailable(Boost)


注意事项:

  • 如果find_package在fetchcontent包填充之前找到Boost,则添加OVERRIDE_FIND_PACKAGE不会执行任何操作-例如,此操作失败:https://coliru.stacked-crooked.com/a/55451090f10cffa7
  • FetchContents_Declare**点处FETCHCONTENT_TRY_FIND_PACKAGE_MODE**的值是重要的

更多的文档在集成FindPackage

相关问题