在 xcode 中设置 C++ 编译标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31346150/
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
Setting C++ compile flags in xcode
提问by R4j
I faced with the same issue for this question: Undefine symbols for architecture x86_64 using FFTW
And I tried to use flag -L and -l for C++ in xcode, but it doesn't work
Here is the error log:
对于这个问题,我遇到了同样的问题:使用 FFTW 为体系结构 x86_64 取消定义符号
我尝试在 xcode 中为 C++ 使用标志 -L 和 -l,但它不起作用
这是错误日志:
clang: warning: -lsndfile: 'linker' input unused
clang: warning: -lfftw3: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'
Undefined symbols for architecture x86_64:
"_fftw_destroy_plan", referenced from:
_main in main.o
"_fftw_execute", referenced from:
_main in main.o
"_fftw_plan_dft_r2c_1d", referenced from:
_main in main.o
"_sf_close", referenced from:
_main in main.o
"_sf_open", referenced from:
_main in main.o
"_sf_read_double", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But if I compile with gcc
in command line, it works well.
但是如果我gcc
在命令行中编译,它运行良好。
gcc -I/Users/sr2/Documents/Soft/fftw-3.3.4 -I/usr/local/include
-L/usr/local/lib -lfftw3 -lsndfile main.c -o fft_sample
where am I wrong?
我错在哪里?
回答by celticminstrel
Instead of putting these under "Other C/C++ Flags", they should go under "Other Linker Flags" (in the Linking section).
它们不应放在“其他 C/C++ 标志”下,而应放在“其他链接器标志”下(在链接部分)。
(Note that my XCode is old, so it may be slightly different for your version.)
(请注意,我的 XCode 很旧,因此您的版本可能略有不同。)
You might wonder, why is this necessary?
你可能想知道,为什么这是必要的?
Well, when you build your project, there are several stages to go through. The most basic breakdown is into compilingand linking. (They could perhaps be broken down further, but that's the important distinction here.)
好吧,当您构建项目时,需要经历几个阶段。最基本的分解是编译和链接。(它们也许可以进一步细分,但这是这里的重要区别。)
The compilertakes a source file (eg, example.cpp) and outputs an object file (such as example.o). An object file is not executable. When compiling, the compiler generally only knows about the one source file that it's currently processing. Thus the compiler doesn't need to know which libraries you're using - all it needs to know is where the header files are.
所述编译器需要一个源文件(例如,example.cpp),并输出一个目标文件(例如example.o)。目标文件不可执行。编译时,编译器通常只知道它当前正在处理的一个源文件。因此,编译器不需要知道您正在使用哪些库——它只需要知道头文件在哪里。
The linkertakes one or more object files and combines them together to create an executable binary. At this point, it must also resolve any external symbols not defined in your code - for example, symbols defined in an external library. For that reason, the linker needs to know about any libraries you're using.
该连接器需要一个或多个对象文件,并结合在一起,创建一个可执行二进制文件。此时,它还必须解析任何未在您的代码中定义的外部符号 - 例如,在外部库中定义的符号。因此,链接器需要了解您正在使用的任何库。
The compiler does not know what to dowith an -l
or -L
flag - they're not relevant to the process of compiling your code into an object file.
编译器不知道做什么用-l
或-L
标志-他们没有相关的编译代码成目标文件的过程。
When you invoke gcc
from the command-line like you demonstrated, it automatically invokes the linker for you and forwards those -l
and -L
flags to it. Because of this, no object file is produced on disk, and you get an executable file.
当您gcc
像演示的那样从命令行调用时,它会自动为您调用链接器并将这些-l
和-L
标志转发给它。因此,不会在磁盘上生成目标文件,您会得到一个可执行文件。
However, when you build through XCode, it does things a little differently. It invokes the compiler once for each of your source files, producing an object file like I described above. (This is the reason why you can specify extra compiler flags for specific source files in the Build Phases -> Compile Sources section.) Because the compiler has been asked to produce an object file, it does not invoke the linker, and since you're trying to pass it flags that should be forwarded to the linker, you get that warning that the flags are not used.
但是,当您通过 XCode 构建时,它的处理方式略有不同。它为您的每个源文件调用一次编译器,生成一个如上所述的目标文件。(这就是您可以在 Build Phases -> Compile Sources 部分为特定源文件指定额外编译器标志的原因。)因为编译器被要求生成一个目标文件,它不会调用链接器,并且由于您重新尝试将应该转发给链接器的标志传递给它时,您会收到未使用标志的警告。
Once all the source files have successfully compiled, XCode next invokes the linker directly to combine them all into a single executable binary. This is the stage that needs to know about your libraries. (Incidentally, in any large project, this method is generally preferable even if you're not using XCode.)
一旦所有源文件都成功编译,XCode 接下来直接调用链接器将它们全部组合成一个可执行的二进制文件。这是需要了解您的库的阶段。(顺便说一句,在任何大型项目中,即使您不使用 XCode,这种方法通常也是可取的。)
回答by Sound Blaster
You need probably add
你可能需要添加
-lstdc++
to the Other Linker Flags
in Build Settings
of your Project.
到Other Linker Flags
在Build Settings
你的项目中。