了解Makefile:x86_64的未定义符号(Mac)

时间:2015-11-12 22:01:56

标签: c compiler-errors makefile include

以下是一个名为USLOSS的操作系统模拟器。 main.c在phase1.a库中执行,这是项目的第4阶段。以下是通过调用make test00生成的

gcc -Wall -g -I./usloss/include -D_XOPEN_SOURCE -D_XOPEN_SOURCE        -L. -L./usloss/lib  testcases/test00.c   -o testcases/test00
testcases/test00.c:18:5: warning: implicit declaration of function
      'GetTimeofDay' is invalid in C99 [-Wimplicit-function-declaration]
    GetTimeofDay(&begin);
    ^
testcases/test00.c:30:5: warning: implicit declaration of function 'Terminate'
      is invalid in C99 [-Wimplicit-function-declaration]
    Terminate(1);
    ^
testcases/test00.c:44:5: warning: implicit declaration of function 'Spawn' is
      invalid in C99 [-Wimplicit-function-declaration]
    Spawn("Child1", Child, "1", USLOSS_MIN_STACK, 4, &pid);
    ^
testcases/test00.c:49:5: warning: implicit declaration of function 'Wait' is
      invalid in C99 [-Wimplicit-function-declaration]
    Wait(&pid, &status);
    ^
4 warnings generated.
Undefined symbols for architecture x86_64:
  "_GetTimeofDay", referenced from:
      _Child in test00-4284b8.o
  "_Sleep", referenced from:
      _Child in test00-4284b8.o
  "_Spawn", referenced from:
      _start4 in test00-4284b8.o
  "_Terminate", referenced from:
      _Child in test00-4284b8.o
      _start4 in test00-4284b8.o
  "_USLOSS_Console", referenced from:
      _Child in test00-4284b8.o
      _start4 in test00-4284b8.o
  "_Wait", referenced from:
      _start4 in test00-4284b8.o
  "_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)
make: *** [testcases/test00] Error 1

make test00应该创建test00.c文件的可执行版本,但我不明白为什么程序无法链接。提供的是我的Makefile

    TARGET = libphase4.a
ASSIGNMENT = 452phase1
CC = gcc
AR = ar

COBJS = phase4.o libuser.o
CSRCS = ${COBJS:.o=.c}

#PHASE1LIB = phase1
#PHASE2LIB = phase2
#PHASE3LIB = phase3
PHASE1LIB = phase1debug
PHASE2LIB = phase2debug
PHASE3LIB = phase3debug

HDRS = phase1.h phase2.h phase3.h phase4.h libuser.h provided_prototypes.h

INCLUDE = ./usloss/include

CFLAGS = -Wall -g -I${INCLUDE}
CFLAGS += -D_XOPEN_SOURCE

UNAME := $(shell uname -s)

ifeq ($(UNAME), Darwin)
        CFLAGS += -D_XOPEN_SOURCE      # use for Mac, NOT for Linux!!
endif

LDFLAGS = -L. -L./usloss/lib

PHASE4 = /home/cs452/fall15/phase4

ifeq ($(PHASE4), $(wildcard $(PHASE4)))
        LDFLAGS += -L$(PHASE4)
endif
    TESTDIR = testcases

TESTS = test00 test01 test02 test03 test04 test05 test06 test07 test08 \
        test09 test10 test11 test12 test13 test14 test15 test16 test17 \
        test18 test19 test20 test21 test22
LIBS = -lusloss -l$(PHASE1LIB) -l$(PHASE2LIB) -l$(PHASE3LIB) -lphase4

$(TARGET):      $(COBJS)
                $(AR) -r $@ $(COBJS)

#$(TESTS):      $(TARGET) $(TESTDIR)/$$@.c
$(TESTS):       $(TARGET)
        $(CC) $(CFLAGS) -I. -c $(TESTDIR)/$@.c
        $(CC) $(LDFLAGS) -o $@ $@.o $(LIBS)

clean:
        rm -f $(COBJS) $(TARGET) test??.o test?? core term*.out

phase4.o:       phase4.c libuser.c

submit: $(CSRCS) $(HDRS) Makefile
        tar cvzf phase4.tgz $(CSRCS) $(HDRS) Makefile

我不太了解Makefile,无法理解链接器发生了什么。还提供了test00.c

#include <stdlib.h>
#include <stdio.h>
#include <usloss.h>
#include "phase1.h"
#include "phase2.h"
#include <usyscall.h>
#include "libuser.h"
#include <assert.h>

#define ABS(a,b) a-b > 0 ? a-b : -(a-b)

int Child(char *arg)
{
    int begin, end, time;
    int me = atoi(arg);

    USLOSS_Console("Child%d(): Going to sleep for 10 seconds\n", me);
    GetTimeofDay(&begin);
    Sleep(10);
    GetTimeofDay(&end);
    time = end - begin;
    time = ABS(10000000, time);
        if (time > 1000000) {
        USLOSS_Console("Child%d(): Sleep bad: %d %d\n",
                       me, time, ABS(10000000, time));
    }
    else {
        USLOSS_Console("Child%d(): Sleep done at time %d\n", me, end);
    }
    Terminate(1);

    return 0;
} /* Child */


int start4(char *arg)
{
    int pid, status;

    USLOSS_Console("start4(): Start 5 children who all sleep for 10 seconds. Upon\n");
    USLOSS_Console("          waking up, each child checks if its sleep time was at\n");
    USLOSS_Console("          least 10 seconds.\n");

    Spawn("Child1", Child, "1", USLOSS_MIN_STACK, 4, &pid);
    Spawn("Child2", Child, "2", USLOSS_MIN_STACK, 4, &pid);
    Spawn("Child3", Child, "3", USLOSS_MIN_STACK, 4, &pid);
    Spawn("Child4", Child, "4", USLOSS_MIN_STACK, 4, &pid);
    Spawn("Child5", Child, "5", USLOSS_MIN_STACK, 4, &pid);
    Wait(&pid, &status);
    Wait(&pid, &status);
    Wait(&pid, &status);
    Wait(&pid, &status);
    Wait(&pid, &status);
    USLOSS_Console("start4(): Test sleep done.\n");
    Terminate(0);

    return 0;
}

为什么会发生此链接器错误,我该怎么做才能修复它并测试我的文件?结果应该是一个可执行的.o文件来运行并查看输出。

1 个答案:

答案 0 :(得分:0)

我在你的班上。看起来您需要使用MacOSX库。