gpt4 book ai didi

c - 在 OS X 上使用 clang 链接 OSMalloc.h 时出现 undefined symbol

转载 作者:行者123 更新时间:2023-11-30 16:58:20 24 4
gpt4 key购买 nike

您好,感谢您的帮助。

我正在尝试仅使用低级 OS X 内核调用来创建一个简单的“hello world”来分配内存并将其写入标准输出。为什么?我正在完成第二版 K&R 的第 8 章,重点是从头开始编写标准文件库。当然,它已经完全过时了,但本章的概念仍然存在。不管怎样,我似乎无法弄清楚如何正确链接以使一切顺利进行,因此给自己带来了很多漂亮的 undefined symbol 错误。

我已经解析了许多导致相同错误的其他问题,但没有找到任何解决如何在我尝试使用的内核库中链接的问题。第三个 #include 中的疯狂长路径需要在链接错误之前进行编译。

代码:

#include <fcntl.h>
#include <unistd.h> // equivalent to (K&R) #include "syscalls.h"
#include </Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/libkern/OSMalloc.h> // for low-level memory allocation

#define MINSTDIOTAG "com.apple.minstdio" // Used by OSMalloc from <libkern/OSMalloc.h>
#define PAGE_SIZE_64K (64 * 1024) // Page size to allocate

int main(void) {

char *base = NULL; // Memory buffer
char *ptr = base; // Location in buffer

// Create tag
OSMallocTag mytag = OSMalloc_Tagalloc(MINSTDIOTAG, OSMT_DEFAULT);

// Attempt to allocate PAGE_SIZE_64K of memory
if ((base = (char *)OSMalloc(PAGE_SIZE_64K, mytag)) == NULL)
return 1;
ptr = base;

// Stuff the buffer with stuff
*ptr++ = 'f';
*ptr++ = 'o';
*ptr++ = 'o';
*ptr++ = '\n';
*ptr = '\0';

// Write it out to stdout
(void)write(STDOUT_FILENO, base, (size_t)(ptr - base));

// Free allocated memory
OSFree(base, PAGE_SIZE_64K, mytag);

// Get out of Dodge City, Kansas
return 0;
}

生成文件:

BIN = ../../bin
ODIR = obj
CC = cc
CFLAGS = -std=c99 -Wall -g -I.
_OBJ = minstdio3.o
_BIN = minstdio3
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

.PHONY: all clean

all: $(_BIN)

clean:
rm -rv $(ODIR) $(_BIN)

minstdio3: $(ODIR)/minstdio3.o
$(CC) $(CFLAGS) $^ -o $@
cp -v $@ $(BIN)/$@

$(ODIR)/%.o: %.c $(DEPS)
mkdir -pv $(ODIR)
$(CC) $(CFLAGS) -c -o $@ $<

收到的错误:

Todds-MBP-2:cbasics todddecker$ make
mkdir -pv obj
cc -std=c99 -Wall -g -I. -c -o obj/minstdio3.o minstdio3.c
cc -std=c99 -Wall -g -I. obj/minstdio3.o -o minstdio3
Undefined symbols for architecture x86_64:
"_OSFree", referenced from:
_main in minstdio3.o
"_OSMalloc", referenced from:
_main in minstdio3.o
"_OSMalloc_Tagalloc", referenced from:
_main in minstdio3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [minstdio3] Error 1

-- 编辑--

“如果您想要低级,为什么不使用 sbrk 系统调用?这将与您对 write 的使用相匹配,并且不会产生任何链接问题。”(来自 CRD )

Using 'sbrk' (and it's cousin 'brk') was the original path I was headed down; however, the man page for 'sbrk' states, "The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management." This statement put me on a path toward trying to discover its replacement. 'malloc' is, of course, the correct and normal utility for memory allocation. However, K&R Chapter 8 is all about writing your own from base OS system calls. So, the base call I was able to find for OS X Darwin is 'OSMalloc' which I'm trying to use.

最佳答案

OSMalloc 仅可用于编写内核本身(内核扩展、设备驱动程序)。用户程序必须使用sbrk。

关于c - 在 OS X 上使用 clang 链接 OSMalloc.h 时出现 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937346/

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