架构的未定义符号x86_64 - C

时间:2017-10-03 23:55:38

标签: c makefile linker-errors undefined-symbol

我开始用C编写程序,但是我有以下错误,我无法弄清楚原因:

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

据我了解,我已将所有内容正确地包含在相应的文件和makefile中,但我仍然遇到此错误。这就是我的代码:

main_prog.c

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

#include "physical.h"
#include "main_prog.h"

int main(int argc, char *argv[]) 
{
    PhysicalState * initPhysical();
    const Direction blah = L2R;

    unsigned char new_char; 
    new_char = receiveByte(blah);

    printf("Done.\n");
}

main_prog.h

#ifndef _MAIN_PROG_H
#define _MAIN_PROG_H

// nothing yet

#endif

physical.h

#ifndef _PHYSICAL_H
#define _PHYSICAL_H

#include <pthread.h>

#define MAX_FRAME_SIZE 1024

typedef enum
{
  false,
  true
} boolean;

typedef enum
{
  L2R,
  R2L
} Direction;

struct PHYSICAL_STATE
{
  pthread_cond_t  L2RTxSignal;     
  pthread_cond_t  L2RRxSignal;     
  boolean         L2RReady;        
  pthread_mutex_t L2RLock;       

  pthread_cond_t  R2LTxSignal;     
  pthread_cond_t  R2LRxSignal;     
  boolean         R2LReady;        
  pthread_mutex_t R2LLock;        
};

typedef struct PHYSICAL_STATE PhysicalState;

PhysicalState * initPhysical();

unsigned char receiveByte(const Direction dir);

#endif

最后是我的

生成文件

PROG = main_prog
HDRS = physical.h main_prog.h
SRCS = main_prog.c 

OBJDIR = object
OBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(SRCS)) 

CC = gcc
CFLAGS = -Wall --std=c99 -L.
LIBS = -lm

all : $(OBJDIR) $(PROG)

$(PROG) : $(OBJS)
    $(CC) $(CFLAGS) $^ -o $(PROG) $(LIBS)

object/%.o : %.c $(HDRS)
    $(CC) -c $(CFLAGS) $< -o $@ $(LIBS)

$(OBJDIR) :
    mkdir -p $@/

clean:
    rm -rf object/
    rm -f $(PROG)

还有一个physical.o文件与所有这些文件位于同一目录中,而main_prog.o文件位于object/中,由makefile指定。我已经尝试移动physical.o文件以及main_prog.o文件(并更改makefile以对应),但我仍然遇到此错误。非常感谢任何解决此错误的方法。

1 个答案:

答案 0 :(得分:1)

您的OBJS makefile变量不包含physical.o,因此您实际上并未在该目标文件中进行链接。尝试将其列为

OBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(SRCS)) physical.o