4

I don't want the gcc linker to show the undefined symbols as linkage errors. I'm using gcc 4.2 on MAC OSX 10.6

Of course I've looked online for a solution, but all the tries failed!

How can I modify this command to get rid of the undefined symbols error?

gcc -o Proj $(wildcard *.o);

Thanks.

Edit: I'm working on a research project where I need the code to be linkable & I don't care if it'll have runtime exceptions

Pansy
  • 320
  • 1
  • 5
  • 13
  • 3
    What does the command have to do with the question? Also, how to you expect to get an executable binary out of this if the linking phase fails? ... there is a reason why it is an error and not a warning ;) – 0xC0000022L Feb 08 '12 at 19:24
  • 3
    This is one of my favourite questions of all time. – Seth Carnegie Feb 08 '12 at 19:28
  • there are options to suppress the linkage errors like in here sourceware.org/binutils/docs/ld/Options.html I'm working on a research project where I need the code to be linkable & I don't care if it'll have runtime exceptions – Pansy Feb 08 '12 at 19:30
  • It's not related to runtime errors. It can't run if there's functions that don't exist. For instance, what if main is the linker error? What makes you think you can ignore these linker errors? – Mooing Duck Feb 08 '12 at 19:36
  • I already checked out and main is not the linker error. I just need the generated exe, I'm not going to run it, it's needed for research investigation – Pansy Feb 08 '12 at 19:40
  • 1
    @Pansy: Investigation into what? Have you considered that you are possibly asking the wrong question here? What are you trying to achieve with this? – Ed S. Feb 08 '12 at 20:05
  • This is an answerable question: compile with -undefined suppress and -flat_namespace. It's an odd thing to do but it arguably can be useful for dynamic loading. – Praxeolitic Sep 22 '14 at 00:08
  • @MooingDuck But of course you can run a program with functions that don't exist! Just one small caveat -- it'll segfault... ;) – Praxeolitic Sep 22 '14 at 00:16

3 Answers3

7

Huh? That makes no sense. It is an error and it cannot be ignored. You cannot build an executable when the linker fails to do its job. Would you expect it to simply invent an implementation and hope that it just works out?

Instead, you need to learn how to properly configure your project to build. If you depend on an external library then you need to add the path to it in your linker search paths.

EDIT: Per your comment...

there are options to suppress the linkage errors like in here sourceware.org/binutils/docs/ld/Options.html I'm working on a research project where I need the code to be linkable & I don't care if it'll have runtime exceptions

Well, you still can't and it still makes no sense. Read that section more carefully:

The reasons for allowing undefined symbol references in shared libraries specified at link time are that:

A shared library specified at link time may not be the same as the one that is available at load time, so the symbol might actually be resolvable at load time. There are some operating systems, eg BeOS and HPPA, where undefined symbols in shared libraries are normal.

So, there are some circumstances wherein allowing undefined symbols in a shared library are reasonable because you may not be linking to the same version of a dynamic library that you will be linking to in practice.

However, you want to build an executable, not a shared library, so it makes no sense to do what you are asking for. You simply have an executable that is borked and will not work. Why would any sane compiler allow for such a condition?

Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • there are options to suppress the linkage errors like in here http://sourceware.org/binutils/docs/ld/Options.html I'm working on a research project where I need the code to be linkable & I don't care if it'll have runtime exceptions – Pansy Feb 08 '12 at 19:29
  • @Pansy: I edited my response, but it's not what you are after. – Ed S. Feb 08 '12 at 20:04
  • 1
    The linked documentation is for GNU ld and doesn't apply to OS X but GNU ld will in fact allow you to generate executables with unresolved symbols using --unresolved-symbols=ignore-all. Doing so "makes sense" in that it is well defined, but it is not a sensible thing to do. Why would a sane compiler allow this? It's not the compiler's job to pass judgment. – Praxeolitic Sep 22 '14 at 00:22
2

You can use ld -r -o Proj.o $(wildcard *.o) instead.
What you'll get will not be an executable that you can run, but an object file, which you can link into an executable. At that final linkage, you'll have to provide the unresolved symbols.

ugoren
  • 16,023
  • 3
  • 35
  • 65
0

You probably want to create a library not an exe. These should not generate linker errors.

ar r libProj.a $(wildcard *.o)
ranlib libProj.a
Tarun
  • 639
  • 6
  • 3