我在 C 上构建项目时遇到问题,并且我创建了最小构建来重现该问题。项目构建并使用.a库,报错:
linker-test$ make
gcc -Wall -g -c -o main.o main.c
gcc -Wall -g -c -o ar-test1.o ar-test1.c
gcc -Wall -g -c -o ar-test2.o ar-test2.c
ar rcs libartest.a ar-test1.o ar-test2.o
gcc -Wall -g -o myapp main.o -L. -lartest
ld: archive member '/' not a mach-o file in '/Users/serg/temp/temp/linker-test/libartest.a'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [myapp] Error 1
尝试存档:https://file.io/OomLs7yTSAnz
我发现github上的一些项目在升级到sonoma后构建时出现问题,并出现同样的错误,有人解决了这个问题吗?
生成文件:
CC = gcc
CFLAGS = -Wall -g
# List of source files and object files
SRCS = main.c
OBJS = $(SRCS:.c=.o)
# Archive name
LIBRARY = libartest.a
all: myapp
myapp: $(OBJS) $(LIBRARY)
$(CC) $(CFLAGS) -o $@ $(OBJS) -L. -lartest
$(LIBRARY): ar-test1.o ar-test2.o
ar rcs $(LIBRARY) ar-test1.o ar-test2.o
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f myapp $(OBJS) $(LIBRARY) *.o
.PHONY: all clean
ar-test1.c:
#include <stdio.h>
void ar_test1() {
printf("This is ar-test1.c\n");
}
ar-test2.c:
#include <stdio.h>
void ar_test2() {
printf("This is ar-test2.c\n");
}
main.c:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
问题是使用的
ar
是从自制软件安装的:
/opt/homebrew/opt/binutils/bin/ar -V
GNU ar (GNU Binutils) 2.41
它适用于文图拉,但不适用于索诺玛,已使用
Apple clang version 15.0.0
和 Homebrew clang version 17.0.6
进行了测试。
使用
ar
中的 /usr/bin/ar
可以解决问题。