我正在使用 mac osx 优胜美地,xcode 6.1.1。 我写了这段简单的代码。当我用 gcc 编译和链接它时,我不断收到错误: 找不到体系结构 x86_64 的符号
但相同的代码在 xcode 或使用 g++、clang++ 中编译成功。我想知道有什么区别? 该项目是单个文件,包含:
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <stdio.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
int main(int argc, char** argv){
vector<ListNode *> vec;
vec.push_back(new ListNode(0));
vec.push_back(new ListNode(1));
cout<<vec[0]->val<<vec[1]->val<<endl;
return 0;
}
最佳答案
如果您使用 g++ 链接目标文件,它会自动链接到标准 C++ 库中(gcc 不会这样做)。这就是为什么 xcode、g++ 和 clang++ 会编译您的代码而 gcc 会抛出错误。
GCC: GNU Compiler Collection
- Refers to all the different languages that are supported by the GNU compiler.gcc: GNU C Compiler
g++: GNU C++ Compiler
gcc
与g++
的主要区别:
- gcc 将编译:
*.c/*.cpp
文件分别为 C 和 C++。 - g++ 将编译:
*.c/*.cpp
文件,但它们都将被视为 C++ 文件。 此外,如果您使用 g++ 链接目标文件,它会自动链接到标准 C++ 库中(gcc 不会这样做)。 - gcc 编译 C 文件的预定义宏较少。
- gcc 编译
*.cpp
和 g++ 编译*.c/*.cpp
文件有一些额外的宏。
关于c++ - 同一段代码,可以在xcode中编译,也可以在terminal中用g++、clang++编译,不能用gcc或clang编译。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27394616/