【发布时间】:2015-01-24 07:28:01
【问题描述】:
我正在尝试使用 -D 标志从 ubuntu 系统上的命令行为源文件定义一个宏。 源文件是:
#include<iostream>
using namespace std;
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
#ifdef DEEPAK
cout<<"hello"<<endl;
#endif
}
int main()
{
factorial(4);
return(0);
}
我输入的命令是:
gcc -Wall -DDEEPAK factorial.cpp -o main
但是,我得到了错误:
/tmp/cc4Ii5l2.o: In function `__static_initialization_and_destruction_0(int, int)':
factorial.cpp:(.text+0x63): undefined reference to `std::ios_base::Init::Init()'
factorial.cpp:(.text+0x72): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
任何帮助将不胜感激。谢谢。
【问题讨论】:
-
你应该使用 g++ 而不是 gcc 。你的 cout 在这里没用。
-
调用 g++ 而不是 gcc,默认情况下 gcc 不链接 c++ 标准库,因此未解决对 std::cout 的引用。宏定义得恰到好处。
-
非常感谢。唉,一个非常愚蠢的错误。
-
你应该用
g++ -Wall -Wextra -g编译 -
@DeepakSaini 如果答案有帮助,您应该接受它。