用g ++编译头文件和cpp文件

Ham*_*FzM 1 c++ compiler-errors g++ header header-files

我有一个问题,使用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
Run Code Online (Sandbox Code Playgroud)

在程序中,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
Run Code Online (Sandbox Code Playgroud)

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

rik*_*und 7

尝试

g++ -Wall -O2 main.cpp a.cpp b.cpp
Run Code Online (Sandbox Code Playgroud)

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

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