【问题标题】:How to build when multiple source files in rootfs in embedded linux?嵌入式linux的rootfs中有多个源文件时如何构建?
【发布时间】:2017-12-30 15:20:48
【问题描述】:

我有一个简单的 c 应用程序

Ctest.c 文件

#include <stdio.h>
#include "new.h"
#include "new.c"

int main()
{
    switching();
    return 0;
}

我有那些 new.c 和 new.h 文件。

new.h 文件为

void switching();

我的 new.c 文件为

void switching(){
    char grade ='B';
    switch(grade){
        case 'A':
            printf("Excellent\n");
            break;
        case 'B':
            printf("Super\n");
            break;
        case 'C':
            printf("Well done\n");
            break;
        case 'D':
            printf("You passed\n");
            break;
        case 'F':
            printf("Better try again");
            break;
        default:
            printf("invalid grade");
            break;
    }

    printf("your grade is %c \n",grade);

    }

当我尝试在我的嵌入式 linux 工具中使用构建命令来编译和生成二进制文件时,构建失败,这是我为 rootfs 上的应用程序更改的 make 文件。

为应用 Ctest 制作文件:

APP  = Ctest


# Add any other object files to this list below

APP_OBJS = Ctest.o
APP_OBJS += new.o


all: build

build: $(APP)

$(APP): $(APP_OBJS)
    $(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS)

这是我在编译时的错误日志

    DEBUG: Executing shell function do_compile
NOTE: make -j 4
ERROR: oe_runmake failed
aarch64-xilinx-linux-gcc  --sysroot=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/plnx_aarch64 -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/work/aarch64-xilinx-linux/Ctest/1.0-r0=/usr/src/debug/Ctest/1.0-r0 -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/plnx_aarch64=   -c -o Ctest.o Ctest.c
aarch64-xilinx-linux-gcc  --sysroot=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/plnx_aarch64 -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/work/aarch64-xilinx-linux/Ctest/1.0-r0=/usr/src/debug/Ctest/1.0-r0 -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/sysroots/plnx_aarch64=   -c -o new.o new.c
new.c: In function 'switching':
new.c:5:13: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
             printf("Excellent\n");
             ^~~~~~
new.c:5:13: warning: incompatible implicit declaration of built-in function 'printf'
new.c:5:13: note: include '<stdio.h>' or provide a declaration of 'printf'
new.c:24:5: warning: incompatible implicit declaration of built-in function 'printf'
     printf("your grade is %c \n",grade);
     ^~~~~~
new.c:24:5: note: include '<stdio.h>' or provide a declaration of 'printf'
Ctest.c:33:17: fatal error: new.h: No such file or directory
 #include "new.h"
                 ^
compilation terminated.
make: *** [<builtin>: Ctest.o] Error 1
make: *** Waiting for unfinished jobs....
ERROR: Function failed: do_compile (log file is located at /home/janani/projects/peta2017.1-zcu102/zcu102/petlnx_zcu102/build/tmp/work/aarch64-xilinx-linux/Ctest/1.0-r0/temp/log.do_compile.19737)

我知道我需要在构建应用程序的 make 文件或 bitbake 文件中进行更改,即 Ctest.bb 文件如果是这样,有哪些更改?我正在使用 petalinux 2017.1

应用程序的bitbake文件是

#
# This file is the Ctest recipe.
#

SUMMARY = "Simple Ctest application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://Ctest.c \
       file://new.c \
       file://Makefile \
      "

S = "${WORKDIR}"

do_compile() {
         oe_runmake
}

do_install() {
         install -d ${D}${bindir}
         install -m 0755 Ctest ${D}${bindir}
         install -m 0755 new ${D}${bindir}
}

如何将 new.h 文件放入 make 文件中,还是需要更改 bitbake 文件?

【问题讨论】:

  • 编译是否在源目录以外的目录中进行?如果是这样,CFLAGS+=-I/path/to/where/source/is
  • .c 文件不包括在内,而是单独编译。请正确掌握基础知识;一本 C 书会有很大帮助。

标签: linux shell embedded c petalinux


【解决方案1】:

您的Ctest.c 文件包含new.c,因此您根本不应该尝试构建new.o。删除线

APP_OBJS += new.o

install -m 0755 new ${D}${bindir}

您没有在 SRC_URI 中提供 new.h 文件。改成

SRC_URI = "file://Ctest.c \
   file://new.c \
   file://new.h \
   file://Makefile \
"

【讨论】:

  • 你推荐的东西完全错了。 .c 文件不应包含在其他文件中。这是非常糟糕的做法。
  • @Olaf 我同意你的观点,这不是一个好主意。我只是在回答关于 yocto 的问题。我相信海报会在适当的时候取得更好的实施。
  • 我看不出这与 yocto 有什么关系。这是错误的方法,就是这样。
  • “您正在构建 new.o,因此您无需在 Ctest.c 中包含 new.c”既可以回答问题,也可以避免不良做法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多