首页 > 解决方案 > CMake:另一个具有相同名称的目标存在于一个链接库中

问题描述

完全是 CMake 的新手。我收到以下错误:add_library cannot create target "hello-pack" because another target with the same name already exists.

我的目录结构是这样的:

目录结构

我的应用程序使用两个库:hello-packhello-world-pack

hello-world-pack也依赖于hello-pack来提供其功能。

myapp2 CMakeLists.txt 文件内容如下:

cmake_minimum_required(VERSION 3.4.1)

project("myapp2" C)

add_subdirectory("hello-pack")
add_subdirectory("hello-world-pack")

add_executable("${PROJECT_NAME}" myapp2.c)
target_include_directories("${PROJECT_NAME}" 
    PRIVATE "hello-pack/include" 
    PRIVATE "hello-world-pack/include")
target_link_libraries("${PROJECT_NAME}" "hello-pack" "hello-world-pack")
target_link_directories("${PROJECT_NAME}" 
PRIVATE "hello-pack"
PRIVATE "hello-world-pack")

这是此代码的 git 存储库: https ://github.com/anuragvohraec/myapp2.git

hello-pack 打印出来:hello!

你好包代码

和 hello world 打印出来:hello! world!

在此处输入图像描述

我想模拟一种情况,在我的主应用程序和依赖库中使用通用包进行构建。

标签: ccmake

解决方案


推荐阅读