簡體   English   中英

C ++中頭文件的結構,並通過Terminal Mac進行編譯

[英]structure of header files in C++ and compiling via Terminal Mac

我對頭文件的結構感到困惑。 在我的程序中,頭文件只是具有一個正向聲明int add(int x, int y); 在main方法中,它調用頭文件,但不使用任何return語句初始化前向聲明,它僅分配x和y特定值。 換句話說,當頭文件中的代碼沒有任何返回類型時,main方法如何知道如何處理頭文件中的代碼。 任何幫助將不勝感激。 如果有幫助,下面是這兩個文件的代碼:

add.h:

int add(int x, int y); // function prototype for add.h

main.cpp:

#include <iostream>
#include "add.h" // this brings in the declaration for add()

int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}

我用g++ add.h main.cpp execution1; ./execution1從Terminal編譯它g++ add.h main.cpp execution1; ./execution1 g++ add.h main.cpp execution1; ./execution1 ,我收到此錯誤:

clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated
clang: error: cannot specify -o when generating multiple output files
-bash: ./execution1: No such file or directory

它是重復的,請參見此處: 如何在Mac上通過命令行編譯C ++程序

但是你回答...

您可以使用c++ -c main.cpp ,然后使用c++ main.o ,它將生成.exe

再見!

暫無
暫無

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

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