问题描述
我正在从相同的C源文件构建两个目标-可执行文件和共享库.共享库要求代码可重定位(使用-fPIC编译),但是由于各种原因,我无法在可执行文件中使用可重定位的代码.是否有一种标准/通用的方式来处理Makefile中的这种情况?
I'm building two targets from the same C source files - an executable and a shared library. The shared library requires the code to be relocatable (compiled with -fPIC), but I cannot use relocatable code in my executable for various reasons. Is there a standard/common way to handle this situation in a Makefile?
推荐答案
我建议您有两个目录用于目标文件,一个目录用于标准对象,一个目录用于可重定位的对象.像这样:
I suggest you have two directories for object files, one for standard objects and one for relocatable objects. Something like this:
$(STANDARD_OBJS) = $(addprefix $(STANDARD_OBJS_DIR)/, $(OBJS))
$(RELOC_OBJS) = $(addprefix $(RELOC_OBJS_DIR)/, $(OBJS))
$(RELOC_OBJS_DIR)/%.o: CCFLAGS += -fPic
$(RELOC_OBJS_DIR)/%.o $(STANDARD_OBJS_DIR)/%.o: $(SRC)/%.cc
g++ $< -o $@
(还有一些更复杂的版本,但这可以帮助您入门.)
(There are more sophisticated variations, but this will get you started.)
这篇关于Makefile:来自相同源的两个目标用不同的标志编译了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!