gcc: Allow Undefined Symbols [closed]

2019-07-04 01:00发布

问题:

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

回答1:

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?



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



回答3:

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


标签: c gcc linker