gpt4 book ai didi

c - 从我的 makefile 运行时 GCC 忽略库

转载 作者:行者123 更新时间:2023-11-30 19:15:18 24 4
gpt4 key购买 nike

我创建了自己的库,名为 j(创造性地)。我将库放在 makefile 的目录中。为了显示我的布局,这里逐字记录了 ls -R吐出:

a.exe  header  libj.a  makefile  src

./header:
jlib

./header/jlib:
dstring.h eio.h

./src:
main.c main.d main.o

当我运行gcc src/main.c -static -L. -lj -Iheader -std=c11时它链接得很好!然而,当我运行我认为与我的 makefile 等效的内容时,ld提示缺少符号...
我在 Windows 上使用 Mingw-w64。

这是我的生成文件:

target := librarytest.exe
lflags := -static -L. -lj
cflags := -std=c11 -g -Iheader -Wall
cc := C:\MinGW\bin\gcc

srcfiles := $(wildcard src/*.c)
hdrfiles := $(wildcard header/*)

objfiles := $(srcfiles:.c=.o)
dependencies := $(srcfiles:.c=.d)

build: $(target)

$(target): $(objfiles)
$(cc) -o $(target) $(objfiles) $(lflags)

.c.o:
$(cc) $(cflags) -MMD -MP -c $< -o $@


-include $(dependencies)

这是确切的错误:

C:\MinGW\bin\gcc -std=c11 -g -Iheader -Wall -MMD -MP -c src/main.c -o src/main.o
src/main.c: In function 'main':
src/main.c:8:14: warning: variable 'paramSeed' set but not used [-Wunused-but-set-variable]
const char* paramSeed = NULL;
^
C:\MinGW\bin\gcc -o librarytest.exe src/main.o -static -L. -lj
src/main.o: In function `main':
F:\school\csci440\homework1/src/main.c:56: undefined reference to `String_wrap_d'
F:\school\csci440\homework1/src/main.c:57: undefined reference to `String_touint'
collect2.exe: error: ld returned 1 exit status
makefile:15: recipe for target 'librarytest.exe' failed
mingw32-make: *** [librarytest.exe] Error 1

这是 libj.a 的 nm

0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
U __imp___iob_func
U fprintf
U free
U malloc
U memcpy
U printf
U realloc
U sscanf
U strcpy
000000000000013f T String_append
0000000000000000 T String_construct_d
000000000000004f T String_construct_s
00000000000000ce T String_deconstruct_d
00000000000000fa T String_deconstruct_s
0000000000000286 T String_empty
00000000000002e6 T String_find_after
000000000000025c T String_print
00000000000002a6 T String_touint
0000000000000093 T String_wrap_d
U strlen

eio.o:
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 r .xdata
U __imp___iob_func
U fgets
0000000000000000 T readline
U String_append
U String_construct
U String_empty
U strlen

我显然不像我想象的那样理解链接,而且我在 Google 上找不到任何内容,因为我不断获得有关基本链接的结果,就像我的第一个工作示例一样。

最佳答案

将评论转换为答案。

在用确切的错误消息和库的内容详细阐述问题之前:

What do you see as the linking command from make? You have a path to your compiler in the makefile; are you sure that's the same program you execute from the command line? You're also linking an object file in the makefile but compiling the source file to object and then linking on the command line — what is the error message again?

乔恩·韦尔登回应:

When I compile with the same exe as the path in the makefile I get other errors that I'll add to my question.

为了寻求进一步澄清,我问道:

Does it work if you replace $(objfiles) by $(srcfiles) in the link command line? The paramSeed warning isn't affecting the linking (but would if you add -Werror to your compiler options). Is there any risk of a mix of 32-bit code in your program and 64-bit code in your library, or vice versa? You'd often get a warning if that was an issue, but it would also prevent linking.

乔恩·韦尔登回应:

@JonathanLeffler was right with the discrepancy in the paths. My path env variable finds gcc first in a Haskell installation, which is 32 bit. However, when I was using my makefile, the gcc in my specified directory was 64 bit. GCC either ignored the library, or couldn't resolve the symbols because of a naming convention I suppose.

如果您以前没有遇到过此类问题,则很难诊断。但是,链接器根本找不到任何包含正确类型的目标文件中的符号的目标文件。关于错误类型的目标文件(或整个库)的警告本来很好,但最终它是正确的 - 它找不到正确的符号。

关于c - 从我的 makefile 运行时 GCC 忽略库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com