如何通过终端Mac编译C++程序

use*_*353 1 c++ macos terminal compilation header-files

我有一个关于如何在终端Mac中编译C++程序的问题.我的程序有一个头文件和一个主文件.我知道我无法编译头文件和主文件.并只是编译主文件.我也知道我需要创建一个名称来存储编译文件.这是我使用的编译命令,g++ -o execute1 main.cpp我得到了这个:

Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
  _main in main-f2nZvj.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?任何帮助将不胜感激.如果有帮助,下面是我的两个文件的代码:

add.h:

int add(int x, int y);
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include <iostream>
#include "add.h"

int main(){
    using namespace std;
    cout << "The sum of 9 and 9 is " << add(9, 9) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*l R 5

你需要一个add.cpp实现你的add()功能的文件,然后你可以编译整个东西:

$ g++ -Wall main.cpp add.cpp -o execute1
Run Code Online (Sandbox Code Playgroud)