0

我目前正在编写一个简短的测试应用程序。编译给了我这些错误:

CC main.c
Building ../bin/pmono
./main.o:(.data+0x18): undefined reference to `busy'
./main.o:(.data+0x58): undefined reference to `busy'
./main.o:(.data+0x98): undefined reference to `busy'
./main.o:(.data+0xd8): undefined reference to `busy'
./main.o:(.data+0x118): undefined reference to `busy'
./main.o:(.data+0x158): more undefined references to `busy' follow
collect2: ld a retourné 1 code d'état d'exécution

我将尝试将代码缩小到特定部分。

这是我使用的包含所需参考的结构:

/*
 * Chained list of blocks from a frame of the cyclic executive
 */
typedef struct block {
   long c;                    /* Worst case execution time */
   long d;                    /* Deadline */
   long p;                    /* Period */
   void (*action) (long);       /* Action performed by this frame */
   struct block * next;       
} *Frame;

函数指针是尚未编写的通用函数的占位符,在同一个 .h 文件中声明如下:

/*
 * Load the CPU for a determined time expressed in nanosecond
 */
void busy(long t);

该函数目前在 c 文件中是空的:

void busy(long t) {
}

最后,这是我在测试中使用的示例默认结构:

struct block D = {8,20,20,busy,0};
struct block C = {2,20,20,busy,&D};
struct block B = {3,10,10,busy,&C};
struct block A = {1,10,10,busy,&B};
Frame sequence0 = &A;

所有这些部分都包含在一个公共源文件中,这些文件在许多周期性任务的实现之间共享。目标文件的编译似乎很好。当我尝试编译给定的实现时,我首先包含 .h 文件,编译 .o 文件,然后尝试使用 makefile 链接整个内容。这里有一个 makefile 可以给你一个想法:

BIN = ../bin/pmono
CC = gcc

SUBDIR = .
SRC = $(foreach dir, $(SUBDIR), $(wildcard $(dir)/*.c))
OBJ = $(SRC:.c=.o) $(wildcard ../common/*.o)
INCLUDES = 
WARNINGS = 
OPTIMISATION =
DEBUG =

XENO_CONFIG = /usr/xenomai/bin/xeno-config
XENO_POSIX_CFLAGS = $(shell $(XENO_CONFIG) --skin=posix --cflags)
XENO_POSIX_LDFLAGS = $(shell $(XENO_CONFIG) --skin=posix --ldflags)

CFLAGS = $(INCLUDES) $(XENO_POSIX_CFLAGS) $(WARNINGS) $(OPTIMISATION)
LDFLAGS = -lm $(XENO_POSIX_LDFLAGS) $(DEBUG)

all:.depend $(BIN)

%.o:%.c
    @echo "CC $<"
    @$(CC) -c $(CFLAGS) $< -o $@

$(BIN): $(OBJ)
    @echo "Building ${BIN}"
    @$(CC) $(OBJ) -o $@ $(LDFLAGS)

clean:
    rm -f $(OBJ)

distclean: clean
    rm -f $(BIN)
    rm -f ./.depend

.depend: $(SRC)
    @echo "Génération des dépendances"
    @$(CC) $(CFLAGS) -MM $(SRC) > .depend

-include .depend

所以,我是这方面的初学者,这是我的理解:main.o中缺少busy函数的符号,而它存在于cyclic_executive.o文件中。我不明白这是怎么可能的,因为我包含了 cyclic_executive.h 文件,从而给出了正确的声明和原型。

我认为我做错了,但我缺乏想法。另外,我真的不喜欢我如何声明我的“默认”序列。我知道有一种正确的方法可以做到这一点,但我不记得了……有人有名字可以帮助搜索吗?

谢谢。

4

1 回答 1

1

您没有将文件与busy()函数调用链接。

从命令行试试这个:

gcc main.c cyclic_executive.c

如果它有效,或者至少没有给出busy()函数错误,那将确认问题。然后尝试

make all

这应该在执行所有命令时打印它们。如果您仍然在黑暗中,请尝试

make -d

这将为您提供有关 make 实际操作的大量诊断信息。

于 2012-06-14T10:58:46.290 回答