简体   繁体   English

C ++中头文件的结构,并通过Terminal Mac进行编译

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

I am confused on the structure of header files. 我对头文件的结构感到困惑。 In my program, the header file just has a forward declaration of int add(int x, int y); 在我的程序中,头文件只是具有一个正向声明int add(int x, int y); and in the main method, it calls the header file but does not initialize the forward declaration with any return statements, it just assigns the x and y particular values. 在main方法中,它调用头文件,但不使用任何return语句初始化前向声明,它仅分配x和y特定值。 In other words, how does the main method know what to do with the code inside the header files when the code in the header file does not have any return type. 换句话说,当头文件中的代码没有任何返回类型时,main方法如何知道如何处理头文件中的代码。 Any help will be greatly appreciated. 任何帮助将不胜感激。 If it helps, below is the code for the two files: 如果有帮助,下面是这两个文件的代码:

add.h: add.h:

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

main.cpp: 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;
}

and i compile it from Terminal by using g++ add.h main.cpp execution1; ./execution1 我用g++ add.h main.cpp execution1; ./execution1从Terminal编译它g++ add.h main.cpp execution1; ./execution1 g++ add.h main.cpp execution1; ./execution1 and I get this error: 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

It's a duplicate, see here: How to compile C++ program by command line on Mac 它是重复的,请参见此处: 如何在Mac上通过命令行编译C ++程序

But you answer... 但是你回答...

You can use c++ -c main.cpp , then c++ main.o , it will generate the .exe . 您可以使用c++ -c main.cpp ,然后使用c++ main.o ,它将生成.exe

Bye! 再见!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM