LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Checking if a directory exists within makefile (https://www.linuxquestions.org/questions/linux-newbie-8/checking-if-a-directory-exists-within-makefile-946504/)

SexyBachelor 05-23-2012 01:52 PM

Checking if a directory exists within makefile
 
Hi,

I'm having trouble with a makefile I'm trying to write that uses a bash command to test for the existence of a certain directory.

Here is the makefile:
Code:

DEBUG=
OUT=a.out
LIBS=-lSDL -lSDL_image
OBJECTS=Main.o
BIN=bin

default: $(BIN) $(OBJECTS)
    g++ -o $(OUT) $(DEBUG) $(addprefix $(BIN)/, $(OBJECTS)) $(LIBS)

$(BIN):
    if [ ! -d "./$(BIN)" ];then
        mkdir $(BIN)
    fi


Main.o: Main.cpp
    g++ -c $(BIN)/$@ -c $<

Here is the output I get when I run make in the console:
Code:

josh@josh-Dell-XPS420:~/Desktop/Diminisher/Version 1$ make
if [ ! -d "./bin" ];then
/bin/sh: 1: Syntax error: end of file unexpected
make: *** [bin] Error 2
josh@josh-Dell-XPS420:~/Desktop/Diminisher/Version 1$

Any ideas? I'm not very good with Makefile or Bash so it's probably something obvioius I'm missing.. I just can't find it.

divyashree 05-23-2012 02:03 PM

Try to store the full path of bin in the variable BIN like:

Quote:

BIN="/path/to/bin"
you have mentioned the relative path.

And also in the script mention like this:

Quote:

if [ ! -d $(BIN)]

lej 05-23-2012 04:01 PM

Quote:

Originally Posted by divyashree (Post 4685791)
Try to store the full path of bin in the variable BIN like:

Er, no. The path must be relative to the project directory (what happens if the Makefile is moved to another directory?)

The problem is that make assumes each line is a separate command. If you want a command to span multiple lines, you need to backslash escape them (except for the last one, of course), like so:

Code:

$(BIN):
        if [ ! -d "./$(BIN)" ];then    \
                mkdir $(BIN);          \
        fi

Note the semi-colon after the mkdir, this is needed as all the backslashed lines are joined together into one line.

SexyBachelor 05-23-2012 04:29 PM

I tried the backslashes because that thought came into my mind.. just forgot the semicolon. Thanks lej.


All times are GMT -5. The time now is 07:29 PM.