简体   繁体   English

如何抑制 C++ 中的未定义符号错误?

[英]How to suppress undefined symbol error in C++?

I am looking for a way to suppress/satisfy the linker when a symbol is undefined at compile time.我正在寻找一种在编译时未定义符号时抑制/满足 linker 的方法。 To illustrate what I am trying to accomplish I will put up a simple example:为了说明我要完成的工作,我将举一个简单的例子:

CoreCpp.h file: CoreCpp.h 文件:

#include <stdio.h>

int calculate();

CoreCpp.cpp file: CoreCpp.cpp 文件:

#include "CoreCPP.h"

int calculate(){
    return 0;
}

With these two files above I will create aa static library.使用上面的这两个文件,我将创建一个 static 库。

Then in Xcode I will create a simple command line project as you can see in the image below:然后在 Xcode 中,我将创建一个简单的命令行项目,如下图所示:

在此处输入图像描述

If I link the static library, the project will compile without any issue of course:如果我链接 static 库,项目将毫无问题地编译:

在此处输入图像描述

However, if I don't link the static library, it will throw an undefined symbol error.但是,如果我不链接 static 库,则会抛出未定义符号错误。

Undefined symbols for architecture x86_64:
"calculate()", referenced from:
    _main in main.o
ld: symbol(s) not found for architecture x86_64

Question : Is there a way that I can suppress the error or somehow make the linker happy at compile time?问题:有没有办法可以抑制错误或以某种方式使 linker 在编译时满意? Can we apply the concept of weak linking here?我们可以在这里应用弱链接的概念吗?

I'm not familiar with Xcode, but its compilation/linking process should be similar to Eclipse.我不熟悉Xcode,但它的编译/链接过程应该类似于Eclipse。 So first you should understand the differences between compilation , linking , and building .因此,首先您应该了解compilationlinkingbuilding之间的区别。 Then you should also realize that there are 2 ways of building your project: either compile your source code files separately and then link them together to get machine-executable instructions, OR to compile and link in one single step/command.然后您还应该意识到有两种构建项目的方法:分别编译源代码文件,然后将它们链接在一起以获得机器可执行指令,或者在一个步骤/命令中编译链接 In the step of compilation only, you can either get an output of a static or dynamic library, or ao object file, which will be used later in the linking step.仅在编译步骤中,您可以获取 static 或动态库的 output 或动态库,或者 object 文件,稍后将在链接步骤中使用。 With separate steps of compile/link you have more control over your build process.通过单独的编译/链接步骤,您可以更好地控制构建过程。 What Eclipse (and supposedly Xcode) is doing when you build your project, is either of those 2 ways, depending on your project settings.当您构建项目时,Eclipse(据说是 Xcode)正在做什么,是这两种方式中的任何一种,具体取决于您的项目设置。 If you'd like to take a full control over the process of building in Eclipse or whatever IDE you have, you should take care of your project settings, or even better to set it up to work with a Makefile , where you can define exactly how your source code should be compiled and linked and in which steps/order.如果您想完全控制在 Eclipse 或任何 IDE 中构建的过程,您应该注意您的项目设置,或者更好地设置它以使用Makefile您的源代码应该如何编译和链接以及在哪些步骤/顺序中。 In Eclipse you need to create a Makefile-project and edit your Makefile alongside the project files.在 Eclipse 中,您需要创建一个Makefile-project并在项目文件旁边编辑您的Makefile

In this your question above, what you were trying to do (or asking for help) is forcing the IDE to build your project without specifying the library, for which obviously your linker was complaining about.在你上面的这个问题中,你试图做的(或寻求帮助)是强迫 IDE 在没有指定库的情况下构建你的项目,显然你的linker正在抱怨。 Instead, if you specify the library it doesn't complain any more.相反,如果您指定库,它就不会再抱怨了。 But be careful, you should make sure your IDE settings are fine to (re)compile your library from its source upon changes, otherwise you may end-up linking an old version of the compiled library file with your main.cpp and other files.但是请注意,您应该确保您的 IDE 设置可以在更改时从源代码(重新)编译您的库,否则您最终可能会将编译后的库文件的旧版本与您的 main.cpp 和其他文件链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM