Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm developing an application using statistical attacks to crack wep keys.

When I compile with my makefile (above) I get this error :

ld: can't link with a main executable file 'execStatAttack' for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation) make: * [statAttack] Error 1

My project contain those files :

  • statAttack.cpp : contain the main function, uses files above

  • rc4.h + rc4.cpp : with those function

#include <iostream>
#include <stdlib.h>
#include <stdio.h
#include <vector

#ifndef RC4
#define RC4

using namespace std
int* rc4(int);
int random_byte();
vector<int> cipher_mess_seq (long, int);

#endif
  • bias.h + bias.cpp :
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <utility>
#include <fstream>
#include <vector>
#include <string>

#ifndef BIAIS
#define BIAIS

using namespace std;

typedef pair<int,double> IntegerDoublePair;
vector<IntegerDoublePair> get_bias (string, int);
int compareTo (double, double);
vector<IntegerDoublePair> get_all_biases(string);

#endif
  • and the makefile :
CC = g++
CFLAGS = -Wall -g
LDFLAGS = -lm 
EXEC_NAME_NAIVE = execNaiveAttack
EXEC_NAME_STATALGO = execStatAttack
OBJ_FILES_NAIVE = naiveAttack.o biais.o rc4.o
OBJ_FILES_STATALGO = statAttack.o biais.o rc4.o

naiveAttack : $(EXEC_NAME_NAIVE)

statAttack : $(EXEC_NAME_STATALGO)

$(EXEC_NAME_NAIVE) : $(OBJ_FILES_NAIVE)
   $(CC) $(OBJ_FILES_NAIVE) $(LDFLAGS) -o $(EXEC_NAME_NAIVE)

$(EXEC_NAME_STATALGO) : $(OBJ_FILES_STATALGO)
   $(CC) $(OBJ_FILES_STATALGO) $(LDFLAGS) -o $(EXEC_NAME_STATALGO)

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

clean :
   rm -f $(OBJ_FILES_NAIVE) $(OBJ_FILES_STATALGO)

mrproper: clean
   rm -rf $(EXEC_NAME_NAIVE) $(EXEC_NAME_STATALGO)

this is my configuration (terminal) :

==> g++ --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)

Target: x86_64-apple-darwin13.1.0

Thread model: posix

So i would like your help, to find out why this error appeared.

Thanks.

like image 367
Souf Avatar asked Apr 17 '14 11:04

Souf


1 Answers

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 $@ $^
like image 87
Tim Sylvester Avatar answered Nov 05 '22 03:11

Tim Sylvester