OSX上的makefile路径问题

问题描述 投票:1回答:7

[好,我以为我会尝试最后一次更新,看看是否能使我到处都可以。我创建了一个非常小的测试用例。这不应该构建任何东西,它只是测试路径设置。另外,我已经设置了路径,所以没有空格。这是我能想到的最小,最简单的测试用例。

此makefile将设置路径,回显该路径,使用指定的完整路径运行avr-gcc -v,然后尝试在不指定完整路径的情况下运行它。第二次尝试应在路径中找到avr-gcc,但没有

makefile

TOOLCHAIN := /Users/justinzaun/Desktop/AVRBuilder.app/Contents/Resources/avrchain


PATH := ${TOOLCHAIN}/bin:${PATH}
export PATH


all:
    @echo ${PATH}
    @echo --------
    "${TOOLCHAIN}/bin/avr-gcc" -v
    @echo --------
    avr-gcc -v

输出

JUSTINs-MacBook-Air:Untitled justinzaun$ make
/Users/justinzaun/Desktop/AVRBuilder.app/Contents/Resources/avrchain/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
--------
"/Users/justinzaun/Desktop/AVRBuilder.app/Contents/Resources/avrchain/bin/avr-gcc" -v
Using built-in specs.
COLLECT_GCC=/Users/justinzaun/Desktop/AVRBuilder.app/Contents/Resources/avrchain/bin/avr-gcc
COLLECT_LTO_WRAPPER=/Users/justinzaun/Desktop/AVRBuilder.app/Contents/Resources/avrchain/bin/../libexec/gcc/avr/4.6.3/lto-wrapper
Target: avr
Configured with: /Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../gcc/configure --prefix=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --exec-prefix=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --datadir=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --target=avr --enable-languages=c,objc,c++ --disable-libssp --disable-lto --disable-nls --disable-libgomp --disable-gdbtk --disable-threads --enable-poison-system-directories
Thread model: single
gcc version 4.6.3 (GCC) 
--------
avr-gcc -v
make: avr-gcc: No such file or directory
make: *** [all] Error 1
JUSTINs-MacBook-Air:Untitled justinzaun$ 


原始问题]

我正在尝试从Makefile中设置路径。我似乎无法在OSX上执行此操作。用PATH := /new/bin/:$(PATH)设置路径无效。请参阅下面的我的makefile。

makefile

PROJECTNAME = Untitled

# Name of target controller
# (e.g. 'at90s8515', see the available avr-gcc mmcu
# options for possible values)
MCU = atmega640

# id to use with programmer
# default: PROGRAMMER_MCU=$(MCU)
# In case the programer used, e.g avrdude, doesn't
# accept the same MCU name as avr-gcc (for example
# for ATmega8s, avr-gcc expects 'atmega8' and 
# avrdude requires 'm8')
PROGRAMMER_MCU = $(MCU)

# Source files
# List C/C++/Assembly source files:
# (list all files to compile, e.g. 'a.c b.cpp as.S'):
# Use .cc, .cpp or .C suffix for C++ files, use .S
# (NOT .s !!!) for assembly source code files.
PRJSRC = main.c   \
         utils.c

# additional includes (e.g. -I/path/to/mydir)
INC = 

# libraries to link in (e.g. -lmylib)
LIBS = 

# Optimization level,
# use s (size opt), 1, 2, 3 or 0 (off)
OPTLEVEL = s



### You should not have to touch anything below this line ###



PATH := /Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin:/usr/bin:/bin:$(PATH)
CPATH := /Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/include


# HEXFORMAT -- format for .hex file output
HEXFORMAT = ihex

# compiler
CFLAGS = -I. $(INC) -g -mmcu=$(MCU) -O$(OPTLEVEL)  \
         -fpack-struct -fshort-enums               \
         -funsigned-bitfields -funsigned-char      \
         -Wall -Wstrict-prototypes                 \
         -Wa,-ahlms=$(firstword                    \
                    $(filter %.lst, $(<:.c=.lst)))

# c++ specific flags
CPPFLAGS = -fno-exceptions                 \
           -Wa,-ahlms=$(firstword          \
           $(filter %.lst, $(<:.cpp=.lst)) \
           $(filter %.lst, $(<:.cc=.lst))  \
           $(filter %.lst, $(<:.C=.lst)))

# assembler
ASMFLAGS = -I. $(INC) -mmcu=$(MCU)          \
           -x assembler-with-cpp            \
           -Wa,-gstabs,-ahlms=$(firstword   \
           $(<:.S=.lst) $(<.s=.lst))

# linker
LDFLAGS = -Wl,-Map,$(TRG).map -mmcu=$(MCU)  \
          -lm $(LIBS)

##### executables ####
CC=avr-gcc
OBJCOPY=avr-objcopy
OBJDUMP=avr-objdump
SIZE=avr-size
AVRDUDE=avrdude
REMOVE=rm -f

##### automatic target names ####
TRG=$(PROJECTNAME).out
DUMPTRG=$(PROJECTNAME).s

HEXROMTRG=$(PROJECTNAME).hex
HEXTRG=$(HEXROMTRG) $(PROJECTNAME).ee.hex

# Start by splitting source files by type
#  C++
CPPFILES=$(filter %.cpp, $(PRJSRC))
CCFILES=$(filter %.cc, $(PRJSRC))
BIGCFILES=$(filter %.C, $(PRJSRC))
#  C
CFILES=$(filter %.c, $(PRJSRC))
#  Assembly
ASMFILES=$(filter %.S, $(PRJSRC))

# List all object files we need to create
OBJDEPS=$(CFILES:.c=.o)     \
        $(CPPFILES:.cpp=.o) \
        $(BIGCFILES:.C=.o)  \
        $(CCFILES:.cc=.o)   \
        $(ASMFILES:.S=.o)

# Define all lst files.
LST=$(filter %.lst, $(OBJDEPS:.o=.lst))

# All the possible generated assembly
# files (.s files)
GENASMFILES=$(filter %.s, $(OBJDEPS:.o=.s))

.SUFFIXES : .c .cc .cpp .C .o .out .s .S \
            .hex .ee.hex .h .hh .hpp

# Make targets:
# all, disasm, stats, hex, writeflash/install, clean
all: $(TRG)

$(TRG): $(OBJDEPS)
    $(CC) $(LDFLAGS) -o $(TRG) $(OBJDEPS)

#### Generating assembly ####
# asm from C
%.s: %.c
    $(CC) -S $(CFLAGS) $< -o $@

# asm from (hand coded) asm
%.s: %.S
    $(CC) -S $(ASMFLAGS) $< > $@

# asm from C++
.cpp.s .cc.s .C.s :
    $(CC) -S $(CFLAGS) $(CPPFLAGS) $< -o $@

#### Generating object files ####
# object from C
.c.o:
    $(CC) $(CFLAGS) -c $< -o $@

# object from C++ (.cc, .cpp, .C files)
.cc.o .cpp.o .C.o :
    $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

# object from asm
.S.o :
    $(CC) $(ASMFLAGS) -c $< -o $@

#### Generating hex files ####
# hex files from elf
.out.hex:
    $(OBJCOPY) -j .text                    \
               -j .data                    \
               -O $(HEXFORMAT) $< $@

.out.ee.hex:
    $(OBJCOPY) -j .eeprom                     \
               --change-section-lma .eeprom=0 \
               -O $(HEXFORMAT) $< $@

#### Information ####
info:
    @echo PATH:
    @echo "$(PATH)"
    $(CC) -v
    which $(CC)

#### Cleanup ####
clean:
    $(REMOVE) $(TRG) $(TRG).map $(DUMPTRG)
    $(REMOVE) $(OBJDEPS)
    $(REMOVE) $(LST)
    $(REMOVE) $(GENASMFILES)
    $(REMOVE) $(HEXTRG)

错误

JUSTINs-MacBook-Air:Untitled justinzaun$ make
avr-gcc -I.  -g -mmcu=atmega640 -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -Wa,-ahlms=main.lst -c main.c -o main.o
make: avr-gcc: No such file or directory
make: *** [main.o] Error 1
JUSTINs-MacBook-Air:Untitled justinzaun$

如果我将CC=更改为包括完整路径:

CC=/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin/avr-gcc

然后找到它,但这似乎不是正确的处理方式。例如,它试图使用系统as而不是正确路径中的一个。

update-可以肯定的是,我也添加了ls命令的输出,因此每个人都知道该文件存在。另外,我已经向makefile文件中添加了一个make info目标,并显示了该输出。

JUSTINs-MacBook-Air:Untitled justinzaun$ ls /Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin
ar      avr-elfedit avr-man     avr-strip   objcopy
as      avr-g++     avr-nm      avrdude     objdump
avr-addr2line   avr-gcc     avr-objcopy c++     ranlib
avr-ar      avr-gcc-4.6.3   avr-objdump g++     strip
avr-as      avr-gcov    avr-ranlib  gcc
avr-c++     avr-gprof   avr-readelf ld
avr-c++filt avr-ld      avr-size    ld.bfd
avr-cpp     avr-ld.bfd  avr-strings nm
JUSTINs-MacBook-Air:Untitled justinzaun$ 

我的路径中有make info\的输出

JUSTINs-MacBook-Air:Untitled justinzaun$ make info
PATH:
/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
avr-gcc -v
make: avr-gcc: No such file or directory
make: *** [info] Error 1
JUSTINs-MacBook-Air:Untitled justinzaun$ 

make info不在我的路径中的\的输出

JUSTINs-MacBook-Air:Untitled justinzaun$ make info
PATH:
/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR Builder.app/Contents/Resources/avrchain/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
avr-gcc -v
make: avr-gcc: No such file or directory
make: *** [info] Error 1
JUSTINs-MacBook-Air:Untitled justinzaun$ 

更新-如上所述,当我将CC设置为包括完整路径时,这是make info的结果。

JUSTINs-MacBook-Air:Untitled justinzaun$ make info
PATH:
/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR Builder.app/Contents/Resources/avrchain/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin/avr-gcc -v
Using built-in specs.
COLLECT_GCC=/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR Builder.app/Contents/Resources/avrchain/bin/avr-gcc
COLLECT_LTO_WRAPPER=/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR Builder.app/Contents/Resources/avrchain/bin/../libexec/gcc/avr/4.6.3/lto-wrapper
Target: avr
Configured with: /Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../gcc/configure --prefix=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --exec-prefix=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --datadir=/Users/justinzaun/Development/AVRBuilder/Packages/gccobj/../build/ --target=avr --enable-languages=c,objc,c++ --disable-libssp --disable-lto --disable-nls --disable-libgomp --disable-gdbtk --disable-threads --enable-poison-system-directories
Thread model: single
gcc version 4.6.3 (GCC) 
which /Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin/avr-gcc
/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR Builder.app/Contents/Resources/avrchain/bin/avr-gcc
JUSTINs-MacBook-Air:Untitled justinzaun$ 
makefile
7个回答
2
投票

我在OSX和Linux上尝试了您的示例,并获得了与您相同的结果。我不太了解为什么它不起作用(并且很想知道),但是我确实有两种可能会有所帮助的解决方法。

export SHELL

而不是在您的Makefile中设置PATH,而是像这样覆盖SHELL:

export SHELL=/Users/whatever/avr-dir/wrapper

这是该包装的可能版本:

#!/bin/bash
PATH="/Users/whatever/avr-dir:${PATH}"
/bin/bash "$@"

Make将调用此包装器以运行yoru食谱的每一行。这有点难看,但是它确实在OSX上对我有用。

外部

将路径固定在make之外。也许创建一个每次登录运行一次的脚本来修复外壳中的PATH,或者创建一个小的脚本(我通常将其称为mk)来修复PATH,然后调用make传递任何参数。这是一个例子:

#!/bin/bash
PATH="/Users/whatever/avr-dir:${PATH}" exec make "$@"

我知道您要求一个Makefile解决方案,但我想我还是会提到此选项。这只是我的意见,但是类似PATH的东西通常是特定于机器的(而不是特定于项目的),我更喜欢将它们与源代码分开。


1
投票

您的问题不是make找不到avr-gcc。您的问题在这一行:

$(CC) $(CFLAGS) -mmcu=$(MCU) -c $(input) -o $(output)

由于尚未定义$(input)$(output),因此avr-gcc命令行不完整。尝试将该行更改为此:

$(CC) $(CFLAGS) -mmcu=$(MCU) -c $< -o $@

$<$@automatic变量,分别定义为表示“第一个必备条件”和“输出目标”。


1
投票

make的问题is无法找到avr-gcc,这是由于\行中的PATH=

$ mkdir /tmp/foo\ bar
$ cd /tmp/foo\ bar
$ (echo "#! /bin/sh"; echo "echo this got run") > execable
$ chmod +x execable
$ mkdir /tmp/tstmake; cd /tmp/tstmake

(现在制作一个具有如图所示内容的Makefile)

$ cat Makefile
PATH := /tmp/foo\ bar:$(PATH)

all:
        @echo path is "$(PATH)"
        execable
$ make
path is /tmp/foo\ bar:/Users/torek/bin.i386:/Users/torek/scripts:[snipped lots]
execable
make: execable: Command not found
make: *** [all] Error 127
$ ed Makefile
71
1s/\\//p
PATH := /tmp/foo bar:$(PATH)
w
70
q
$ make
path is /tmp/foo bar:/Users/torek/bin.i386:/Users/torek/scripts:[snipped lots]
execable
this got run


更新:这不是唯一的问题,至少当我使用MBP模拟该问题时。其余两个是:
  • [CPATH还需要删除反斜杠(这是关于这些:=设置的一般规则)]
  • [CPATH需要通过添加以下行来显式export

    导出CPATH

Makefile

((有时需要反斜杠的原因,而不是其他时间,与将字符串显式传递给shell的次数有关:在$(CC)中一次,但在环境变量或环境变量中零次)的$(PATH)。)


1
投票

看到此页面没有正确答案,我将链接到包含以下内容的页面:

How I could add dir to $PATH in Makefile?

无论出于何种原因,OS X都不会导出PATH,除非您也设置了SHELL变量。

所以:

SHELL=/bin/bash
export PATH:=/foo/bar:$(PATH)

..将工作。


0
投票

我想您正在使用OSX。找出一个优雅的解决方案可能需要几次迭代。

与此同时尝试此合并,并告诉我们结果:

CC=`avr-gcc`

0
投票

如果要更新PATH变量,请执行:

export PATH=$(shell echo $${PATH}):<paths to add>

我做过的例子:

文件:./c/luckme.sh

echo "Hello Lucky Me ! "

Makefile:

export PATH=$(shell echo $${PATH}):c:.
all:
    @luckyme.sh

make的输出:

~$ make
Hello Lucky Me ! 

0
投票

我最近遇到了这个问题。正如其他评论所建议的那样,MacOS附带的make版本存在一些问题。从Homebrew构建(如@MadScientist上面所建议)或install GNU make。我的系统上make的安装版本为3.81,并且出现相同的问题。从Homebrew提供的版本(版本4.3)可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.