Tôi đã có những vấn đề sau đây:Làm thế nào để thực hiện theo thứ tự liên kết khi liên kết với thư viện tĩnh với gnu-make?
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
Nhưng
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG tests/list_tests.c build/liblcthw.a -o tests/list_tests
chạy tốt, nm
cho thấy nội dung mong muốn, kiểm tra chạy, tất cả mọi người là hạnh phúc vv
tôi đã tìm kiếm SO và tìm thấy rất nhiều câu trả lời (ví dụ: Linker order - GCC), do đó, rõ ràng rằng mối liên kết hoạt động như nó thực sự cần. Vì vậy, làm thế nào tôi nên sửa đổi makefile của tôi để làm theo thứ tự?
Đây là Makefile cho đến nay:
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 [email protected] $(OBJECTS)
ranlib [email protected]
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o [email protected] $(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
thử một số điều kỳ lạ và rõ ràng là sai như đệ quy gọi
$(TESTS):
$(MAKE) $(TESTS) $(TARGET)
Chạy này trong Debian6
dưới VirtualBox trên Windows7
. Thông số hệ thống:
$ 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)
P.S. từ bài học C The Hard Way của Zed Shaw, exercise 33. Không biết liệu tôi có nên đánh dấu nó là bài tập ở nhà không :)
như thế nào những kết nối? Hai ví dụ đầu tiên (một tốt, một xấu) xây dựng 'list_tests'. Makefile xây dựng 'liblcthw.a'. – wallyk
Thử thêm '-Wl, - no-as-needed' sau' $ (OPTFLAGS) 'trong' CFLAGS' –