Question has been solved, visit the bottom part of this paragraph for details.
I have a program: foo.cpp and bar.cpp and have created a Makefile to compile them.
This is how it(Makefile) looks:
CC=g++
CFLAGS=-c
foo.o: foo.cpp
$(CC) $(CFLAGS) foo.cpp -o foo
all: foo.o bar.o
bar.o: bar.cpp
$(CC) $(CFLAGS) bar.cpp -o bar
Now, when I run make all, it compiles and I have the output files there but when I run ./foo it gives me this error-->bash: ./foo: Permission denied
However if I do g++ foo.cpp and then do ./a.out then it runs.
I have seen the similar questions like: https://askubuntu.com/questions/466605/cannot-open-output-file-permission-denied and tried the solution but it did not work.
I don't understand why this is happening. Can someone please help(I am new to Makefiles)?
Thanks
For Future visitors
Solution and Mistakes done:
1)using -c flag
, it created an output file and not an executable file which I was trying to execute.
2)for more information , here is an answer on linkers: What's an object file in C?