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
How to compile C++ program by command line on Mac
提问by user2261693
I used to use Xcode
to build and run C++
program.
我曾经用来Xcode
构建和运行C++
程序。
I use command line to compile the same source code in my Xcode
project.
我使用命令行在我的Xcode
项目中编译相同的源代码。
Compiling a individual .cpp
file is OK.
编译单个.cpp
文件是可以的。
Compiling more complicated project(more than one file) is NOT OK.
编译更复杂的项目(多个文件)是不行的。
I have tried gcc
, g++
, clang
, clang++
.
我已经试过gcc
,g++
,clang
,clang++
。
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) 标志。构建项目的步骤:
Compile all of the source files:
c++ -c file1.cpp c++ -c file2.cpp c++ -c file3.cpp
Link your final executable:
c++ file1.o file2.o file3.o
You can use an optional
-o
flag to specify the output program name. It probably defaults toa.out
.
编译所有源文件:
c++ -c file1.cpp c++ -c file2.cpp c++ -c file3.cpp
链接您的最终可执行文件:
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
例如 )来自动化此过程,否则您会发现它非常容易出错。