【问题标题】:Defining macros for a source file form the command line从命令行为源文件定义宏
【发布时间】: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 如果答案有帮助,您应该接受它。

标签: c++ macros


【解决方案1】:

您应该在命令中使用g++ 而不是gcc,因为默认情况下gcc 不链接到C++ STL,因此它提供了对std::ios_base 的未定义引用。

g++ -Wall -DDEEPAK  factorial.cpp -o main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    相关资源
    最近更新 更多