g++ : can't link with a main executable file g++ : can't link with a main executable file xcode xcode

g++ : can't link with a main executable file


This error usually means you're missing a -c when compiling a simple program, something like this:

%.o: %.c    $(CC) $(CFLAGS) -o $@ $^Program: main.o    $(CC) $(LDFLAGS) -o $@ $^

What's happening is that the first rule is building main.o just like you asked, but instead of being an object file, it's actually the complete, compiled and linked program.

When the second rule tries to use it as an object file, the linker finds that it's not an object file at all and produces "can't link with a main executable file."

Obviously for a more complex program, one with multiple object files or with library dependencies, it would not be able to build an executable from just the one source file, so you'll get a different error and never get as far as the link rule.

The solution, of course, is to add -c to the first rule so that the first invocation only compiles and does not link, producing an actual object file.

%.o: %.c    $(CC) $(CFLAGS) -c -o $@ $^


(Answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )

@Paul R Wrote:

You have several typos in your source files, e.g. #include <vector and using namespace std - you will need to fix these and any other similar mistakes to have a chance of compiling the code.

@Etan Reisner wrote:

You should also probably stick to spelling bias bias instead of also using biais in some places.

The OP wrote:

I finally found the error, it was in my makefile, those lines: naiveAttack : $(EXEC_NAME_NAIVE) statAttack : $(EXEC_NAME_STATALGO) because the executable was already generated, and another compilation was done after, so it generates this error, for more informations I can provide the new makefile, for those who have this type of error.