Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

c - Linux header file not recognized in Visual Studio 2017 Linux Project

When including a Linux header file, ucontext.h in this case, in a Linux C++ Project on Visual Studio 2017 for my C program, it does not recognize the header file. Even when I include sys/ucontext.h, it does not recognize functions that I should be able to use for a ucontext_t object, such as getContext() and setContext(). Shouldn't I be able to use these functions in a Linux C++ project?

Code I'm writing:

#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, '', 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()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On my Linux system (Debian Jessie) ucontext.h is in usr/include which in turn includes sys/ucontext.h which gcc will find in usr/include/i386-linux-gnu/sys. The first defines the functions getcontext and setcontext. The second defines the data structures ucontext_t etc.

On the Windows host, VCLinux has installed a copy of the second ucontext.h (which defines the data structures) in C:Program Files (x86)Microsoft Visual Studio 14.0VCLinuxincludeusrincludex86_64-linux-gnusys . But the first ucontext.h is not present.

VCLinux/Visual Studio will compile and run this program on the Linux remote:

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

But IntelliSense will not know about the functions getcontext and setcontext or the associated data structures. So you will get little red squiggles under the names and no completion assistance.

You can take a copy of the first ucontext.h and put it in C:Program Files (x86)Microsoft Visual Studio 14.0VCLinuxincludeusrinclude on your Windows host. Then everything will work as it should. And you could raise an issue for the missing header on the VCLinux GitHub site.

Note: Windows paths are for Visual Studio 2015. They will be different for 2017.

Applies to VCLinux 1.0.6.

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

Update 10-Apr-18

Microsoft have addressed the issue of differences in standard include file locations between Linux systems. As explained in this Visual C++ blog post, the headers specific to the GCC setup are copied from the Linux remote and stored on the Windows host on a per-connection basis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...