xcode 如何在 Mac 上通过命令行编译 C++ 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16127573/
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-09-15 03:15:31  来源:igfitidea点击:

How to compile C++ program by command line on Mac

xcodegccg++clang

提问by user2261693

I used to use Xcodeto build and run C++program.

我曾经用来Xcode构建和运行C++程序。

I use command line to compile the same source code in my Xcodeproject.

我使用命令行在我的Xcode项目中编译相同的源代码。

Compiling a individual .cppfile is OK.

编译单个.cpp文件是可以的。

Compiling more complicated project(more than one file) is NOT OK.

编译更复杂的项目(多个文件)是不行的。

I have tried gcc, g++, clang, clang++.

我已经试过gccg++clangclang++

The main problem is undefined symbol.

主要问题是未定义的符号。

Could you show how to compile complicated project (more than one file) by command line?

你能展示如何通过命令行编译复杂的项目(多个文件)吗?

回答by Carl Norum

From your description, it sounds like you're not using the -c(compile, but don't link) flag. Steps to build your project:

从您的描述来看,您似乎没有使用-c(compile, but don't link) 标志。构建项目的步骤:

  1. Compile all of the source files:

    c++ -c file1.cpp
    c++ -c file2.cpp
    c++ -c file3.cpp
    
  2. Link your final executable:

    c++ file1.o file2.o file3.o
    

    You can use an optional -oflag to specify the output program name. It probably defaults to a.out.

  1. 编译所有源文件:

    c++ -c file1.cpp
    c++ -c file2.cpp
    c++ -c file3.cpp
    
  2. 链接您的最终可执行文件:

    c++ file1.o file2.o file3.o
    

    您可以使用可选-o标志来指定输出程序名称。它可能默认为a.out.

It would be better to use some kind of build tool (like make, for example) to automate this process, or you'll find it to be very error prone.

最好使用某种构建工具(make例如 )来自动化此过程,否则您会发现它非常容易出错。