[英]How to follow linking order when linking against static library with gnu-make?
I've got the following problem: 我有以下问题:
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG build/liblcthw.a tests/list_tests.c -o tests/list_tests
/tmp/ccpvGjZp.o: In function `test_create':
~/lcthw/tests/list_tests.c:12: undefined reference to `List_create'
collect2: ld returned 1 exit status
make: *** [tests/list_tests] Error 1
But 但
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/list_tests.c build/liblcthw.a -o tests/list_tests
runs just fine, nm
shows the expected content, tests run, everybody is happy, etc. 运行得很好, nm
显示预期的内容,测试运行,每个人都很高兴,等等。
I've searched SO and found a plenty of answers (eg Linker order - GCC ), so it's clear that linker works as it really should. 我搜索了SO并找到了很多答案(例如链接器命令 - GCC ),所以很明显链接器的工作原理应该如此。 So, how should I modify my makefile to follow the order? 那么,我应该如何修改我的makefile以遵循命令呢?
Here's the Makefile so far: 到目前为止,这是Makefile:
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS=$(OPTLIBS)
PREFIX?=/usr/local
BUILD=build
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
TARGET=$(BUILD)/liblcthw.a
TARGET_LINK=lcthw
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
#The Target Build
all: $(TARGET) $(SO_TARGET) tests
dev: CFLAGS=-g -Wall -Isrc -Wall -Wextra $(OPTFLAGS)
dev: all
$(TARGET): CFLAGS += -fPIC
$(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS)
build:
@mkdir -p $(BUILD)
@mkdir -p bin
#The Unit Tests
.PHONY: tests
tests: CFLAGS+=$(TARGET) #I think this line is useless now
tests: $(TESTS)
sh ./tests/runtests.sh
#some other irrelevant targets
Tried some weird and obviously wrong things like recursive calling 尝试了一些奇怪的,明显错误的事情,如递归调用
$(TESTS):
$(MAKE) $(TESTS) $(TARGET)
Running this in Debian6
under VirtualBox on Windows7
. 在Windows7
VirtualBox下的Debian6
运行它。 System specifications: 系统规格:
$ uname -a
Linux VMDebian 2.6.32-5-686 #1 SMP Mon Mar 26 05:20:33 UTC 2012 i686 GNU/Linux
$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)
PS its from Zed Shaw's Learn C The Hard Way, exercise 33 . PS来自Zed Shaw的Learn C The Hard Way, 练习33 。 Don't know if I should mark it as a homework :) 不知道我是否应该把它标记为作业:)
You don't show the makefile rule that is building tests/list_tests
but it looks as though it's just the built-in rule. 您没有显示构建tests/list_tests
的makefile规则,但它看起来好像只是内置规则。 With GNU Make, you can print out that rule with -p
, which will show you: 使用GNU Make,您可以使用-p
打印该规则,它将显示:
# default
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
[...]
.c:
# recipe to execute (built-in):
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
By adding the library to $(CFLAGS)
(via the target-specific variable tests: CFLAGS+=$(TARGET)
), you are placing it before $^
in the resulting command. 通过将库添加到$(CFLAGS)
(通过特定于目标的变量tests: CFLAGS+=$(TARGET)
),您将它放在结果命令中的$^
之前。 Instead you should add it to $(LDLIBS)
so that it appears after the object files: 相反,你应该将它添加到$(LDLIBS)
以便它出现在目标文件之后:
tests: LDLIBS+=$(TARGET)
However note that relying on the propagation of target-specific variables like this doesn't work especially well in practice. 但请注意,依赖于像这样的特定目标变量的传播在实践中并不是特别有效。 When you type make tests
then the library is used to build tests/list_tests
et al. 当您键入make tests
该库将用于构建tests/list_tests
等。 However when you are just interested in one test, you will find that make tests/list_tests
fails because the link library is not included in the command. 但是,当您只对一个测试感兴趣时,您会发现make tests/list_tests
失败,因为该命令中不包含链接库。 (See this answer for details.) (有关详细信息,请参阅此答案 。)
I'm a noob, progressing through the same book and I got it to build this way: 我是一个菜鸟,通过同一本书进步,我得到了这样建立:
I changed the line: 我换了一行:
tests: CFLAGS+=$(TARGET) #I think this line is useless now 测试:CFLAGS + = $(TARGET)#I我认为这条线现在没用了
to 至
tests: CFLAGS+=$(SO_TARGET) 测试:CFLAGS + = $(SO_TARGET)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.