C 用于在Linux中创建库的Makefile不';不编译

C 用于在Linux中创建库的Makefile不';不编译,c,linux,eclipse,ubuntu,makefile,C,Linux,Eclipse,Ubuntu,Makefile,我有3个文件,my_pipe.h,my_pipe.c和main.c,其中my_pipe应该是一个库 当我在Eclipse中编译它时,它编译得非常好,没有错误,但是当我在terminal中运行以下makefile并点击make时: exer3: main.o libmywrapper.a gcc main.c libmywrapper.a -o exer3 -static -lrt libmywrapper.a: my_pipe.o ar rcs libmywrappe

我有3个文件,
my_pipe.h
my_pipe.c
main.c
,其中
my_pipe
应该是一个库

当我在Eclipse中编译它时,它编译得非常好,没有错误,但是当我在
terminal
中运行以下
makefile
并点击
make
时:

exer3:  main.o libmywrapper.a

    gcc main.c libmywrapper.a -o exer3 -static -lrt



libmywrapper.a: my_pipe.o

    ar rcs libmywrapper.a my_pipe.o



main.o: main.c my_pipe.h

    gcc -lpthread -lrt -c main.c



my_pipe.o:  my_pipe.c my_pipe.h

    gcc -lpthread -lrt -c my_pipe.c
我明白了:

a@ubuntu:~/Desktop/myExer$ make
gcc -lpthread -lrt -c main.c
gcc -lpthread -lrt -c my_pipe.c
ar rcs libmywrapper.a my_pipe.o
gcc main.c libmywrapper.a -o exer3 -static -lrt
libmywrapper.a(my_pipe.o): In function `shm_pipe_init':
my_pipe.c:(.text+0x61): undefined reference to `sem_init'
libmywrapper.a(my_pipe.o): In function `shm_pipe_read':
my_pipe.c:(.text+0x17f): undefined reference to `sem_wait'
my_pipe.c:(.text+0x196): undefined reference to `sem_getvalue'
my_pipe.c:(.text+0x1ba): undefined reference to `sem_wait'
libmywrapper.a(my_pipe.o): In function `shm_pipe_write':
my_pipe.c:(.text+0x4b7): undefined reference to `sem_post'
collect2: ld returned 1 exit status
make: *** [exer3] Error 1
知道makefile有什么问题吗

谢谢


更新,如上所述链接器选项,如
-lpthread
-lrt
必须在编译行的最后一行。尝试:

gcc main.o libmywrapper.a -o exer3 -static -lrt
编译时,不需要链接器标志。例如:

main.o: main.c my_pipe.h
  gcc -c main.c

以下是解决方案:

exer3:  main.o sharedMemoryLib.a

    gcc main.o sharedMemoryLib.a -o exer3 -static -lrt -lpthread



sharedMemoryLib.a:  my_pipe.o

    ar rcs sharedMemoryLib.a my_pipe.o



main.o: main.c my_pipe.h

    gcc -c main.c



my_pipe.o:  my_pipe.c my_pipe.h

    gcc -c my_pipe.c

看起来其他目标尚未生成,或者这只是重新运行?@lynxlynx:未生成任何内容。您是否尝试将-lpthread last置于命令行上?我发现这个命令是相关的。另外,我知道您可能只需要-lpthread而不需要-lrt?请删除所有的对象文件,包括库,然后重新创建。完整的构建可能会有所帮助output@lserni:当我在eclipse中编译时,两者都需要。此外,我尝试将
-lpthread
放在末尾,结果与它显示的结果相同。顺便说一句:在链接步骤中,它应该是
main.o
,不
main.c
编译时不需要链接器标志,链接时应使用新编译的
main.o
最后一个编译器步骤行可以如下所示:
gcc-c main.c
-lpthread-lrt
仅在链接时才是必需的。您(=Ron,问题的原始海报)真的应该学习更多关于GNU
make
和它使用的许多隐式规则和变量,比如
CFLAGS
etcRun
make-p
来了解所有规则(和变量)
make
知道的。。。。那就看文件吧。我觉得不错。当然,你可以用makefile做很多魔术。例如,放置
gcc-c$