c - 架构 x86_64 的 undefined symbol - C

标签 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 中,但我仍然收到此错误。这就是我的代码的样子:

ma​​in_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");
}

ma​​in_prog.h

#ifndef _MAIN_PROG_H
#define _MAIN_PROG_H

// nothing yet

#endif

物理.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/ 中,如指定的那样生成文件。我尝试移动 physical.o 文件以及 main_prog.o 文件(并更改相应的 makefile),但仍然收到此错误。对于此错误的任何解决方案将不胜感激,谢谢。

最佳答案

您的 OBJS makefile 变量不包含 physical.o,因此您实际上并未链接到该目标文件。尝试将其列为类似的内容

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

关于c - 架构 x86_64 的 undefined symbol - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555106/

相关文章:

c++ - LNK2001 使用自定义结构的 std::vector

c++ - Makefile.am 的 EXTRA_DIST 重复目录名

makefile - 使用 gnumake 和先决条件并行构建

linux - 关于更新 linux-headers

c++ - 返回虚函数的静态成员,缺少抽象类的 vtable

python - 使用 SWIG 从 Python 向 C 传递数组参数

c++ - CMake - 项目级宏添加到应用程序级 target_link_libraries

c - 组织/显示用 C 或 MATLAB 编码的算法的方法

java - 如何在JNI代码中包含cspi/spi.h头文件?

ios - SoundCloud iOS SDK 架构