我有以下问题:
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但
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/list_tests.c build/liblcthw.a -o tests/list_tests运行良好,nm显示预期的内容,测试运行,每个人都很高兴,等等。
我已经搜索过了,并且找到了大量的答案(例如Linker order - GCC),所以很明显链接器的工作方式是它真正应该的。那么,我应该如何修改我的makefile来遵循顺序呢?
到目前为止,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尝试了一些奇怪且明显错误的事情,比如递归调用
$(TESTS):
$(MAKE) $(TESTS) $(TARGET)在Debian6中,在Windows7上的VirtualBox下运行此程序。系统规格:
$ 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) 另外,它来自Zed Shaw的“艰难地学习C语言,exercise 33”。不知道是否应该将其标记为家庭作业:)
发布于 2012-05-11 02:49:17
您没有展示构建tests/list_tests的makefile规则,但看起来它只是一个内置规则。使用GNU Make,您可以使用-p打印出该规则,它将向您显示:
# default
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
[...]
.c:
# recipe to execute (built-in):
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@通过将库添加到$(CFLAGS) (通过特定于目标的变量tests: CFLAGS+=$(TARGET)),您可以在结果命令中将其放在$^之前。相反,您应该将其添加到$(LDLIBS)中,以便它出现在目标文件之后:
tests: LDLIBS+=$(TARGET)但是,请注意,像这样依赖于特定于目标的变量的传播在实践中效果并不是特别好。当您输入make tests时,该库将用于构建tests/list_tests等人。但是,当您只对一个测试感兴趣时,您会发现make tests/list_tests失败了,因为该命令中没有包含链接库。(详细信息请参见this answer。)
发布于 2013-01-01 15:18:12
我是一个新手,在读同一本书的过程中,我得到了这样的构建:
我改了行:
测试:CFLAGS+=$(目标)#我觉得这一行现在没用了
至
测试: CFLAGS+=$(SO_TARGET)
https://stackoverflow.com/questions/10539153
复制相似问题