从命令行定义源文件的宏

时间:2015-01-24 07:28:01

标签: c++ macros

我正在尝试使用-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

任何帮助都将深表感谢。感谢。

1 个答案:

答案 0 :(得分:2)

您应该在命令中使用g++而不是gcc,因为默认情况下gcc没有链接到C ++ STL,因此它给{{1}提供了未定义的引用}}。

std::ios_base