链接 raspberry pi 上的文件并遇到问题

Linking files on a raspberry pi and getting issues

我有 5 个文件正在尝试 link 在 raspberry pi 上使用 gcc虽然我会问,看看是否有人能看出我做错了什么。

我得到了一个 运行 看起来像这样的 make 文件

Mainfile: 
    gcc -c ./Include/func_1.c 
    gcc -c ./Include/func_2.c
    gcc -c ./Include/func_3.c
    gcc -c ./Include/func_4.c 
    gcc -c ./Include/func_5.c 
    gcc -c Mainfile.c 

    gcc -o Mainfile func_1.o func_2.o func_3.o func_4.o func_5.o Mainfile.o
    

第 1 - 6 行似乎工作正常,我设法制作了 object 文件,但是当我尝试将它们 link 添加到第 8 行的 Mainfile 时,出现以下错误

/usr/bin/ld: /tmp/cc5NQuqi.o: in function `main':
Mainfile.c:(.text+0xfc): undefined reference to `func1_1'
/usr/bin/ld: Mainfile.c:(.text+0x104): undefined reference to `func1_2'
/usr/bin/ld: Mainfile.c:(.text+0x10c): undefined reference to `func2_1'
/usr/bin/ld: Mainfile.c:(.text+0x114): undefined reference to `func2_2'
/usr/bin/ld: Mainfile.c:(.text+0x11c): undefined reference to `func3'
/usr/bin/ld: Mainfile.c:(.text+0x124): undefined reference to `func4_1'
/usr/bin/ld: Mainfile.c:(.text+0x12c): undefined reference to `func4_2'
/usr/bin/ld: Mainfile.c:(.text+0x134): undefined reference to `func5'
collect2: error: ld returned 1 exit status
make: *** [makefile:9: Mainfile] Error 1

似乎无法弄清楚它有什么问题.. 将我的 func c 文件连同它们的 headers 一起放在 include 文件夹中,我将 headers 包含在 Mainfile 中并测试是否这是我将 c 文件包含在 headers 中的代码,然后构建正常。

有谁知道我的 make 文件哪里做错了吗?第一次尝试 link 这么多文件并使用 make 文件,非常感谢您的建议。

编辑:

我的主文件看起来像

#include "./Include/func_1.h" 
#include "./Include/func_2.h" 
... 
#include "./Include/func_5.h" 

in main()
{
    func1_1(argument);
    ... 
    func5_1(argument); 

    return 0; 
}

我的 func_1.h 文件看起来像

#ifndef FUNC_1_H
#define FUNC_1_H

// Argument check, is Host name and Port number default? 
static void func1_1(int argument);

#endif

然后 func_1.c 文件看起来像

#include "math.h"
#include "func_1.h"

static void func1_1(int argument)
{
    printf(argument + 1);
}
static void func1_1(int argument)

static 关键字导致该函数在其他模块(“翻译单元”)中不可见 - 这就是它的全部目的。 (参见 What does "static" mean in C?)。特别是符号 func1_1 不会成为全局符号,其他模块对该符号的引用也不会解析,正如您所见。

如果您想使用其他源文件中的函数,请在声明和定义中删除 static 关键字。

为什么要声明您的函数 static 而您想在它们自己的文件以外的其他地方使用它们?删除所有这些 static 限定符,情况应该会好一些。

但如果我可以建议,你应该:

  1. 创建一个 Src 目录并将所有 *.c 文件移动到其中(是的,Mainfile.c 也是)。将它们存储在名为 Include 的目录中很奇怪。

  2. 改进您的 Makefile。以下是未经测试的起点:

     # Tell the compiler about the headers directory
     CFLAGS += -I./Includes
     # Find all C source files
     SRCS := $(wildcard Src/*.c)
     # Compute object file names from source file names
     OBJS := $(patsubst Src/%.c,%.o,$(SRCS))
    
     # Static pattern rule to tell make how to build an
     # object file from its source file
     $(OBJS): %.o: Src/%.c
         $(CC) $(CFLAGS) -c -o $@ $<
    
     # Tell make that `funcXX.o` depends on the `Includes/funcXX.h`
     # such that it rebuilds the former if the latter changes
     func_%.o: Includes/func_%.h
    
     # Tell make that `Mainfile.o` depends on all `Includes/funcXX.h`
     # files such that it rebuilds the former if any of the latter
     # changes
     Mainfile.o: $(wildcard Includes/func_*.h)
    
     # Tell make how to build the executable from the object files
     Mainfile: $(OBJS)
         $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
    

根据您所展示的内容,技术上不需要在 funcXX.c 中包含 funcXX.h。您只需将它们包含在 Mainfile.c 中。所以,如果你想剥离你的项目,请删除这些包含语句以及:

func_%.o: Includes/func_%.h

来自 Makefile 的规则。但是,正如评论中所指出的,即使不是严格需要它,它也可以检测到声明和定义之间的不匹配。由你决定。