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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20335437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:03:41  来源:igfitidea点击:

How to compile a C++ program via Terminal Mac

c++macoscompilationterminalheader-files

提问by user2967353

I have a question on how to compile a C++ program in Terminal Mac. My program has a header file and a main file. I know that I can't compile both the header file and the main file. and just to compile the main file. I also know that I need to create a name for storing the compiled file. Here is my compile command that I used g++ -o execute1 main.cppand I get this:

我有一个关于如何在终端 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)

How can I fix this? Any help will be greatly appreciated. If it helps, below is my code for the two files:

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

add.h:

添加.h:

int add(int x, int y);

main.cpp:

主.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;
}

回答by Paul R

You need an add.cppfile that implements your add()function, then you can compile the whole thing as:

您需要一个add.cpp实现您的add()功能的文件,然后您可以将整个内容编译为:

$ g++ -Wall main.cpp add.cpp -o execute1

回答by nhgrif

This line:

这一行:

int add(int x, int y);

in your add.hjust tells the compiler that somewhere, there's a function called addthat takes two integers and returns an integer. Having this and this alone will let the compiler leave you alone when you use this addfunction in files that #include "add.h". The compiler doesn't have to know exactly what the function does, it just needs to know what parameters it accepts and what the function returns. It doesn't bother looking for the function body until it actually goes to compile the function.

在你add.h只是告诉编译器在某处,有一个函数被调用add,它接受两个整数并返回一个整数。当您add#include "add.h". 编译器不必确切地知道函数做了什么,它只需要知道它接受什么参数以及函数返回什么。在实际编译函数之前,它不会费心寻找函数体。

In order for this to properly compile, you need to include a function body for your addfunction in add.cpp. Even just this will work:

为了正确编译,您需要addadd.cpp. 即使只是这样也会起作用:

int add(int x, int y) {
    return 1;
}

This will allow the program to compile because now the compiler know what code it's supposed to execute when it gets to your call to the addfunction within main.

这将允许该程序编译,因为现在编译器知道是什么代码,它应该当它到达你的电话到执行add中的功能main

This will work, as a minimum, as a placeholder until you're ready to actually write the exact logic you want this function to contain. But until this function body exists, you won't be able to compile (unless you remove all the other references to the function).

这将至少用作占位符,直到您准备好实际编写您希望此函数包含的确切逻辑为止。但是在此函数体存在之前,您将无法编译(除非您删除对该函数的所有其他引用)。