c++ - 从命令行定义源文件的宏

标签 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

任何帮助将不胜感激。谢谢。

最佳答案

您应该在命令中使用 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/

相关文章:

c++ - 如果 send() 返回 x 个字节,recv() 是否在一次调用中获得相同数量的字节?

c++ - 位域和编译指示

c++ - 使用 std::getline() 读取一行?

以下 C 宏会导致问题吗?

c++ - 用于探索由 c/c++ 编写的宏的 vim 插件

ms-access - 如何导出 Access 2010 数据宏

c++ - 如何调用在 main() 内部但在外部范围内定义的变量

c++ - Linux组和用户管理

c - 为什么 GCC 保留空函数?

javascript - Imacros - 使用 Javascript 循环多个宏特定次数