LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: Disabled
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.
 
Old 05-23-2012, 03:03 PM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
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)]

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

Rep: Reputation: Disabled
Quote:
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:

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.
 
1 members found this post helpful.
Old 05-23-2012, 05:29 PM   #4
SexyBachelor
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.
 
  


Reply


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


All times are GMT -5. The time now is 09:30 PM.

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