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