【问题标题】:Why aren't any of my pthread functions recognized by g++ or clang?为什么 g++ 或 clang 不能识别我的任何 pthread 函数?
【发布时间】:2014-03-26 20:51:10
【问题描述】:

我试过用以下代码编译这个程序:

  • g++ test.cpp
  • g++ -pthread test.cpp
  • g++ -lpthread test.cpp
  • clang -pthread -c test.cpp

他们都告诉我没有定义函数wait(),fork(),exit()。什么给了?

#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <string.h>

void  parse(char *line, char **argv)
{
    while (*line != '\0')    // If not the end of line...
    {
        while (*line == ' ' || *line == '\t' || *line == '\n')
        {
            // Replace white spaces with 0.
            *line++ = '\0';
        }

        // Save the argument position.
        *argv++ = line;

        while ((*line != '\0') && (*line != ' ') &&
               (*line != '\t') && (*line != '\n'))
        {
            // Skip the argument until...
            line++;
        }
    }

    // Mark the end of argument list.
    *argv = '\0';
}

void  execute(char **argv)
{
    pid_t  pid;
    int    status;

    // Fork a child process.
    if ((pid = fork()) < 0)
    {     
        printf("*** ERROR: forking child process failed\n");
        exit(1);
    }
    else if (pid == 0)    // For the child process:
    {    
        // Execute the command.
        if (execvp(*argv, argv) < 0)
        {
            printf("*** ERROR: exec failed\n");
          exit(1);
        }
    }
    else                 // For the parent:
    {                                
        // Wait for completion.
        while (wait(&status) != pid);

    }
}

int  main()
{
    char line[1024];             /* the input line                 */
    char *argv[64];              /* the command line argument      */

    // Repeat until done...
    while (1)
    {
        printf("Shell -> ");     /*   display a prompt             */
        gets(line);              /*   read in the command line     */
        printf("\n");

        parse(line, argv);       /*   parse the line               */

        // Is it an "exit"?
        if (strcmp(argv[0], "exit") == 0)
          exit(0);               /*   exit if it is                */

        execute(argv);           /* otherwise, execute the command */
    }
}

以下是一些错误:

g++ -pthread test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  *argv = '\0';                 /* mark the end of argument list  */
          ^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
  if ((pid = fork()) < 0) {     /* fork a child process           */
             ^
test.cpp:26:5: error: use of undeclared identifier 'exit'
    exit(1);
    ^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
    if (execvp(*argv, argv) < 0) {     /* execute the command  */
        ^
test.cpp:31:7: error: use of undeclared identifier 'exit'
      exit(1);
      ^
test.cpp:35:12: error: use of undeclared identifier 'wait'
    while (wait(&status) != pid)       /* wait for completion  */
           ^
test.cpp:51:7: error: use of undeclared identifier 'exit'
      exit(0);            /*   exit if it is                */
      ^
1 warning and 6 errors generated.
Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ g++ -lpthread test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  *argv = '\0';                 /* mark the end of argument list  */
          ^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
  if ((pid = fork()) < 0) {     /* fork a child process           */
             ^
test.cpp:26:5: error: use of undeclared identifier 'exit'
    exit(1);
    ^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
    if (execvp(*argv, argv) < 0) {     /* execute the command  */
        ^
test.cpp:31:7: error: use of undeclared identifier 'exit'
      exit(1);
      ^
test.cpp:35:12: error: use of undeclared identifier 'wait'
    while (wait(&status) != pid)       /* wait for completion  */
           ^
test.cpp:51:7: error: use of undeclared identifier 'exit'
      exit(0);            /*   exit if it is                */
      ^
1 warning and 6 errors generated.
Tylers-MacBook-Pro:BlastUniversal tylerpfaff$ clang -pthread -c test.cpp
test.cpp:16:11: warning: expression which evaluates to zero treated as a null pointer constant of type 'char *' [-Wnon-literal-null-conversion]
  *argv = '\0';                 /* mark the end of argument list  */
          ^~~~
test.cpp:24:14: error: use of undeclared identifier 'fork'
  if ((pid = fork()) < 0) {     /* fork a child process           */
             ^
test.cpp:26:5: error: use of undeclared identifier 'exit'
    exit(1);
    ^
test.cpp:29:9: error: use of undeclared identifier 'execvp'
    if (execvp(*argv, argv) < 0) {     /* execute the command  */
        ^
test.cpp:31:7: error: use of undeclared identifier 'exit'
      exit(1);
      ^
test.cpp:35:12: error: use of undeclared identifier 'wait'
    while (wait(&status) != pid)       /* wait for completion  */
           ^
test.cpp:51:7: error: use of undeclared identifier 'exit'
      exit(0);            /*   exit if it is                */

【问题讨论】:

  • 您的 pthread 标头是否声明了函数?试试看
  • 在哪里可以找到该标题?我正在使用 OSX。
  • 通常定义为 。它将在您的标准系统头文件位置,您可能不需要去寻找它。

标签: c++ linux gcc clang


【解决方案1】:

这些函数都不是 pthread 函数,尽管 pthreads 确实定义了其中之一的 pthread 特定变体 - pthread_exit。 wait、fork 和 exit 函数是 unistd 库的一部分。

(编辑:顺便说一下,这些调用都与 threading 无关,而是您正在冒险进入进程间通信(ipc)领域。)

【讨论】:

  • 是的。 exit() 在标头 &lt;stdlib.h&gt; 中声明,wait() 应在 &lt;sys/wait.h&gt; 中,fork()execvp()&lt;unistd.h&gt; 中。这些标题都没有包含在程序中。如果模块功能带来可靠的“包含您使用的内容”警告,那么仅此一项就值得了。
  • 是的,它做到了,不知道为什么我认为这一切都在 pthread 中。谢谢。
猜你喜欢
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
相关资源
最近更新 更多