【发布时间】:2017-08-18 17:58:47
【问题描述】:
当在我的 C 程序的 Visual Studio 2017 上的 Linux C++ 项目中包含 Linux 头文件 ucontext.h 时,它无法识别头文件。即使我包含 sys/ucontext.h,它也无法识别我应该能够用于 ucontext_t 对象的函数,例如 getContext() and setContext()。我不应该能够在 Linux C++ 项目中使用这些函数吗?
我正在编写的代码:
#include <stddef.h>
#include <string.h>
#include <sys/ucontext.h>
// If I use ucontext.h instead, it gives the error: cannot open source file ucontext.h
//TCB structure
typedef struct TCB_t {
struct TCB_t *next;
struct TCB_t *prev;
ucontext_t context;
} TCB_t;
void init_TCB(TCB_t *tcb, void *function, void *stackP, int stack_size)
{
memset(tcb, '\0', sizeof(TCB_t));
tcb->context.uc_stack.ss_sp = stackP;
tcb->context.uc_stack.ss_size = (size_t)stack_size;
int c = getcontext(tcb->context); // Cannot resolve field getcontext()
}
【问题讨论】:
-
虽然您可能正在使用 C++ 编程或使用 C++ 编译器(看起来不像,为什么还有
c标签?)但函数和结构仍然是 C 函数和结构。 C 中没有成员函数,只有全局范围内的独立非成员函数。我还建议您再次阅读链接参考,并注意函数对 arguments 的作用。 -
啊,是的,我的最后一行错了,我改了。因此,首先,我使用了 c 标签,因为这是我作为 VS17 Linux C++ 项目编写的 C 程序,据我所知,经过一些研究,它将编译为 C 程序。现在,关于我的实际问题,由于 C 中没有成员函数,这是否意味着 Linux C++ 项目不能在 Linux 中包含 C 库?还是我理解错了?
-
getcontext将指向ucontext_t的指针作为参数,而不是ucontext_t作为参数。此外,由于它初始化了结构,您可能应该在调用它之后更改uc_stack。
标签: c linux visual-studio visual-studio-2017