c++11 - SIGINT 未在此范围内声明

标签 c++11 makefile raspbian

背景

我正在尝试为在 Raspberry 3 上运行的 Rasbian 构建示例 REST api 应用程序。我使用了 cpprestsdk .

示例包含以下头文件:

#include <condition_variable>
#include <mutex>
#include <iostream>

static std::condition_variable _condition;
static std::mutex _mutex;

namespace cfx {
    class InterruptHandler {
    public:
        static void hookSIGINT() {
            signal(SIGINT, handleUserInterrupt);        
        }

        static void handleUserInterrupt(int signal){
            if (signal == SIGINT) {
                std::cout << "SIGINT trapped ..." << '\n';
                _condition.notify_one();
            }
        }

        static void waitForUserInterrupt() {
            std::unique_lock<std::mutex> lock { _mutex };
            _condition.wait(lock);
            std::cout << "user has signaled to interrup program..." << '\n';
            lock.unlock();
        }
    };
}

问题

在 MacOS 上编译时没有问题。

但是,在 rasbian 中编译时,我得到 error: 'SIGINT' was not declared in this scope错误。

很明显SIGINT定义 - #define SIGINT 2或类似的 - 在 rasbian 上编译时无法访问。

问题

为什么我在 rasbian 上收到此错误,但在 macOS 上却没有?是不是因为编译器找不到signal.h ?

我确定 include_directories在 CMakeLists.txt 中包含所需的包含路径。

更新

当我手动添加 #include <csignal> 时错误已解决.

最佳答案

你没有包含signal.h。

您包含了一些 C++ 标准库头文件,并且作为对 MacOS 的副作用,这些恰好包含 signal.h。但是,没有指定会发生这种情况,因此您不能依赖它在这些 header 的不同实现中工作。

尝试添加:

#include <signal.h>

在顶部。

关于c++11 - SIGINT 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52294232/

相关文章:

ssh - 连接被拒绝 : ssh to headless Raspberry Pi 3 b+

c++ - SPI 代码不稳定 - RPi 上的 Raspbian (Debian) Linux

c++11 - 取消引用新对象时 move 语义和构造函数

c++ - 了解 SFINAE 示例

c++11 - 使用 std::bind 与 lambda 进行绑定(bind)。它们有多贵?

c++ - 区分具有相同类型和比率的 std::chrono 持续时间?

c - 目标在 Makefile 中的作用是什么?

linux - 要求用户从 root 模式运行 makefile

macos - 如何在 Makefile (-c -g) 中使用分离的编译/链接过程在 gcc/clang 中生成调试信息?

python - raspbian 自定义守护进程手动启动但不启动