gpt4 book ai didi

c++ - 文件是为不受支持的文件格式构建的?

转载 作者:搜寻专家 更新时间:2023-10-31 01:36:32 26 4
gpt4 key购买 nike

我在 OS X 上,当我尝试在终端中执行此命令时出现编译错误

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

我的其他 c++ 文件,例如 test_A.cpptest_B.cpp 在同一命令上运行良好,但没有最后一部分,例如

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

我也试过在没有 oracle.o 的情况下运行命令,它给出了同样的错误,但没有不支持的文件格式

我该如何解决这个问题?

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)

这是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);
}

最佳答案

在这种情况下,您的问题是您正在尝试链接 Linux ELF 二进制文件,它输出的 header 的十六进制转储证明了这一点 (0x45 0x4C 0x46 = ELF).

基于您可用资源的可能解决方案:

  1. 使用与编译目标文件相同的平台(在本例中为您的解决方案)。
  2. 为您的平台获取目标文件(对于 OS X,您需要一个与您的架构相匹配的 Mach-O,在您的情况下为 x86_64)。
  3. 获取目标文件的源代码,并针对您的平台进行编译。
  4. 反汇编,必要时转换汇编,然后汇编一个新的二进制文件(可能不可行,除非它非常小且实现未知)。

关于c++ - 文件是为不受支持的文件格式构建的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com