0

我刚开始使用 glfw 来尝试构建游戏。我对 C 和 C++ 还很陌生,但我之前在 android 上使用过 openGL。我已经完成了所有 openGL 的工作,现在开始尝试使用 glfw 制作线程。

这是一些基本的测试代码。它类似于文档中的内容。

#include <GL/glfw.h>
#include <stdio.h>

GLFWthread thread;

void GLFWCALL testThread()
{
    printf("hello\n");
}

int main()
{
    printf("test\n");

    glfwInit();

    thread = glfwCreateThread(testThread, NULL);
    glfwWaitThread(thread, GLFW_WAIT);

    glfwTerminate();
    return 1;   
}

这将在 gcc 中编译并按预期工作。

$ gcc -o glthread glthread.c -lglfw
$ ./glthread
test
hello

问题是我想利用 c++ 功能,比如类是我的游戏。当我在 g++ 中编译时,我得到了这个......

$ g++ -o glthread glthread.c -lglfw
glthread.c: In function ‘int main()’:
glthread.c:18: error: invalid conversion from ‘void (*)()’ to ‘void (*)(void*)’
glthread.c:18: error:   initializing argument 1 of ‘GLFWthread glfwCreateThread(void (*)(void*), void*)’

当我把它放在课堂上时,关键错误会变成这样。

error: argument of type ‘void (Renderer::)()’ does not match ‘void (*)(void*)’

我基本上想知道的是,是否可以在 c++ 中使用 glfw 创建线程,如果可以,如何?

我用于此工作的主要 PC 是 Arch linux 机器。我现在不能给出我的编译器的版本。如果有帮助,我以后可以得到它们。

4

1 回答 1

1
void GLFWCALL testThread()
{
    printf("hello\n");
}

应该接收一个类型的参数void*并且你不能在这里使用类函数,因为指向类函数的指针的签名是Ret (Class::*)(args), not void (*)(void*)。如果您想通过线程使用指向类成员的指针 - 您应该使用更多 C++ 样式库(boost::thread或类似的库,或编写自己的包装器)。

您的示例在 C 中有效,因为在 C 中空括号(即 () )意味着 - 任何类型的任意数量的参数,但在 C++ () 中意味着,该函数根本不应该接收参数。

于 2013-05-15T09:05:44.930 回答