我正在尝试使用 -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
,因为 gcc
默认情况下不链接到 C++ STL,因此它给出了对 std::ios_base 的 undefined reference 。
g++ -Wall -DDEEPAK factorial.cpp -o main
关于c++ - 从命令行定义源文件的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123328/