我有一个makefile,它应该编译一组使用<pthread.h>
库的C文件。尽管makefile在我以前的Linux安装上运行,但我现在无法运行它。谁能帮我这个?
CC=gcc
CFLAGS=-c -std=c99
LDFLAGS= -lpthread
SOURCES=pc_0.c stak.h
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=pcths
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.c.o:
$(CC) $< -o $@ $(CFLAGS)
clean:
rm -f *.o core
以下是我在shell提示符下输入“make”时收到的错误消息。
gcc pc_0.c -o pc_0.o -c -std=c99
pc_0.c: In function ‘main’:
pc_0.c:25:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
pc_0.c:29:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
pc_0.c:33:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)()’
gcc -lpthread pc_0.o stak.h -o pcths
pc_0.o: In function `main':
pc_0.c:(.text+0x100): undefined reference to `pthread_create'
pc_0.c:(.text+0x15d): undefined reference to `pthread_create'
pc_0.c:(.text+0x1ba): undefined reference to `pthread_create'
pc_0.c:(.text+0x206): undefined reference to `pthread_join'
pc_0.c:(.text+0x21a): undefined reference to `pthread_join'
pc_0.c:(.text+0x22e): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [pcths] Error 1
但是,如果我手动运行以下说明,则会编译源:
cc pc_0.c -o pc_0.o -c -std=c99
cc pc_0.o -o pcths -lpthread
答案 0 :(得分:0)
不确定它有多重要,但我总是在变量分配
附近添加答案 1 :(得分:0)
将-lpthread
放在命令行的末尾,在目标文件之后,需要来自libthread.[a|so]
的符号
当GNU ld(和其他链接器)解析未定义的引用时,它仅在对象文件和库中查找符号定义,该文件和库跟随包含所述引用的对象。