Linux Visual Studio 2017 Linux 项目无法识别头文件

Linux header file not recognized in Visual Studio 2017 Linux Project

当包含 Linux 头文件时,ucontext.h 在这种情况下,在 Visual Studio 2017 的 Linux C++ 项目中,我的 C 程序无法识别头文件。即使我包含 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, '[=13=]', 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()
}

在我的 Linux 系统 (Debian Jessie) 中 ucontext.h 位于 usr/include 中,后者又包含 sys/ucontext.hgcc 将在 [=15] 中找到=].第一个定义函数 getcontextsetcontext。第二个定义数据结构ucontext_t

在 Windows 主机上,VCLinux 在 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include\x86_64-linux-gnu\sys 中安装了第二个 ucontext.h(定义数据结构)的副本。但是第一个 ucontext.h 不存在。

VCLinux/Visual Studio 将在 Linux 远程编译和 运行 这个程序:

#include <ucontext.h>
#include <iostream>
int main()
{
   ucontext ucxt;
   ::getcontext (&ucxt);
   std::cout << ucxt.uc_flags << std::endl;
   return 0;
}

但是 IntelliSense 不会知道函数 getcontextsetcontext 或相关的数据结构。因此,您会在名称下方看到红色的小波浪线,并且没有完成帮助。

您可以复制第一个 ucontext.h 并将其放入您的 Windows 主机上的 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\Linux\include\usr\include。然后一切都会正常工作。您可以针对 VCLinux GitHub site.

中缺失的 header 提出问题

注意:Windows 路径适用于 Visual Studio 2015 年。2017 年将有所不同。

适用于 VCLinux 1.0.6.

==============

2018 年 4 月 10 日更新

Microsoft 已经解决了 Linux 系统之间标准包含文件位置差异的问题。如 this Visual C++ blog post 中所述,GCC 设置特定的 header 是从 Linux 远程复制并存储在 Windows 主机上 per-connection 基础.

On this answer to do the following you need Visual Studio Community 2017 15.9.7+ - Tested this solution on Visual Studio Enterprise 2019 Preview 4.

Visual Studio 需要将所有远程 headers 下载到您的本地计算机中,以实现智能感知的正确行为。

新方法 'rsync_ssh' 不会下载全部 headers。您可以通过 sftp_ssh 使用旧方法 .zip。

0. 添加远程连接。
工具->选项->跨平台->连接管理器

1. Select 你的连接 从工具->选项->跨平台->连接管理器->远程 Headers 智能感知管理器进行更新。 接下来单击探索按钮。

2. C:\Users[你的用户]\AppData\Local\Microsoft\Linux\HeaderCache.0[IdNumber] 将 HeaderCache settings.xml.unused 文件重命名为 settings.xml

3. 在 settings.xml 文件中将 syncMethod 更改为 sftp_ssh.

4. 从工具->选项->跨平台->连接管理器->远程 Headers 智能感知管理器更新 headers 缓存。 5.尽情享受吧。

之前

之后