Using rabbitmq-c as static library gives error: fatal error: amqp.h: No such file or directory

Using rabbitmq-c as static library gives error: fatal error: amqp.h: No such file or directory

我正在尝试将 rabbitmq-c 构建为静态库,以便我可以在 AWS lambda 中使用。

我能够按照文档构建示例,并且工作正常。

但现在我正在尝试在我自己的项目中使用。 以下是我采取的步骤:

  1. 第三方 目录中克隆 https://github.com/alanxz/rabbitmq-c
  2. 在我的源目录中,我正在使用 cmake 进行构建。

下面是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_BUILD_TYPE "Release")
project(executable LANGUAGES CXX)

SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBS OFF)

set(RABBITMQ_TARGET "<absolute_path_to_third_party_dir>/third_party/rabbitmq-c/")
add_definitions(-DAMQP_STATIC)
add_subdirectory(${RABBITMQ_TARGET})

include_directories(${LIBRABBITMQ_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} execute_code.cpp)
target_link_libraries(executable PRIVATE ${RMQ_LIBRARY_TARGET})


  1. I 运行 cmake .. -DBUILD_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF(没有任何问题)。
  2. 但是当我执行 make 时,它给出了以下错误:
fatal error: amqp.h: No such file or directory
compilation terminated.
CMakeFiles/executable.dir/build.make:62: recipe for target 'CMakeFiles/executable.dir/execute_code.cpp.o' failed
make[2]: *** [CMakeFiles/executable.dir/execute_code.cpp.o] Error 1
CMakeFiles/Makefile2:169: recipe for target 'CMakeFiles/executable.dir/all' failed
make[1]: *** [CMakeFiles/executable.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

这是我的execute_code.cpp(我只是为了这里删除了不必要的代码)

#include<iostream>
#include <amqp.h>
#include <amqp_tcp_socket.h>

using namespace std;

int main(int argc, char** argv) {
  char const *hostname;
  int port, status;
  char const *exchange;
  char const *bindingkey;
  amqp_socket_t *socket = NULL;
  amqp_connection_state_t conn;

  amqp_bytes_t queuename;

  conn = amqp_new_connection();
  socket = amqp_tcp_socket_new(conn);
  return 0;
}

当我不使用时

#include <amqp.h>
#include <amqp_tcp_socket.h>

我能够构建 rabbitmq-static,下面是 make 响应

Scanning dependencies of target rabbitmq-static
[ 35%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_framing.c.o
[ 37%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_api.c.o
[ 40%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_connection.c.o
[ 42%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_mem.c.o
[ 44%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_socket.c.o
[ 46%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_table.c.o
[ 48%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_url.c.o
[ 51%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_tcp_socket.c.o
[ 53%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_time.c.o
[ 55%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_consumer.c.o
[ 57%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_openssl.c.o
[ 60%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_openssl_hostname_validation.c.o
[ 62%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_hostcheck.c.o
[ 64%] Building C object third_party/rabbitmq-c/librabbitmq/CMakeFiles/rabbitmq-static.dir/amqp_openssl_bio.c.o
[ 66%] Linking C static library librabbitmq.a
[ 66%] Built target rabbitmq-static

能够生成librabbitmq.a,所以能够静态构建。

我尝试使用 https://github.com/alanxz/rabbitmq-c/releases/tag/v0.10.0,这是最新的稳定版本。但是还是一样的错误。

我尝试搜索此问题,但找不到任何有用的信息。

我是 cmake 的新手,你能告诉我我做错了什么或者我遗漏了什么吗?

rabbitmq-c 存储库在 rabbitmq-c/librabbitmq 子目录中设置 LIBRABBITMQ_INCLUDE_DIRS 变量,然后还使用 PARENT_SCOPE so it is also available in the top-level rabbitmq-c directory. See that code here:

设置它
set(LIBRABBITMQ_INCLUDE_DIRS
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${SOCKET_IMPL}
    ${MSINTTYPES_INCLUDE}
    )

include_directories(${LIBRABBITMQ_INCLUDE_DIRS})

set(LIBRABBITMQ_INCLUDE_DIRS
    ${LIBRABBITMQ_INCLUDE_DIRS}
    PARENT_SCOPE)

但是,当您通过 add_subdirectory 在项目中包含 rabbitmq-c 存储库时,它会引入一个 额外的 范围,并且 LIBRABBITMQ_INCLUDE_DIRS变量在此父-父范围内不可用。您将必须:

  1. 重新组织您的 CMake 项目,使您的项目位于rabbitmq-c 存储库(尽管这不可扩展,而且可能不是您想要的)。
  2. 修改顶层rabbitmq-c CMake文件进一步扩展这个变量的范围。
  3. 在您的计算机上安装 rabbitmq-c,并使用 CMake Find Module 或 CMake 配置文件(例如 rabbitmq-c-config.cmake) 由 rabbitmq-c 提供以找到已安装的软件。

聚会有点晚,我今天偶然发现了同样的问题。

None Kevin 提出的 3 个解决方案满足我的需要(lib 被声明为子模块,所以我不能修改它,不会安装在生产机器上)

我的解决方案是将 rabbitmq 包含目录添加到链接库的目标:

target_include_directories("${PROJECT_NAME}" PUBLIC include/ path/to/rabbitmq-c/librabbitmq)