我尝试使用函数' sysctlbyname'编译代码。它在编译时闪现了错误列表
/tmp/ccq6bQz6.o: In function `main':
haha.c:(.text+0x43): undefined reference to `sysctlbyname'
haha.c:(.text+0x7a): undefined reference to `sysctlbyname'
haha.c:(.text+0xae): undefined reference to `sysctlbyname'
haha.c:(.text+0xe5): undefined reference to `sysctlbyname'
haha.c:(.text+0x120): undefined reference to `sysctlbyname'
collect2: error: ld returned 1 exit status
我无法找出导致它的原因,但它似乎与sys/sysctl.h
库相关
我正在使用的代码如下:
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main()
{
char *p = NULL;
size_t len;
sysctlbyname("hw.model", NULL, &len, NULL, 0);
p = malloc(len);
sysctlbyname("hw.model", p, &len, NULL, 0);
printf("%s\n", p);
/* CTL_MACHDEP variables are architecture dependent so doesn't work
for every one */
sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0);
p = malloc(len);
sysctlbyname("machdep.cpu.brand_string", p, &len, NULL, 0);
printf("%s\n", p);
int64_t mem;
len = sizeof(mem);
sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
printf("System Memory : %lld\n", mem);
return (0);
}
用于编译的命令是gcc haha.c -o haha
是什么导致了这个问题,我该如何解决?