簡體   English   中英

在G ++中編譯標頭和cpp文件

[英]Compiling header and cpp files in g++

我在使用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(因此包括ah ),而類A使用類B(因此包括bh )。

那么,如何編譯該程序以生成執行文件?

我嘗試使用以下命令,但它反饋了錯誤:

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

我的操作系統是ubuntu 12.04。 我知道如果我用dev c ++之類的東西制作一個項目,它將自動將文件鏈接在一起,但是我不想使用它。 我也看過其他鏈接和資源,但找不到對自己有用的東西。

嘗試

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

這將創建一個可執行文件a.out -Wall標志僅告訴編譯器發出更多警告,而-O2標志啟用優化。 最后三個參數是您的源文件。 您需要以任何方式將所有非#include :d的源文件提供給g++

您可以使用-o (name)選項指定可執行文件名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM