C++ 是否为不支持的文件格式生成文件?
我在OSX上,当我试图在终端中执行这个命令时,我得到了一个编译错误 g++-Wall-o test_E test_E.cpp dynamic_array.cpp oracle.o < >我的其他C++文件,如<代码> TESTA A .CPP < /C> >和<>代码> TestBu.CPP<代码>在相同的命令上运行,但没有最后一部分,例如 g++-Wall-o测试A测试A.cpp动态数组.cpp 我还尝试在不使用oracle.o的情况下运行该命令,但没有不支持的文件格式,它会给出相同的错误 我怎样才能解决这个问题C++ 是否为不支持的文件格式生成文件?,c++,macos,compiler-errors,C++,Macos,Compiler Errors,我在OSX上,当我试图在终端中执行这个命令时,我得到了一个编译错误 g++-Wall-o test_E test_E.cpp dynamic_array.cpp oracle.o < >我的其他C++文件,如 TESTA A .CPP < /C> >和代码> TestBu.CPP在相同的命令上运行,但没有最后一部分,例如 g++-Wall-o测试A测试A.cpp动态数组.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);
}
#包括
#包括“dynamic_array.h”
#包括“oracle.h”
使用名称空间std;
无效生成切分(动态切分数组和切分、整数开始、整数增量、整数计数){
for(int i=0;i cout在这种情况下,您的问题是您试图链接Linux ELF二进制文件,它输出的头的十六进制转储证明了这一点(0x45 0x4C 0x46
=ELF
)
基于您现有资源的可能解决方案:
使用与编译对象文件相同的平台(本例中为您的解决方案)
为您的平台获取一个对象文件(对于OSX,您需要一个与您的体系结构相匹配的Mach-O,在您的例子中是x86_64)
获取目标文件的源代码,并为您的平台编译它
根据需要拆解、转换程序集,并组装一个新的二进制文件(可能是可行的,除非它非常小且实现未知)
45 4C 46
=ELF
。你的oracle.o文件是Linux ELF二进制文件,不是OS X Mach-o。那么你认为我能做什么呢?oracle.o文件是我的老师给我们的。你的老师希望你能在OS X上编译吗?除非你有oracle.o的源代码为其他平台编译,或者你有时间工作在进行反汇编、转换和重新组装时,您需要使用编译对象文件的相同平台。他没有指定。但我现在切换到ubuntu,我会试试它是否有效