[英]Building shared libraries with Makefile
I have a project that I want to build a shared library for it. 我有一个项目想要为其建立共享库。 The following Makefile works:
以下Makefile起作用:
libfastpd.so: fastpd.cpp
$(CXX) -std=c++11 -fPIC -c fastpd.cpp -o fastpd.o
$(CXX) -std=c++11 -fPIC -c graph.cpp -o graph.o
$(CXX) -std=c++11 -fPIC -c LinkedBlockList.cpp -o LinkedBlockList.o
$(CXX) -std=c++11 -fPIC -c maxflow.cpp -o maxflow.o
$(CXX) -std=c++11 -shared -Wl,-soname,libfastpd.so -o libfastpd.so fastpd.o graph.o LinkedBlockList.o maxflow.o
clean:
rm *.o *.so
Then I came across this recipe in Cogswell et al.'s C++ Cookbook: https://www.oreilly.com/library/view/c-cookbook/0596007612/ch01s18.html and decided to improve my Makefile based on that: 然后我在Cogswell等人的C ++ Cookbook中找到了这个食谱: https : //www.oreilly.com/library/view/c-cookbook/0596007612/ch01s18.html,并决定基于此来改进我的Makefile:
# Specify extensions of files to delete when cleaning
CLEANEXTS = o so
# Specify the source files, the target files,
# and the install directory
SOURCES = fastpd.cpp graph.cpp LinkedBlockList.cpp maxflow.cpp
OUTPUTFILE = libfastpd.so
INSTALLDIR = ./
.PHONY: all
all: $(OUTPUTFILE)
# Build lib*.so from all the *.o;
# subst is the search-and-replace
# function demonstrated in Recipe 1.16
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES))
$(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^
.PHONY: install
install:
mkdir -p $(INSTALLDIR)
cp -p $(OUTPUTFILE) $(INSTALLDIR)
.PHONY: clean
clean:
for file in $(CLEANEXTS); do rm -f *.$$file; done
# Generate dependencies of .ccp files on .hpp files
include $(subst .cpp,.d,$(SOURCES))
%.d: %.cpp
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
Running this file I obtained the following error: 运行此文件,我得到以下错误:
/usr/bin/ld: fastpd.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
/usr/bin/ld:fastpd.o:在创建共享库时,不能使用针对“ .rodata”的R_X86_64_32重定位; recompile with -fPIC fastpd.o: error adding symbols: Bad value
用-fPIC fastpd.o重新编译:添加符号时出错:值错误
Checking the terminal output, I observed that the following commands were executed: 检查终端输出,观察到执行了以下命令:
g++ -c -o fastpd.o fastpd.cpp
g++ -c -o graph.o graph.cpp
g++ -c -o LinkedBlockList.o LinkedBlockList.cpp
g++ -c -o maxflow.o maxflow.cpp
No -fPIC
! 没有
-fPIC
!
My question is: Which lines of the Makefile execute these commands and how to add -fPIC
to them? 我的问题是: Makefile的哪几行执行这些命令,以及如何向它们添加
-fPIC
?
Any references to good ressources to understand the entire Makefile above would be very much appreciated as well! 任何对理解上述整个Makefile的良好资源的引用也将不胜感激!
Thank you very much in advance for your help! 预先非常感谢您的帮助!
Which lines of the Makefile execute these commands... ?
Makefile的哪几行执行这些命令...?
The short answer is none . 简短的答案是没有 。 The rule...
规则...
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES))
$(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^
only specifies the link time dependencies and command. 仅指定链接时间依赖性和命令。 The
-fPIC
option needs to be specified when you compile the source file but you haven't provided any rule to build a .o
from a .cpp
so make
falls back on its implicit rule which (for the purposes of this example) is essentially... 在编译源文件时,需要指定
-fPIC
选项,但尚未提供任何从.cpp
构建.o
规则,因此make
依赖于其隐式规则 (对于本示例而言)实质上是...
%.o: %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
So the obvious solution is to add -fPIC
to CXXFLAGS
... 因此,显而易见的解决方案是将
-fPIC
添加到CXXFLAGS
...
CXXFLAGS += -fPIC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.