mac上的架构x86_64错误的未定义符号

问题描述 投票:2回答:2

我正在尝试从我个人的Mac笔记本电脑上学习一个项目,我通常使用腻子连接的学校计算机,而我正在他们的网络上。但是,我现在不在他们的网络上,需要在这个项目上工作。他们提供makefile和.h文件,所以这些都是正确的,一切都在他们的机器上进行编译,但是当我拉到我的mac并尝试运行makefile时,我得到了这个错误:

gcc -o test -g main.o linked_list_functions.o unit_tests.o

Undefined symbols for architecture x86_64:
   "_malloc_options", referenced from:
      _main in main.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: *** [test] Error 1

在Windows机器上工作,但不在我的机器上,所以我假设我没有下载的东西。我通过自制软件更新了GCC,GDB,gperf,我仍然遇到这些错误。

编辑调用它的文件的源代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unit_tests.h"

extern char *malloc_options ;           // Keep this line. Do not change it.

// Run the unit tests or as a "normal program".
// You can run this as a "normal program" if you want for a simple test of the $
int main( int argc, char *argv[] )
{
        malloc_options = "CFGSU" ;              // Keep this line. Do not chang$

        test() ;

        return 0 ;
}
c macos gcc compilation
2个回答
0
投票

它没有在OSX中实现。 (抱歉,简短而无聊的答案)

FreeBSD manpages包含malloc_options,但在OSX中则不然。因此链接器正确失败,因为它找不到您尝试外部引用的符号。

OSX简介 - >

SYNOPSIS
     #include <stdlib.h>

     void *
     calloc(size_t count, size_t size);

     void
     free(void *ptr);

     void *
     malloc(size_t size);

     void *
     realloc(void *ptr, size_t size);

     void *
     reallocf(void *ptr, size_t size);

     void *
     valloc(size_t size);

FreeBSD简介 - >

SYNOPSIS
     #include <stdlib.h>

     void *
     malloc(size_t size);

     void *
     calloc(size_t number, size_t size);

     void *
     realloc(void *ptr, size_t size);

     void *
     reallocf(void *ptr, size_t size);

     void
     free(void *ptr);

     char * malloc_options;

/一个


0
投票

malloc_options是一个非便携式扩展。您实验室中的系统是否运行BSD?它似乎只在FreeBSD上实现,并在FreeBSD 10中被删除。

来自FreeBSD 9.3 documentation malloc_options允许人们配置malloc()分配器在您的过程中的工作方式。您的代码指定"CFGSU",它启用以下选项:

  • C - 将最大大小类的大小加倍/减半,它是高速缓存行大小的倍数(64)。超过此大小,子页间距(256字节)用于大小类。默认值为512字节。
  • F - 将活动页面与脏页面的最小比例减半/加倍。在通过madvise(2)向内核通知这些页面中的至少一半之前,可以允许一些脏的未使用页面在比率设置的限制内累积。这为内核提供了足够的信息来回收脏页面,如果物理内存变得稀缺并且页面仍未使用。默认最小比率为32:1; MALLOC_OPTIONS = 6F将禁用脏页清除。
  • G - 在完全特定于线程的缓存垃圾回收扫描之间将大约间隔(按线程特定的高速缓存分配/释放事件计算)加倍/减半。垃圾收集实际上是逐步执行的,一次一个大小类,以避免大量收集暂停。默认扫描间隔为8192; MALLOC_OPTIONS = 14g将禁用垃圾回收。
  • S - ??
  • U - 为所有操作生成utracektrace(1)条目。有关此选项的详细信息,请咨询源。

如您所见,这些只是调整分配器。对于大多数程序,比如你的任务,很少需要像这样调整内存分配器。其他malloc_options启用特定的调试相关选项,这些选项用于检测内存的无效使用,这在开发过程中非常有用,但您的代码不会使用这些选项。

作为一个好奇的学生,我会问教师malloc_options代码的目的是什么。您可能会继续解释您是否尝试在不支持此非标准扩展的非BSD系统上进行开发。

此外,您可以安全地在Mac上进行开发时禁用该代码。我会使用以下代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unit_tests.h"

#ifdef __FreeBSD__
extern char *malloc_options ;
#endif /* __FreeBSD__ */

// Run the unit tests or as a "normal program".
// You can run this as a "normal program" if you want for a simple test of the $
int main( int argc, char *argv[] )
{

#ifdef __FreeBSD__
        malloc_options = "CFGSU" ;
#endif /* __FreeBSD__ */

        test() ;

        return 0 ;
}
© www.soinside.com 2019 - 2024. All rights reserved.