如何在C程序中使用静态pthread库(Visual Studio 2008)?

时间:2013-01-25 10:39:36

标签: c visual-studio-2008 pthreads static-libraries static-linking

我得到了以下测试程序:

#include <stdio.h>
#include "pthread.h"


void* test_thread(void *ptr)
{
    printf("In teh thread");
    return NULL;
}

int main(void)
{
    int foo = 1;
    pthread_t t;

    if (0 != pthread_create(&t, NULL, test_thread, (void *)foo)) {
        printf("This was never going to work.");
    }

    while(1)
        ;

    return 0;
}

构建时,我遇到以下错误:

  

1&gt; main.obj:错误LNK2019:未解析的外部符号   函数_main 1&gt; C:\ Users \ rtt.PROLAN \ Downloads \ pthread-win32-master \ Debug \ Majs.exe中引用的_ imp _pthread_create:   致命错误LNK1120:1个未解析的外部

我从这个source构建了静态库。然后我将“pthread_lib.lib”添加到Linker - &gt;在Project属性中输入。并确保该文件位于lib路径中。

知道是什么导致了链接器错误吗?

1 个答案:

答案 0 :(得分:3)

当你静态链接时,你必须在你的应用程序中添加以下行。

#define PTW32_STATIC_LIB