编译.C文件:体系结构x86_64的未定义符号 [英] Compiling a .C file: Undefined symbols for architecture x86_64

查看:68
本文介绍了编译.C文件:体系结构x86_64的未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我在编译.c程序后收到错误消息。

For some reason i get an error message after compiling a .c program.

生成11条警告。
体系结构x86_64的未定义符号:
_main,引用自:
主可执行文件的隐式入口/开始
ld:体系结构x86_64 $ b $找不到的符号b clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

11 warnings generated. Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的date.c:

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

struct date {
  char *day;
  char *month;
  char *year;
};

/*
 * date_create creates a Date structure from `datestr`
 * `datestr' is expected to be of the form "dd/mm/yyyy"
 * returns pointer to Date structure if successful,
 *         NULL if not (syntax error)
 */
Date *date_create(char *datestr) {
  Date *d = (Date *)malloc(sizeof(Date));
  const char delimiter[2] = "/";
  char *token;

  if (d != NULL) {  
    token = strtok(datestr, delimiter);
    d->day = *token;
    token = strtok(NULL, delimiter);
    d->month = *token;
    token = strtok(NULL, delimiter);
    d->year = *token;  
  }
};

/*
 * date_duplicate creates a duplicate of `d'
 * returns pointer to new Date structure if successful,
 *         NULL if not (memory allocation failure)
 */
Date *date_duplicate(Date *d) {
  return NULL;
};

/*
 * date_compare compares two dates, returning <0, 0, >0 if
 * date1<date2, date1==date2, date1>date2, respectively
 */
int date_compare(Date *date1, Date *date2) {
  return 0;
};

/*
 * date_destroy returns any storage associated with `d' to the system
 */
void date_destroy(Date *d) {

};

Bash输出:

bash-3.2$ gcc -W -Wall date.c
date.c:25:12: warning: incompatible integer to pointer conversion assigning to
      'char *' from 'char'; remove * [-Wint-conversion]
    d->day = *token;
           ^ ~~~~~~
date.c:27:14: warning: incompatible integer to pointer conversion assigning to
      'char *' from 'char'; remove * [-Wint-conversion]
    d->month = *token;
             ^ ~~~~~~
date.c:29:13: warning: incompatible integer to pointer conversion assigning to
      'char *' from 'char'; remove * [-Wint-conversion]
    d->year = *token;
            ^ ~~~~~~
date.c:37:44: warning: format specifies type 'void *' but the argument has type
      'char' [-Wformat]
    printf("Day: %p Month: %p Year: %p\n", *d->day, *d->month, *d->year);
                 ~~                        ^~~~~~~
                 %c
date.c:37:53: warning: format specifies type 'void *' but the argument has type
      'char' [-Wformat]
    printf("Day: %p Month: %p Year: %p\n", *d->day, *d->month, *d->year);
                           ~~                       ^~~~~~~~~
                           %c
date.c:37:64: warning: format specifies type 'void *' but the argument has type
      'char' [-Wformat]
    printf("Day: %p Month: %p Year: %p\n", *d->day, *d->month, *d->year);
                                    ~~                         ^~~~~~~~
                                    %c
date.c:40:1: warning: control reaches end of non-void function [-Wreturn-type]
};
^
date.c:47:28: warning: unused parameter 'd' [-Wunused-parameter]
Date *date_duplicate(Date *d) {
                           ^
date.c:55:24: warning: unused parameter 'date1' [-Wunused-parameter]
int date_compare(Date *date1, Date *date2) {
                       ^
date.c:55:37: warning: unused parameter 'date2' [-Wunused-parameter]
int date_compare(Date *date1, Date *date2) {
                                    ^
date.c:62:25: warning: unused parameter 'd' [-Wunused-parameter]
void date_destroy(Date *d) {
                        ^
11 warnings generated.
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


推荐答案

date.c 中需要一个 main 函数。或者,您也可以只使用 -c 进行编译,以使其此时不链接而稍后链接主函数。

You need a main function in date.c. Or you could just compile with -c to not link at this time and link the main function in later.

这篇关于编译.C文件:体系结构x86_64的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆