【问题标题】:SIGINT was not declared in this scopeSIGINT 未在此范围内声明
【发布时间】:2019-02-17 00:56:31
【问题描述】:

背景

我正在尝试为在 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

我确保 CMakeLists.txt 中的 include_directories 包含必需的包含路径。

更新

当我手动添加 #include &lt;csignal&gt; 时错误已解决。

【问题讨论】:

  • signal.h 丢失

标签: c++11 makefile cmake raspbian cpprest-sdk


【解决方案1】:

你没有包含signal.h。

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

尝试添加:

#include <signal.h>

在顶部。

【讨论】:

  • 感谢您的建议,我试过了,但仍然出现同样的错误。但是,当我包含 &lt;csignal&gt; 时,错误解决了。如果有人详细说明,我将不胜感激。
【解决方案2】:

在 Linux 上,要包含的头文件是

#include <signal.h>

在 MacOS 上,要包含的等效头文件是

#include <csignal.h>

根据您的操作系统,头文件总是会发生变化。不过他们都应该做同样的事情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2016-08-09
    • 2021-01-07
    • 2016-10-27
    • 2016-06-03
    • 2014-09-11
    相关资源
    最近更新 更多