这些天我正在阅读 APUE 并自己设置 Makefile,而不是使用该书网站提供的现有 .mk Makefile。我的 Makefile 是这样的:
CC=gcc
LD=ld
CC_FLAGS= -c -I ./include/ -Wall -Wextra
LD_FLAGS= -e main -lc
LIB_OBJS= ./lib/*.o #There'are about 30 .c files in lib, \
most of which are wrap of error handling routines or simplified APIs. \
So I don't copy them all in the code block
all:
ls: ls.c $(LIB_OBJS)
$(CC) $(CC_FLAGS) -o ls.o ls.c
$(LD) $(LD_FLAGS) -o ls ls.o ./lib/error.o
$(LIB_OBJS):
cd ./lib; \
make
clean:
cd ./lib; \
make clean
rm ./ls
./lib 中的 Makefile 与这个非常相似,因为它只是 gcc -c 该目录中的所有 .c 文件,并且不执行任何链接工作。 ls.c 是第一章中的第一段代码,它列出了给定目录的每个元素。 我的问题是,我的 Makefile 可以生成可执行文件而不会打印任何错误,但该文件根本不可执行。每当我 ./ls 具有 x 权限时,bash 返回:
bash: ./ls: No such file or directory
但是如果我使用 gcc 将 ls.o 和 ./lib/error.o 链接在一起,它会顺利运行。所以我想知道 gcc 的 ld 调用有什么特别之处。只用ld链接gcc出品怎么办? 预先感谢!
最佳答案
我不确定您使用的是什么工具链,但是除了您指定的库之外,gcc 传递给链接器的默认库很少,例如库
这是带有 -v 选项的 gcc 的部分输出,它讲述了链接器步骤:
GNU 汇编程序版本 2.17.50.0.6-12.el5 (i386-redhat-linux) 使用 BFD 版本 2.17.50.0.6-12.el5 20061020 /usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o y.o/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o/usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/。 ./../../tmp/ccS39C4x.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed/usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
关于c - 如何使用 ld 链接 gcc 生成的 obj 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19900317/