0
votes

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"

1
I do not see an actual question here. You are confused about your application's requirements. This is not something that anyone here can answer. Only you know your application source's layout. Once you figure out how your source code looks like, and what actual commands are needed to build then, then the task of writing a Makefile begins.Sam Varshavchik
Change main.cpp to code/main.cpp?user253751
The usual approach is that the makefile works in the parent directory, and you have the .cpp files include the relative path from the parent directory.M.M

1 Answers

1
votes

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.