I have makefile that builds shared library libsimpletron.so:
#shell
MKDIR_P = mkdir -p
#compiler
CC = g++
#cpp flags
FLAGS = -std=c++1z
CPPFLAGS = -fPIC -Wall -Wextra
LDFLAGS = -shared
#directories
INC_DIR = ../inc
LIB_DIR = ../lib
BIN_DIR = ../bin
OBJ_DIR = ./obj
ALG_DIR = ./algebra
SOURCES = $(shell echo *.cpp)
HEADERS = $(shell echo $(INC_DIR)/*.h)
_OBJECTS = $(SOURCES:.cpp=.o)
OBJECTS = $(patsubst %,$(OBJ_DIR)/%,$(_OBJECTS))
ALGEBRA = $(LIB_DIR)/libalgebra.so
TARGET = $(LIB_DIR)/libsimpletron.so
.PHONY: directories
all : directories $(ALGEBRA) $(TARGET)
directories: $(OBJ_DIR) $(BIN_DIR) $(LIB_DIR)
$(OBJ_DIR):
$(MKDIR_P) $(OBJ_DIR)
$(BIN_DIR):
$(MKDIR_P) $(BIN_DIR)
$(LIB_DIR):
$(MKDIR_P) $(LIB_DIR)
$(ALGEBRA):
$(MAKE) -C $(ALG_DIR)
$(OBJ_DIR)/%.o: %.cpp $(HEADERS)
$(CC) -c $(FLAGS) $(CPPFLAGS) -o $# $<
$(TARGET) : $(OBJECTS)
$(CC) $(LDFLAGS) -o $# $^
.PHONY: clean
clean:
rm -f $(OBJ_DIR)/*.o
make command builds library successfully (I think) :
g++ -c -std=c++1z -fPIC -Wall -Wextra -o obj/builder.o builder.cpp
...similar lines for each .cpp file...
g++ -shared -o ../lib/libsimpletron.so obj/builder.o obj/gradient_trainer.o obj/layer.o obj/neuron.o obj/perceptron.o obj/simpletron.o obj/trainer.o
Then I am trying to use this library. I compile my test xor as follows:
#compiler
CC = g++
#cpp flags
FLAGS = -std=c++1z
PUGI = -l pugixml
#directories
LIB_DIR = /home/lrdprdx/Projects/SimplePerceptron/lib
SIMPLETRON = -lsimpletron
ALGEBRA = -lalgebra
XOR = xor
SOURCE = xor.cpp
#config file
CONFIG = config.xml
$(XOR) : $(SOURCE) $(CONFIG)
$(CC) $(FLAGS) -L$(LIB_DIR) $(SIMPLETRON) $(ALGEBRA) $(PUGI) -o $(XOR) $(SOURCE)
But when I try to compile this I get errors of undefined reference to .... OK, though I do not understand why those errors exist, I found that adding a .cpp file with an empty main function to the directory with other .cpp files fixes all the stuff:
//empty.cpp
int main()
{
return 0;
}
And after rebuild the shared library I make and execute xor successfully.
The question is: what is going on here?
I doubt that adding the extra file really fixed anything. Most likely some other side-effect helped.
Your problem is that your link line is incorrect. GCC, like most UNIX linkers, is a single-pass linker. That means it only walks all the libraries one time looking for unresolved symbols. And that means that the order of the arguments to the linker is critically important: you have to ensure that if item A references symbols in item B, that A comes before B on the link line.
In your example, you are putting all your libraries first, and your source files last. So, when make starts looking at your libraries it hasn't seen your sources yet, and there are no symbols it needs to link. By the time it compiles your sources, there are no libraries left to resolve symbols from.
Your link line should be arranged with sources and object files first, then libraries (in referencer ... referencee order):
$(CC) $(FLAGS) $(SOURCE) -L$(LIB_DIR) $(SIMPLETRON) $(ALGEBRA) $(PUGI) -o $(XOR)
I'm trying to put together a simple makefile example like so:
FLAGS = -std=c++14
INC= -I/usr/local/include
LI = -L/usr/local/lib
LIB = /usr/local/lib/
LIBS = $(LIB)libboost_filesystem-mt.a \
$(LIB)libboost_filesystem-mt.dylib \
$(LIB)libboost_filesystem.a \
$(LIB)libboost_filesystem.dylib \
$(LIB)libboost_system-mt.a \
$(LIB)libboost_system-mt.dylib \
$(LIB)libboost_system.a \
$(LIB)libboost_system.dylib
default:
g++ main.cpp $(FLAGS) $(INC) $(LI) $(LIBS) -o assemble
./assemble
clean:
rm assemble
Is there a way to not have to prepend $(LIB) so many times? That's the only way I can get this to work right now (the above doesn't).
If you want the linker to search the path you have to add libraries using the -l flag. So instead of adding libboost_system-mt.a to your link line, you have to add -lboost_system-mt to your link line. Then the linker will search the paths provided by -L.
I'm not sure about the dylib stuff; I don't do much with OS X.
In any event, if you're using GNU make you can do this:
LIBNAMES := filesystem-mt filesystem system-mt system
LIBS := $(foreach N,$(LIBNAMES),$(LIB)libboost_$N.a $(LIB)libboost_$N.dylib)
I'm trying to compile the following code with SDCC, in Debian using only VIM and a Makefile:
void main(void) {
}
Yes, that simple, it's not working yet. I'm using a Makefile like this :
# GNU/Linux specific Make directives.
# Declare tools.
SHELL = /bin/sh
CC = sdcc
LD = gplink
ECHO = #echo
MCU = 16f88
ARCH = pic14
CFLAGS = -m$(ARCH) -p$(MCU)
LDFLAGS = -c -r -w -m I /usr/share/sdcc/lib/$(ARCH)/
EXECUTABLE = t1
SOURCES = test2.c
OBJECTS = $(SOURCES:.c=.o)
CLEANFILES = test2.o test2.asm test2.map test2.lst
.SUFFIXES: .c .o
.PHONY: clean
# Compile
all: $(EXECUTABLE)
.c.o:
$(AT) $(CC) $(CFLAGS) -o $*.o -c $<
$(EXECUTABLE): $(OBJECTS)
$(AT) $(LD) $(LDFLAGS) $(OBJECTS) -o $(EXECUTABLE)
clean:
$(AT) rm -rf $(CLEANFILES)
After all of this the output after running the makefile is:
sdcc -mpic14 -p16f88 -o test2.o -c test2.c
gplink -c -r -w -m I /usr/share/sdcc/lib/pic14/ test2.o -o t1
make: *** [t1] Segmentation fault
I have tried more complex code with the same result,
I can't see what's wrong, anyone ?
I see several things that can be causing you problems:
When you compile for PICs using SDCC, you need the option --use-non-free because some PIC header files have a special Microchip Licence which is not GPL compatible. Furthermore, --use-non-free might not be available on Debian because of their freedom policy if you installed SDCC from repositories. You would need to install the latest SDCC from the official website.
On the linking stage, you should include the PIC libraries needed to run. Try executing sdcc -mpic14 -p16f88 --use-non-free -V test2.c. This way, SDCC links automatically and With -V (verbose) you can see the calls to assembler and linker and can see the libraries that are added on linkage.
I'm trying to compile a small application using Qt but without using QMake, but whenever I try to run it, I get the following error message:
g++ -c -g -pg -Wall -Wextra -pipe -c -o ../../MOCFiles/moc_projectModel.o ../../MOCFiles/moc_projectModel.cpp
In file included from ../../MOCFiles/moc_projectModel.cpp:10:0:
../../MOCFiles/../Src/ProjectStructure/projectModel.h:4:30: fatal error: QAbstractItemModel: No such file or directory
compilation terminated.
make: *** [../../MOCFiles/moc_projectModel.o] Error 1
This would seem to suggest that I haven't constructed my $(INCLUDE) variable properly, but it is as follows:
INCLUDE = -I/usr/include/qt4 \
-I/usr/include/qt4/QtGui \
-I/usr/include/qt4/QtCore \
-I/usr/include/qt4/QtXml \
-I/usr/share/qt4/mkspecs/linux-g++ \
-I../../Src/ProjectStructure
and I am able to locate the QAbstractItemModel file in /usr/include/qt4/QtCore/, a directory which is part of my include variable. This is my first time writing a GNU Makefile and I'm not sure what it is I'm doing wrong. For reference, the entire file is included here:
CXX = g++
CXXFLAGS = -c -g -pg -Wall -Wextra -pipe
LINK = g++
LIBS = -L/usr/lib \
-L/usr/lib/i386-linux-gnu \
-lgtest \
-lQtGui \
-lQtCore \
-lQtXml \
-lpthread
INCLUDE = -I/usr/include/qt4 \
-I/usr/include/qt4/QtGui \
-I/usr/include/qt4/QtCore \
-I/usr/include/qt4/QtXml \
-I/usr/share/qt4/mkspecs/linux-g++ \
-I../../Src/ProjectStructure
OBJECTS = ../Objects/main.o \
../Objects/test_projectEntity.o \
../Objects/moc_test_projectModel.o \
../../Objects/projectEntity.o \
../../Objects/projectModel.o \
../../MOCFiles/moc_projectModel.o
../../Binaries/tests: $(OBJECTS)
$(LINK) $^ $(LIBS) -o $#
../Objects/main.o : ../Src/main.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) ../Src/main.cpp -o ../Objects/main.o
../Objects/moc_test_projectModel.o : ../MOCFiles/moc_test_projectModel.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) ../MOCFiles/moc_test_projectModel.cpp -o ../Objects/moc_test_projectModel.o
../../MOCFiles/moc_test_projectModel.cpp : ../Src/test_projectModel.cpp
moc ../Src/test_projectModel.cpp -o ../MOCFiles/moc_test_projectModel.cpp
../Objects/test_projectEntity.o : ../Src/test_projectEntity.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) ../Src/test_projectEntity.cpp -o ../Objects/test_projectEntity.o
clean:
rm -f ../Objects/main.o ../Objects/test_projectEntity.o ../Objects/test_projectModel.o
It looks as if you're trying to build ../../Binaries/tests. One of the preqs is ../../MOCFiles/moc_projectModel.o, but there's no explicit rule for building that, so Make uses the implicit rule and attempts this:
g++ -c -g -pg -Wall -Wextra -pipe -c -o ../../MOCFiles/moc_projectModel.o ../../MOCFiles/moc_projectModel.cpp
Notice that there's no mention of INCLUDE there. So g++ doesn't know to look in /usr/include/qt4/QtCore/. The simple solution is to add a rule:
../../MOCFiles/moc_projectModel.o : ../../MOCFiles/moc_projectModel.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) $< -o $#
Notice that I used automatic variables $< and $# there, to reduce redundancy and clutter. We could also make it into a pattern rule:
../../MOCFiles/%.o : ../../MOCFiles/%.cpp
$(CXX) $(CXXFLAGS) $(INCLUDE) $< -o $#
This takes care of moc_projectModel.o and anything else that has the same pattern (source and object both in MOCFiles/). We could make other improvements, but that should be enough to get you up and running.
OK, I thought I would try one last update and see if it gets me anywhere. I've created a very small test case. This should not build anything, it just tests the path settings. Also I've setup the path so there are no spaces. The is the smallest, simplest test case I could come up with.
This makefile will set the path, echo the path, run avr-gcc -v with the full path specified and then try to run it without the full path specified. It should find avr-gcc in the path on the second try, but does not.
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
output
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$
Original Question
I'm trying to set the path from within the makefile. I can't seem to do this on OSX. Setting the path with PATH := /new/bin/:$(PATH) does not work. See my makefile below.
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)
error
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$
If I change my CC= to include the full path:
CC=/Users/justinzaun/Library/Developer/Xcode/DerivedData/AVR_Builder-gxiykwiwjywvoagykxvmotvncbyd/Build/Products/Debug/AVR\ Builder.app/Contents/Resources/avrchain/bin/avr-gcc
then it finds it, but this doesn't seem the correct way to do things. For instance its trying to use the system as not the one in the correct path.
update - Just to be sure, I'm adding the output of my ls command too so everyone knows the file exist. Also I've added a make info target to the makefile and showing that output as well.
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$
Output of make info with the \ in my path
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$
Output of make info with the \ not in my path
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$
update - When I have my CC set to include the full path as described above, this is the result of 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$
I tried your example on OSX and Linux, and got the same results that you did. I don't quite understand why that isn't working (and would love to know), but I do have two workarounds that might help.
export SHELL
Instead of setting the PATH in your Makefile, override the SHELL like this:
export SHELL=/Users/whatever/avr-dir/wrapper
Here's a possible version of that wrapper:
#!/bin/bash
PATH="/Users/whatever/avr-dir:${PATH}"
/bin/bash "$#"
Make will invoke this wrapper to run each line of yoru recipes. This is a little ugly, but it did work for me on OSX.
Outside
Fix the PATH outside of make. Perhaps create a script that you run once per login that fixes the PATH in your shell, or create a small script (I usually call it mk) that fixes the PATH and then invokes make passing along any parameters. Here's an exmaple:
#!/bin/bash
PATH="/Users/whatever/avr-dir:${PATH}" exec make "$#"
I know you asked for a Makefile solution, but I thought I would mention this option anyway. It is just my opinion, but things like PATHs tend to be machine specific (and not project specific), and I prefer to keep them separate from source code.
Your problem is not that make failed to find avr-gcc. Your problem is in this line:
$(CC) $(CFLAGS) -mmcu=$(MCU) -c $(input) -o $(output)
Since $(input) and $(output) have not been defined your avr-gcc command-line is incomplete. Try changing that line to this instead:
$(CC) $(CFLAGS) -mmcu=$(MCU) -c $< -o $#
$< and $# are automatic variables defined to mean "the first prerequisite" and "the output target", respectively.
The problem is that make is failing to find avr-gcc, and it's due to the \ in your PATH= line.
$ 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
(now make a Makefile with contents as shown)
$ 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
Update: this is not the only problem, at least when I use my MBP to simulate the issue. The remaining two are:
CPATH also needs the backslash removed (this is a general rule about these := settings)
CPATH needs to be explicitly exported, by adding the line
export CPATH
to the Makefile.
(The reason you need the backslash sometimes, but not other times, has to do with how many times the string gets passed expliclty to the shell: once when it's in $(CC) but zero times when it is an environment variable or part of $(PATH).)
Seeing as this page didn't have a proper answer, I'll link to this page that does:
How I could add dir to $PATH in Makefile?
For whatever reason OS X does not export PATH unless you set the SHELL variable too.
So:
SHELL=/bin/bash
export PATH:=/foo/bar:$(PATH)
..would work.
I just recently ran into this issue. As other comments suggest, the version of make shipped with MacOS has some issues. Build (as #MadScientist suggests above) or install GNU make from Homebrew. The installed version of make on my system is 3.81 and exhibits the same problem. The version provided from Homebrew (version 4.3) works as expected.
I presume you're using OSX. Figuring out an elegant solution may take a few iterations.
In the meantime try this kludge, and tell us the result:
CC=`avr-gcc`
If what you want is to update your PATH variable, then do:
export PATH=$(shell echo $${PATH}):<paths to add>
Example I did:
File : ./c/luckme.sh
echo "Hello Lucky Me ! "
Makefile :
export PATH=$(shell echo $${PATH}):c:.
all:
#luckyme.sh
output of make:
~$ make
Hello Lucky Me !