从命令行定义源文件的宏

Defining macros for a source file form the command line

本文关键字:源文件 定义 命令行      更新时间:2023-10-16

>我正在尝试使用 -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的未定义引用。

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