【问题标题】:Compiling header and cpp files in g++在 g++ 中编译头文件和 cpp 文件
【发布时间】:2014-03-10 18:55:49
【问题描述】:

我在使用 GNU g++ 编译多个文件时遇到问题,请帮助我。

我们假设有五个文件:

main.cpp : the main function 
a.h      : the header file of class A 
a.cpp    : the definition of class A 
b.h      : the header file of class B 
b.cpp    : the definition of class B

在程序中,main.cpp 使用 A 类(因此包括a.h),A 类使用 B 类(因此包括b.h)。

那么,我该如何编译这个程序来生成执行文件呢?

我尝试使用以下命令,但它反馈错误:

undefined reference to ~(objects in class A and B)". 
command: g++ -Wall -O2 -s main.cpp

我的操作系统是 ubuntu 12.04。我知道如果我用 dev c++ 之类的东西制作一个项目,它会自动将文件链接在一起,但我不想使用它。我还看到了其他链接和来源,但我找不到对自己有用的东西。

【问题讨论】:

    标签: c++ header compiler-errors g++ header-files


    【解决方案1】:

    试试

    g++ -Wall -O2 main.cpp a.cpp b.cpp
    

    这将创建一个程序a.out,它是可执行文件。 -Wall 标志只是告诉编译器发出更多警告,-O2 标志启用优化。最后三个参数是您的源文件。您需要以任何方式将所有不是#include:d 的源文件提供给g++

    您可以使用选项-o (name) 指定可执行文件名称。

    【讨论】:

    • 当然。我现在添加了该信息。如果您需要更多详细信息,请尝试man g++
    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2014-03-24
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    相关资源
    最近更新 更多