使用外部库时,CMake提示“没有生成目标的规则”。

3

我正在尝试将我的一个程序与libevent库链接。我使用CMake作为构建系统。我的项目结构如下:

my_project
├── CMakeLists.txt
├── README.md
├── build
│   └── Build stuff
└── software
    ├── README.md
    ├── CMakeLists.txt
    ├── include
    ├── libraries
    │   ├── libevent
    │   │   └── CMakeLists.txt
    │   └── anotherlibrary
    │       └── CMakeLists.txt
    ├── prog1
    │   ├── CMakeLists.txt
    ├── prog2
    │   ├── CMakeLists.txt
    └── prog3
        └── CMakeLists.txt

需要链接到libevent的prog1的CMakeList.txt

cmake_minimum_required(VERSION 2.6)
project (prog1)

file(GLOB prog1
    "*.h"
    "*.cpp"
)

include_directories("${PROJECT_INCLUDE_DIR}/libevent/include")

add_executable(${PROJECT_NAME} ${prog1})
target_link_libraries(${PROJECT_NAME} event_core)

但是当我构建项目时,make找不到由libevent构建的库。它搜索的路径是:libraries/libevent/lib/libevent_core.a,这是错误的路径,因为libevent将其库构建在下面的位置:my_project/build/software/libraries/libevent/lib/libevent_core.a 我该如何告诉CMake在那里搜索库呢?我已经在我的CMake文件中添加了以下代码,但这并没有起作用:
    link_directories(/my_project/build/software/libraries/libevent/lib/)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/lib) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/bin)
有没有人有建议呢?

link_directories 接受包含库文件的 directory,而不是该文件的完整路径。因此,应该这样调用命令:link_directories(/my_project/build/software/libraries/libevent/lib) - Tsyvarev
从您的文件结构中看不出这是否是一个CMake项目。显示的 prog1 / CMakeLists.txt 暗示您有多个项目。您能否添加根目录 CMakeLists.txt 文件的内容以进行澄清?我建议只有一个项目,或者如果您不介意多次构建库,请将 prog1 的CMake依赖项包含到其 CMakeList.txt中。像 add_subdirectory(../libraries/libevent libevent) 这样的方式,然后使用 add_dependencies(${PROJECT_NAME} event_core) 使可执行文件依赖于它。CMake 将处理链接部分。 - Florian
@Tsyvarev 这是一个笔误,抱歉。 - Faas
1个回答

5
我通过删除build目录中的内容并在build目录内重新运行cmake ..来自行解决了问题。我认为CMake没有意识到我所做的更改,通过重新构建项目,问题得到解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接