0

I am looking for a way to suppress/satisfy the linker when a symbol is undefined at compile time. To illustrate what I am trying to accomplish I will put up a simple example:

CoreCpp.h file:

#include <stdio.h>

int calculate();

CoreCpp.cpp file:

#include "CoreCPP.h"

int calculate(){
    return 0;
}

With these two files above I will create a .a static library.

Then in Xcode I will create a simple command line project as you can see in the image below:

enter image description here

If I link the static library, the project will compile without any issue of course:

enter image description here

However, if I don't link the static library, it will throw an undefined symbol error.

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? Can we apply the concept of weak linking here?

zfgo
  • 195
  • 11
  • I don't see how the concept of *weak linking* is applicable here. – Eljay Jul 23 '20 at 03:13
  • 1
    You seem to be asking about _dynamic linking_, which is not possible when you have compiled your library for _static linking_. – paddy Jul 23 '20 at 03:15
  • @Eljay, I might be incorrect. I have an Objective C background and in Objective C there is a notion called weak linking or optional linking. – zfgo Jul 23 '20 at 03:30
  • @zfgo `__attribute__((weak_import))` should also work in C++, but it's generally used with dynamically linked libraries, not statically linked ones (though apparently you can make it work with the latter). See [this question](https://stackoverflow.com/questions/274753/how-to-make-weak-linking-work-with-gcc). – Miles Budnek Jul 23 '20 at 04:28
  • It would be helpful if you could explain what you want to achieve and why dynamic linking would not work for you. – t.niese Jul 23 '20 at 05:56
  • Weak-link of a framework (usually) requires a framework, rather than a static library. But from Miles's link, it appears you can get it to work (I learned something new). – Eljay Jul 23 '20 at 12:02

1 Answers1

0

I'm not familiar with Xcode, but its compilation/linking process should be similar to Eclipse. So first you should understand the differences between compilation, linking, and building. 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 a .o object file, which will be used later in the linking step. 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. 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. In Eclipse you need to create a Makefile-project and edit your Makefile alongside the project files.

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. 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.

Hack06
  • 963
  • 1
  • 12
  • 20