3

Is there a compiler option for ignoring undefined references? I'm compiling C++ code under Linux.

leemes
  • 44,967
  • 21
  • 135
  • 183
vir2al
  • 817
  • 4
  • 11
  • 15
  • 7
    Why ignore when you can fix? - http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Luchian Grigore Jan 21 '14 at 13:57
  • 1
    The only way to ignore these errors is to look somewhere else ;) – Trenin Jan 21 '14 at 13:59
  • possible duplicate of [Can GCC not complain about undefined references?](http://stackoverflow.com/questions/5555632/can-gcc-not-complain-about-undefined-references) – R. Martinho Fernandes Jan 21 '14 at 14:37

4 Answers4

8

No. This is impossible. The linker physically cannot produce an output executable with an undefined reference. It's not a choice that the linker makes.

It's kind of like saying, "Sort this list, but I won't tell you what the list is" or "Solve my problem but I only dumped the code".

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 5
    No reflection on your answer but this is what is crazy about SO. 100 points for stating the obvious and some answer you make today that took time and thought will get 20. – Duck Jan 21 '14 at 14:04
  • 1
    Never say never. Some linkers let you combine .o files into a new .o file. Of course this new .o file will have undefined references. – brian beuning Jan 21 '14 at 14:04
  • A .o is not an executable though. – thecoshman Jan 21 '14 at 14:18
  • 7
    Nonsense. Depends on executable format, and ELF supports this just fine. -1, sorry. – Cat Plus Plus Jan 21 '14 at 14:27
  • No, it just doesn't feature undefined references at executable link time, since apparently all missing references are happily defined as "Patched in later at execution time". The dynamic linker will still face exactly the same problem. – Puppy Jan 21 '14 at 14:36
  • 2
    I never tried, but what doest lds `--unresolved-symbols=ignore-all` exactly do then? I would guess it compiles, links, and crashes at runtime. – PlasmaHH Jan 21 '14 at 14:44
  • Since a failed runtime DLL import often causes the process to just disappear without trace, with no error messages or exception boxes, I try hard to not dynamically load DLLs. – Martin James Jan 21 '14 at 14:54
3

You are not compiling, you are linking. The undefined reference error occurs when the program you are linking calls a function which is not defined inside the program parts you are linking together. Ignoring that error would mean the program calls a function that does not exist - what should happen then?

So the short answer is: theres no such option, and it would not make sense.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
2

For the below I am assuming you are referring to linker errors not compiler errors. Take a look at this: Can GCC not complain about undefined references?

This was useful for me as I am writing testcases which that use a bunch of stuff in a c file. The problem for me was that there were functions defined in this same C file which included references to undefined functions. That is, they are undefined for me in my limited test scope but I am not calling these functions. Therefore I want it to compile and link without error. There are other solutions as well such as this: Is there a way to ignore an unused undefined references? but but I couldn't get it to work (probably because I have an older version of gcc)

Community
  • 1
  • 1
Gregory Kuhn
  • 1,627
  • 2
  • 22
  • 34
1

The compiler already does ignore undefined references, quite by design.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055