AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1295857
Accepted
Theo Paraschiv
Theo Paraschiv
Asked: 2020-11-29 02:03:39 +0800 CST2020-11-29 02:03:39 +0800 CST 2020-11-29 02:03:39 +0800 CST

带有 if 语句错误的 Makefile

  • 772

尝试使用其中包含 ifmakefile的run规则创建一个。

当我运行命令make run时,它返回此错误:

/bin/sh: 1: Syntax error: end of file unexpected (expecting "then")

该文件的名称是Makefile. 我能做些什么?(我Makefile用 Vim 编辑器创建了这个,Vim 的格式已经设置为 unix)。

这是代码的一部分:

#!bin/bash

#other rules 

run:

    if ls exec &>/dev/null
    then
            gcc -o exec file1.o file2.o
            ./exec
    else
           ./exec
    fi
scripts makefile
  • 2 2 个回答
  • 718 Views

2 个回答

  • Voted
  1. Best Answer
    Pilot6
    2020-11-29T02:48:03+08:002020-11-29T02:48:03+08:00

    如果if您想检查exec.

    if [ ! -f exec ]; then
        gcc -o exec file1.o file2.o
    fi
    
    ./exec
    
    • 0
  2. steeldriver
    2020-11-29T04:12:42+08:002020-11-29T04:12:42+08:00

    以这种方式将 shell 代码混合到 Makefile 中几乎肯定不会按您期望的方式工作(如果它完全有效的话)。

    特别是,每个 shell 命令行都将在其自己的子 shell 中执行 -/bin/sh默认情况下使用 - 因此你会看到类似的错误

    /bin/sh: 1: Syntax error: end of file unexpected (expecting "then")
    

    尽管/bin/sh有#!/bin/bashshebang(这make将被视为纯粹的评论)。

    由于 的全部目的make是测试先决条件的存在和修改状态,因此您可以定义一个run目标,如果它不存在或已过期,则有条件地构建其可执行先决条件,如下所示:

    executable: file1.o file2.o
            gcc -o executable file1.o file2.o
    
    run: executable
            ./executable
    

    注意:我命名了可执行程序executable,因为名称exec与内置的 shell 名称冲突。

    或者,更惯用地说,使用make's 的自动变量作为目标和先决条件:

    CC      := gcc
    OBJS    := file1.o file2.o
    
    executable: $(OBJS)
            $(CC) -o $@ $^
    
    run: executable
            ./$<
    

    测试make -n:

    $ touch file1.c file2.c
    
    $ make -n run
    gcc    -c -o file1.o file1.c
    gcc    -c -o file2.o file2.c
    gcc -o executable file1.o file2.o
    ./executable
    
    • 0

相关问题

  • 如何在 Nautilus 中管理保存的完整网页及其目录(例如 n.html 和 n_files)

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve