当前位置: 首页 > 知识库问答 >
问题:

未定义对“pthread\u create”的引用-pthread不工作

史修谨
2023-03-14

我在一个函数中创建了新的线程,其中包括pthread。h、 但它不起作用,编译时我不断收到以下错误:

定义对“pthread\u create”的引用

我用来编译的标志如下:

CFLAGS=-std=gnu99-pthread-g-Wall-Wextra-Werror-Wmissing声明-Wmissing原型-Werror隐式函数声明-Wreturn类型-Wparentheses-Wunused-Wold样式定义-Wundef-Wshadow-Wstrict原型-Wswitch default-Wunreachable代码

编译器是gcc

生成文件:

CC=gcc
CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

all: finder

finder: stack.o list.o finder.o
    $(CC) -o mfind stack.o list.o mfind.o

stack.o: stack.c stack.h
    $(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
    $(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
    $(CC) -c finder.c $(CFLAGS)

clean:
    rm -f *.o finder

共有2个答案

宋耀
2023-03-14

如果您使用的是CmakeList,请添加以下内容:

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(myprog Threads::Threads)
越嘉茂
2023-03-14

-pthon在链接阶段需要,而不是在编译各个翻译单元时。典型的方法如下所示:

CC=gcc
CFLAGS=-std=gnu99 -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code
LIBS=-pthread

all: finder

finder: stack.o list.o finder.o
    $(CC) -o mfind stack.o list.o mfind.o $(LIBS)

stack.o: stack.c stack.h
    $(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
    $(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
    $(CC) -c finder.c $(CFLAGS)

clean:
    rm -f *.o finder
 类似资料:
  • 问题内容: 我遇到了“ 高级Linux编程”中的 一个概念。这里是一个链接:请参阅 4.5 GNU / Linux线程实现 。 我对作者所说的概念很清楚,但是我对他解释的为线程打印processID的程序感到困惑。 这是代码 根据作者,上述代码的输出为 我编译时得到的输出是 我知道,创建线程时,Linux内部调用 clone (大多数情况下),就像 fork 系统调用创建进程一样。唯一的区别是在进

  • 问题内容: 在各种多线程C和C ++项目中,我已经看到该标志同时应用于编译和链接阶段,而其他人则根本不使用它,而只是转到链接阶段。 有没有编译和链接标志的危险,即实际上是做什么的?我主要对Linux平台感兴趣。 问题答案: 尝试: 并查找以。开头的任何内容。 在我的计算机上,这导致文件使用进行编译并与链接。在其他平台上,这可能有所不同。使用于大多数便携性。 在GNU libc上使用,可以更改某些l

  • 问题内容: 我试图找到pthread库的源代码。(我猜它应该是Linux源代码的一部分) 但是以某种方式找不到拥有它的任何好的网站。 我喜欢这个网站:http : //lxr.linux.no/#linux+v2.6.34.1/我通常会在这里找到我需要的东西。无法以某种方式搜索pthread源。无论如何,我想提一提,我需要一个类似于该网站的链接(可搜索)。 浏览linux源码的网站可能是个人喜好的

  • 问题内容: 今天在一次采访中有人问我这个问题。 “当我们使用(POSIX Threads)创建线程时,该线程将自行启动。为什么我们需要显式调用Java。为什么在创建实例时Java不启动该线程是什么原因。” 我很空白,面试官时间很短,最终他无法向我解释原因。 问题答案: 在Java中,不立即启动线程会导致获得更好的API。您可以在线程上设置属性(守护程序,优先级),而不必在构造函数中设置所有属性。

  • 问题内容: 我面临pthread的同步问题。threadWaitFunction1,是线程等待函数。我希望行号。仅在243-246完成后才执行247 。但我感到奇怪的是,有时它会在243-246完成之前直接跳到247。 请帮我。 提前致谢。 创建并调用上述线程的线程是: 如果我使用pthread_mutex_lock保护整个函数,但仍然存在相同的问题。 如何确保有序执行?有人可以帮忙吗? 编辑:n

  • 我正在尝试使用AassetManager从android apk访问资产。然而,尽管我已经包含了asset_manager.h和asset_manager_jni.h,但我仍然得到了“对aassetmanager_fromjava的未定义引用”。其他来自asset_manager.h的函数,如AAssetManager_openDir(mgr,"“)等也不能被引用。 以下是完整的代码 这段代码在一