-1

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?

Community
  • 1
  • 1
rtanmay
  • 3
  • 2
  • 1
    Why redefine CC to g++? you already have CXX for that. But the real problem is that your targets are wrong. – Klaas van Gend Feb 11 '18 at 19:01
  • To run a file like that it needs to be marked *executable* (`chmod +x`). – Jesper Juhl Feb 11 '18 at 19:01
  • @KlaasvanGend I have read that it is convention to do CC for the compiler that you are using, again I am a beginner so don't know much Can you clarify what do you mean by targets are wrong? – rtanmay Feb 11 '18 at 19:04
  • @JesperJuhl yes, I have done that, it gives the following error then: **cannot execute binary file: Exec format error** – rtanmay Feb 11 '18 at 19:06
  • `CC` means *C Compiler*. You need a *C++ Compiler*. See [manual](https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html). – Franklin Yu Feb 11 '18 at 23:30

1 Answers1

1

The file foo is the object file generated from foo.cpp. Object files can of course not be executed, since they are not complete programs.

Since you don't give a specific name for the executable, the default is a.out on POSIX environments (like Linux).

If you want a specific file-name then you need to add a command to create it. And make sure it doesn't clash with an existing object file.


I recommend you change your whole Makefile to something much more simple, like this:

all: foo

foo: foo.o bar.o
    $(CXX) foo.o bar.o -o foo

That's all that is needed. The object files will be created, with the correct names, from implicit rules.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • how is foo an object file? I wrote g++ foo.cpp -o foo and then executed ./foo(it works) isn't makefile's work to write these instructions on the terminal? I wrote the same instructions – rtanmay Feb 11 '18 at 19:10
  • @rtanmay Look at your `Makefile` again. You *don't* create an executable `foo` file, since you use the `-c` options in `CFLAGS`. Also, when running `make` it will print out all the commands exactly as it executes them, with all arguments. There you should clearly see `g++ -c foo.cpp -o foo`. Note, again, the `-c` flag. – Some programmer dude Feb 11 '18 at 19:12
  • Yes, thanks a lot. I didn't knew about the linkers. So, I just removed the -c flag and now it works. But is it considered a good practice(I mean letting the compiler do the linking)? – rtanmay Feb 11 '18 at 19:15
  • @rtanmay So you have *two* programs? That are separate? Then they should not be linked together so disregard my `Makefile`. As for the linking, even when you do e.g. `g++ foo.cpp -o foo` you *still* link. The program `g++` is a front-end program which invokes the actual compiler and linker internally. – Some programmer dude Feb 11 '18 at 19:21
  • Thanks a lot, One more doubt, should I or should not I include -c flags in my commands. what is considered a good practice? I am very new to this, so excuse my ignorance Also, I don't know why but I am not able to tag you in comments using @ sign – rtanmay Feb 11 '18 at 19:27
  • @rtanmay The `-c` flag is used to create object files. If you don't want object files then don't use it. – Some programmer dude Feb 11 '18 at 20:13