4

I am trying to learn how to use libuv. I am on a mac OS X and have the library downloaded and installed. I can compile and run small test programs one only starts a callback loop and then exits since there are no watchers, the other creates an idle watcher and exits when the time runs out.

I have been trying to go through the samples for the file io and have been running into problems. The function prototype for the function to obtain a file descriptor is:

 int uv_fs_open(uv_loop_t* loop, 
                uv_fs_t* req,      // second argument
                const char* path, 
                int flags, 
                int mode, 
                uv_fs_cb cb)

I have cut the sample code down to the min that will still have the error.

#include <stdio.h>
#include <uv.h>

uv_fs_t open_req;

void on_open(uv_fs_t);
void on_read(uv_fs_t);
void on_write(uv_fs_t);

int main(int argc, char **argv) {

    uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open); 
    uv_run(uv_default_loop(), UV_RUN_DEFAULT); 
    return 0;
}


void on_open(uv_fs_t *req) {
    if (req->result != -1) {
        fprintf(stderr, "function called \n");
    }
    else {
        fprintf(stderr, "error opening file: %d\n", req->errorno);
    }
    uv_fs_req_cleanup(req);
}

My declaration of open_req is exactly like the declaration of close_req which appears in the complete code sample and doesn't produce and error. I added the declaration for open_req because the compiler was giving me a "‘open_req’ was not declared in this scope" error. When I added the declaration it changed the error to "invalid conversion from ‘void (*)(uv_fs_t)’ to ‘void (*)(uv_fs_t*)’". I get the same error rather the declaration is inside main or not. If I change the declaration to a pointer declaration

uv_fs_t* open_req; 

then I get the error:

cannot convert uv_fs_t** to uv_fs_t* for argument ‘2’ to 
int uv_fs_open(uv_loop_t*, uv_fs_t*, const char*, int, int, void (*)(uv_fs_t*))

I am trying to go through the actual libuv code to see if I can figure it out but the code is huge, I am using the "An Introduction to libuv" found here: http://nikhilm.github.com/uvbook/ and that is where the sample code came from.

Is there any other source of sample code that I can use to try and figure out this library? Is there anything obvious that I am doing wrong in my declaration? I am not sure where to look for clarification or examples on how to use libuv.

Edit update In the libuv code, in the uv.h file. I find

typedef struct uv_fs_s uv_fs_t;

one of the things I have been looking for in the code is where this struct is actually defined. I have searched using google for typedef and learned a bit about how typedef is used to make the name so that you don't have to type struct every time that you want to declare an instance of the struct. Though some people seem to think it is a terrible practice and others think it is great. Part of what I removed to get a minimum sample as another declaration of a type us_fs_t called close_req. I copied the format for my declaration directly from that one.

I will see what I can find about pointers to functions, I am only vaguely familiar with them and it at least gives me somewhere to start looking.

I have found the function definition for uv_fs_open.

int uv_fs_open(uv_loop_t* loop,
               uv_fs_t* req,
               const char* path,
               int flags,
               int mode,
               uv_fs_cb cb) {
  INIT(OPEN);
  PATH;
  req->flags = flags;
  req->mode = mode;
  POST;
}
4

1 回答 1

4

您声明该函数的原型uv_fs_open

int uv_fs_open(uv_loop_t* loop, 
               uv_fs_t* req, 
               const char* path, 
               int flags, 
               int mode, 
               uv_fs_cb cb)

请注意,第二个参数的类型是uv_fs_t*. 所以当你声明open-rec

uv_fs_t open_req;

然后打电话uv_fs_open()

uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open);

第二个参数 ( &open_req) 获取open_req并创建所需的指针。这应该工作得很好。但是,从您最初的错误消息来看,我认为您没有发布所有相关代码。让我们看看这个错误信息:

从 'void (*)(uv_fs_t)' 到 'void (*)(uv_fs_t*)' 的无效转换</p>

这表明编译器需要一个指向函数的指针,该函数返回 void 并采用单个参数,该参数是指向 a 的指针uv_fs_t。另一方面,您提供了一个指向函数的指针,该函数返回 void 并采用单个类型参数uv_fs_t(即不是指向 的指针uv_fs_t

这表明这open_req是一个函数(或指向函数的指针)。为了进一步帮助您,请使用此函数的声明以及如何初始化open_req变量(如果它是指向函数的指针而不是函数本身的名称)来编辑您的原始问题。

编辑:

再次查看您的问题后,open_req很可能不会导致原始错误。你应该把你的代码改回原来的样子。

另一方面,on_open很可能导致问题。你用原型声明这个函数

void on_open(uv_fs_t);

但定义的第一行是

void on_open(uv_fs_t *req)

从函数定义的第一行删除*,看看会发生什么。

于 2013-03-25T20:47:46.250 回答