[英]Makefile shared library error
I was doing makefile test for creating static library and creating dynamic library from it. 我正在做makefile测试,以创建静态库并从中创建动态库。 I have foo.h foo.c bar.c and main.c files in my directory.
我的目录中有foo.h foo.c bar.c和main.c文件。 I wrote a Makefile as follows:
我写了一个Makefile,如下所示:
#This makefile demonstrate the shared and static library creation. #此makefile演示了共享库和静态库的创建。
CFLAGS = -Wall -Werror -fPIC
CFLAGS1 = -Wall
inc = /home/betatest/Public/implicit-rule-archive
all : foo.o bar.o libfoo.a libfoo.so run
foo.o: foo.c foo.h
@echo "Making the foo.o"
$(CC) -c $(CFLAGS) $< -o $@
bar.o : bar.c foo.h
@echo "Making the bar.o"
$(CC) -c $(CFLAGS) $< -o $@
libfoo.a(foo.o bar.o) : foo.o bar.o
ar cr $@ $^
libfoo.so : libfoo.a
$(CC) -shared -o $@ $^
run : main.c
$(CC) -L$(inc) $(CFLAGS1) -o $@ $< -lfoo
.PHONY : clean
clean :
-rm -f *.o run libfoo.*
while making it gives the following the following errors: 同时导致以下错误:
Making the foo.o
cc -c -Wall -Werror -fPIC foo.c -o foo.o
Making the bar.o
cc -c -Wall -Werror -fPIC bar.c -o bar.o
make: *** No rule to make target 'libfoo.a', needed by 'all'. Stop.
I have written the archive statement as : 我将存档声明写为:
libfoo.a(foo.o bar.o) : foo.o bar.o
ar cr $@ $^
from this to: 从这到:
libfoo.a(*.o) : *.o
ar cr $@ $^
Can I write the statement like this because in both the cases the error is the same. 我可以这样写语句吗,因为在两种情况下错误都是相同的。 I don't understand where I am going wrong.
我不明白我要去哪里错了。 Thanks!!!!!
谢谢!!!!!
Instead of libfoo.a(foo.o bar.o)
you want plain libfoo.a : foo.o bar.o
. 而不是
libfoo.a(foo.o bar.o)
您需要普通的libfoo.a : foo.o bar.o
Another thing is that .a
files are normally built for linking against the final executable, hence object files for .a
are compiled as position-dependent code. 另一件事是
.a
文件通常是为链接最终的可执行文件而构建的,因此.a
目标文件被编译为与位置相关的代码。 Shared libraries, on the other hand, require code compiled as position independent. 另一方面,共享库要求将代码编译为与位置无关。
Hence, you can: 因此,您可以:
libfoo.a
. libfoo.a
。 make -j
works robustly. make -j
可靠地运行。 run
always finds libfoo.so
in its directory. run
始终在其目录中找到libfoo.so
。 This way: 这条路:
CFLAGS = -Wall -Werror
LDFLAGS = -L/home/betatest/Public/implicit-rule-archive -Wl,-rpath,'$$ORIGIN'
all : run
run : main.o libfoo.so
$(CC) $(LDFLAGS) -o $@ $^
libfoo.so : CFLAGS += -fPIC # Build objects for .so with -fPIC.
libfoo.so : foo.o bar.o
$(CC) -shared -o $@ $^
# Compile any .o from .c. Also make dependencies automatically.
%.o : %.c
$(CC) -c $(CFLAGS) -o $@ -MD -MP $<
# Include dependencies on subsequent builds.
-include $(wildcard *.d)
.PHONY : all clean
clean :
-rm -f *.o *.d run libfoo.*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.