【问题标题】:Very basic: Why won't my makefile work with common suffix非常基本:为什么我的 makefile 不能使用通用后缀
【发布时间】:2017-02-23 07:37:37
【问题描述】:
objects = hello.o name.o printing.o
exename = himake

$(exename): $(objects)
    $(CC) -o $(exename) $(objects)

%.o: %.cpp
    $(CC) -c  $^

我正在尝试使用常见的后缀,所以我不需要先将 3 个文件编译成 .o。这应该使用 % 通配符完成所有三个操作。

当我这样做时效果很好,但不是这个。

运行上面的 makefile 会出现以下错误:

[alex@pcc Dir]$ make
cc -o himake hello.o name.o printing.o
hello.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
hello.o: In function `__tcf_0':
hello.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()'

还有更多我没有包含的内容

文件: 你好.cpp:

// hello.cpp

// standard library
#include <iostream>
#include <string>
using namespace std;

// user defined header files
#include "name.h"
#include "printing.h"

int main ()
{
    string name;

    name = getName();   // getName is in name.h
    printHello(name);  // printHello is in print.h

    return 0;
}

名称.cpp

// name.cpp

// user defined header files
#include "name.h" 
#include "printing.h"

string getName()
{
    string name;
    printGreeting();    // printGreeting is from print.h
    getline(cin, name);  
    return name;
}

名称.h

// name.h

#include <iostream>
using namespace std;

string getName();

打印.cpp

// printing.cpp

// user defined include files
#include "printing.h"

void printGreeting(void)
{
    cout << "Your name: ";
    return;
}

void printHello (string  name)
{
    cout <<  "Hi, " << name << endl;
    return;
}

打印.h

// printing.h

#include <iostream>
using namespace std;

void printGreeting();
void printHello(string);

【问题讨论】:

  • 使用$CXX编译c++代码。
  • 另外,请重新考虑您对通常被认为是不良做法的使用:using namespace std;(尤其是在标题中)和endl(这些是解释的链接)。

标签: c++ makefile gnu-make


【解决方案1】:

因为您使用的是 C 编译器前端程序,而不是 C++ 前端程序。

$(CC) 更改为$(CXX)

【讨论】:

  • 哦,所以 gcc 与 $(CC) 不同?
  • 我刚刚研究了一下。是的,我认为它适用于 cxx 而不是 cc
  • @fman "gcc" 是 Gnu 编译器集合。它也是用于访问该集合中的 C 编译器的名称。要访问该集合中的 C++ 编译器,请使用 g++。在 makefile 中,每个编译器名称都有宏,因为您可能在不同的平台上使用不同的编译器(例如,clang++)。 C 编译器的宏是CC,C++ 编译器的宏是CXX
  • @fman 在典型的 Linux 系统上,命令 ccgcc 的别名,c++g++ 的别名。变量$(CC) 默认为cc,变量$(CXX) 默认为c++。因此,如果您使用$(CXX),您将使用c++ 命令,它实际上是g++,它是C++ 的GCC 前端程序。
  • 谢谢,对这一切感到困惑。所以 cc ->> gcc 和 cxx ->g++,谢谢你的详细介绍
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多