背景
我正在尝试为在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
错误。
很明显,在rasbian上编译时,SIGINT
定义-#define SIGINT 2
或类似名称是无法达到的。
问题
为什么我在rasbian上出现此错误,但在macOS上却没有?是因为编译器找不到signal.h
吗?
我确保CMakeLists.txt中的include_directories
包含必需的包含路径。
更新
我手动添加#include <csignal>
时错误已解决。
答案 0 :(得分:2)
您还没有包含signal.h。
您要包含一些C ++标准库标头,并且作为MacOS的副作用,它们恰好包含signal.h。但是,没有指定发生这种情况,因此您不能依靠它在这些标头的不同实现中工作。
尝试添加:
#include <signal.h>
在顶部。
答案 1 :(得分:0)
在Linux上,要包含的头文件是
#include <signal.h>
在MacOS上,要包含的等效头文件为
#include <csignal.h>
根据您的操作系统,头文件始终会更改。他们俩虽然应该做同样的事情