Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-23-2012, 02:52 PM   #1
LQ Newbie
Registered: May 2012
Posts: 4

Rep: Reputation: Disabled
Checking if a directory exists within makefile


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:
LIBS=-lSDL -lSDL_image

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

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

Main.o: Main.cpp
    g++ -c $(BIN)/$@ -c $<
Here is the output I get when I run make in the console:
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.
Old 05-23-2012, 03:03 PM   #2
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

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

you have mentioned the relative path.

And also in the script mention like this:

if [ ! -d $(BIN)]

Last edited by divyashree; 05-23-2012 at 03:08 PM.
Old 05-23-2012, 05:01 PM   #3
LQ Newbie
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
Originally Posted by divyashree View Post
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:

        if [ ! -d "./$(BIN)" ];then     \
                mkdir $(BIN);           \
Note the semi-colon after the mkdir, this is needed as all the backslashed lines are joined together into one line.
1 members found this post helpful.
Old 05-23-2012, 05:29 PM   #4
LQ Newbie
Registered: May 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
I tried the backslashes because that thought came into my mind.. just forgot the semicolon. Thanks lej.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
checking if url exists johnh10000 Programming 17 12-16-2009 02:18 PM
Bash scripting with directory checking if exists netpumber Programming 6 12-01-2009 08:35 PM
Checking if file exists Haiyadragon Programming 2 04-26-2004 11:07 AM
checking if a file exists veilig Programming 6 02-01-2004 05:16 AM
Checking if a file exists in C? tristan_vdv Programming 3 05-24-2002 11:06 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:05 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration