33ted
33ted

Reputation: 699

file was built for unsupported file format?

I'm on OS X and I get a compilation error when I try to execute this command in terminal

g++ -Wall -o test_E test_E.cpp dynamic_array.cpp oracle.o

My other c++ files such as test_A.cpp and test_B.cpp run fine on the same command but without the last part, e.g.

g++ -Wall -o test_A test_A.cpp dynamic_array.cpp

I also tried running the command without oracle.o and it gives the same error, but without the unsupported file format

How could I fix this?

ld: warning: ignoring file oracle.o, file was built for unsupported file 
format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 ) which is not the architecture being linked (x86_64): oracle.o
Undefined symbols for architecture x86_64:
"oracle::insert(int, int)", referenced from:
  generate_oracle(oracle&, int, int, int) in test_E-2b7bb8.o
  run_tests(dynamic_array&, oracle&) in test_E-2b7bb8.o
 "oracle::operator[](unsigned int)", referenced from:
  print_state(dynamic_array&, oracle&) in test_E-2b7bb8.o
  compare_content(dynamic_array&, oracle&) in test_E-2b7bb8.o
"oracle::get_allocated_size() const", referenced from:
  print_state(dynamic_array&, oracle&) in test_E-2b7bb8.o
  compare_content(dynamic_array&, oracle&) in test_E-2b7bb8.o
 "oracle::get_size() const", referenced from:
  print_state(dynamic_array&, oracle&) in test_E-2b7bb8.o
  compare_exceptions(dynamic_array&, oracle&) in test_E-2b7bb8.o
  compare_content(dynamic_array&, oracle&) in test_E-2b7bb8.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is test_E.cpp

 #include <iostream>

 #include "dynamic_array.h"
 #include "oracle.h"

 using namespace std;

void generate_cut(dynamic_array &cut, int start, int delta, int count) {
 for (int i = 0; i < count; i++) {
    cut.insert(start, i);
    start += delta;
   }
}

void generate_oracle(oracle &orc, int start, int delta, int count) {
  for (int i = 0; i < count; i++) {
    orc.insert(start, i);
    start += delta;
     }
  }

void print_state(dynamic_array &cut, oracle &orc) {
 cout << "***** cut" << endl;
 cout << "size: " << cut.get_size() << endl;
 cout << "allocated size: " << cut.get_allocated_size() << endl;
 for (int i = 0; i < cut.get_size(); i++) {
    cout << cut[i] << " ";
    if (i > 50) { // avoid lengthy output
        cout << " ...";
        break;
    }
  }
 cout << endl;

 cout << "***** oracle" << endl;
 cout << "size: " << orc.get_size() << endl;
 cout << "allocated size: " << orc.get_allocated_size() << endl;
 for (int i = 0; i < orc.get_size(); i++) {
    cout << orc[i] << " ";
    if (i > 50) { // avoid lengthy output
        cout << " ...";
        break;
    }
 }
 cout << endl;
 }

int const_f(const dynamic_array &cut, int i) {
  return cut[i];
 }

int compare_exceptions(dynamic_array &cut, oracle &orc) {
{
// ********** operator[]
int indexes[] = {orc.get_size(), orc.get_size()+1000};
int N = sizeof(indexes)/sizeof(int);
for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut[indexes[i]];
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "operator[]: uncaught index range exception at: ";
        cout << indexes[i] << endl;
        return 0;
    }
   }
  }

  {
   // ********** operator[] const
  int indexes[] = {orc.get_size(), orc.get_size()+1000};
  int N = sizeof(indexes)/sizeof(int);
  for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut[indexes[i]];
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "operator[] const: uncaught index range exception at: ";
        cout << indexes[i] << endl;
        return 0;
    }
   }
  }

 {
  // ********** insert(int,int)
 int indexes[] = {-1000, -1, orc.get_size()+1, orc.get_size()+1000};
 int N = sizeof(indexes)/sizeof(int);
 for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut.insert(0, indexes[i]);
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "insert(int,int): uncaught index range exception at: ";
        cout << indexes[i] << endl;
        return 0;
    }
   }
  }

 {
  // ********** insert(dynamic_array&,int)
  int indexes[] = {-1000, -1, orc.get_size()+1, orc.get_size()+1000};
  int N = sizeof(indexes)/sizeof(int);
  dynamic_array a;
  for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut.insert(a, indexes[i]);
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "insert(dynamic_array&,int): uncaught index range exception  at: ";
        cout << indexes[i] << endl;
        return 0;
    }
   }
  }

 {
  // ********** remove(int)
  int indexes[] = {-1000, -1, orc.get_size(), orc.get_size()+1000};
  int N = sizeof(indexes)/sizeof(int);
  for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut.remove(indexes[i]);
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "remove(int): uncaught index range exception at: ";
        cout << indexes[i] << endl;
        return 0;
    }
   }
  }

 {
  // ********** remove(int,int)
  // start out of range
  int start_indexes[] = {-1000, -1, orc.get_size()+1, orc.get_size()+1000};
  int N = sizeof(start_indexes)/sizeof(int);
  for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut.remove(start_indexes[i], orc.get_size());
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "remove(int,int): uncaught index range exception at: ";
        cout << start_indexes[i] << "," << orc.get_size() << endl;
        return 0;
    }
   }

   // end out of range
   int end_indexes[] = {orc.get_size()+1, orc.get_size()+1000};
   N = sizeof(end_indexes)/sizeof(int);
   for (int i = 0; i < N; i++) {
    int caught = 0;
    try {
        cut.remove(0, end_indexes[i]);
    } catch (dynamic_array::exception) {
        caught = 1;
    }
    if (!caught) {
        cout << "remove(int,int): uncaught index range exception at: ";
        cout << end_indexes[i] << "," << orc.get_size() << endl;
        return 0;
    }
   }

   // special case: 0 <= end < start < size
   int caught = 0;
   try {
    cut.remove(1, 0);
   } catch (dynamic_array::exception) {
    caught = 1;
   }
   if (!caught) {
    cout << "remove(int,int): uncaught index range exception at: 1,0" << endl;
    return 0;
  }
  }

  return 1; // no failures detected
  }

   int compare_content(dynamic_array &cut, oracle &orc) {
  // check size
  if (cut.get_size() != orc.get_size()) {
    cout << "ERROR. ";
    cout << "size. cut: " << cut.get_size();
    cout << " orc:" << orc.get_size() << endl;

    print_state(cut, orc);
    return 0;
   }

   // check get_allocated_size
   if (cut.get_allocated_size() != orc.get_allocated_size()) {
    cout << "ERROR. ";
    cout << "allocated_size. cut:" << cut.get_allocated_size();
    cout << " orc:" << orc.get_allocated_size() << endl;

    print_state(cut, orc);
    return 0;
   }

  // check operator[] and operator[] const
  for (int i = 0; i < orc.get_size(); i++) {
    if (cut[i] != orc[i]) {
        cout << "ERROR. ";
        cout << "cut[" << i << "]:" << cut[i];
        cout << " orc[" << i << "]:" << orc[i] << endl;

        print_state(cut, orc);
        return 0;
    }

    int x = const_f(cut, i);
    if (x != orc[i]) {
        cout << "ERROR. ";
        cout << "cut[" << i << "]:" << cut[i];
        cout << " orc[" << i << "]:" << x << endl;

        print_state(cut, orc);
        return 0;
    }
   }

  return 1;
   }

void run_tests(dynamic_array &cut, oracle &orc) {
compare_content(cut, orc);
compare_exceptions(cut, orc);

cut.insert(1, 0);
orc.insert(1, 0);

compare_content(cut, orc);
compare_exceptions(cut, orc);

 }

int main() {
 dynamic_array cut;
 generate_cut(cut, 0, 2, 5);

 oracle orc;
 generate_oracle(orc, 0, 2, 5);

 run_tests(cut, orc);
 }

Upvotes: 3

Views: 4211

Answers (1)

Alexander O&#39;Mara
Alexander O&#39;Mara

Reputation: 60577

This this case, your problem is you are trying to link a Linux ELF binary, as evidenced by the hex dump of the header it output (0x45 0x4C 0x46 = ELF).

Possible solutions based on the resources you have available:

  1. Use the same platform as the object file was compiled for (your solution in this case).
  2. Obtain an object file for your platform (for OS X, you would need a Mach-O matching your architecture, in your case x86_64).
  3. Obtain the source code for the object file, and compile it for your platform.
  4. Disassemble, convert assembly as necessary, and assemble a new binary (probably in-feasible unless it's very small and the implementation is unknown).

Upvotes: 5

Related Questions