Buk Lau
Buk Lau

Reputation: 11

Makefile that changes directory then compiles code

I am having difficulty trying to create a makefile that is in a root directory which will then CD into another directory entitled "code" which is where my code will be compiled. Also the executable then needs to be placed in a new directory which will be created called "bin" which will be in the root directory I have this so far:

all: bin/main

test: bin/main

bin/main: main.cpp
    mkdir -p bin 
    g++ -std=c++11 -Wall -Werror -ansi -pedantic -o bin/program main.cpp 

NOTE: I need those two target all and test. However this make (above) will only work if my code is already in the root

UPDATE: renamed code directory to src and here is new makefile

all:
mkdir -p ./bin
g++ -std=c++11 -Wall -Werror -ansi -pedantic  ./src/main.cpp -o./bin/prgm

test: mkdir -p ./bin g++ -std=c++11 -Wall -Werror -ansi -pedantic ./src/main.cpp -o ./bin/prgm

however now I get this error "make: *** makefile: Is a directory. Stop." Am I trying to run it right? I do this "$ make"

Upvotes: 0

Views: 680

Answers (1)

dlmeetei
dlmeetei

Reputation: 10391

Here is simplest take, You can do many more things like making the target all phoney etc to keep it simple for demonstration.

all:
    - mkdir -p bin 
    g++ -std=c++11 -Wall -Werror -ansi -pedantic -o bin/program src/main.cpp

Hope, you can figure out for target test.

Upvotes: 1

Related Questions