如何在Unix控制台或Mac终端中编译/运行C或C++?
(我知道,忘掉它,然后重新学习它.是时候把它写下来了.)
我一直在XCode开发一个学校项目.最终产品必须在源代码中使用makefile提交,因此我编写了一个makefile并开始编译,以确保我有一个工作副本.这是我的makefile:
all: main.o StackList.o world.o Farm.o
gcc main.o StackList.o world.o Farm.o -g -o Project1
main.o:
gcc -g -c main.cpp
StackList.o:
gcc -g -c Stacklist.cpp
world.cpp:
gcc -g -c world.cpp
Farm.cpp:
gcc -g -c Farm.cpp
clean:
rm *.o Project1
Run Code Online (Sandbox Code Playgroud)
编译每个目标文件工作正常,但当它到达"全部"链接步骤时,它似乎不知道标准库.我从"cin","basic_string"到"operator new"的每一个都得到"未定义的符号"错误.
我的印象是这些东西不需要直接表示,事实上过去并不需要这样做.
知道可能会发生什么吗?
编辑:
如果有帮助,这是(非常长的)错误消息的开头:
Undefined symbols for architecture x86_64:
"std::cin", referenced from:
_main in main.o
"std::cout", referenced from:
_main in main.o
Farm::print(int) in Farm.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced …
Run Code Online (Sandbox Code Playgroud) 这个代码用g ++编译好,但在用gcc编译时会抛出链接器错误.这些错误的原因是什么?
$ g++ traits2.cpp
$ gcc traits2.cpp
/tmp/ccI7CNCY.o: In function `__static_initialization_and_destruction_0(int, int)':
traits2.cpp:(.text+0x36): undefined reference to `std::ios_base::Init::Init()'
traits2.cpp:(.text+0x3b): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccI7CNCY.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
traits2.ccp文件包含上述带有emtpy main()函数的解决方案:
#include <iostream>
using namespace std;
// A default Traits class has no information
template<class T> struct Traits
{
};
// A convenient way to get the Traits of the type of a given value without
// …
Run Code Online (Sandbox Code Playgroud)