Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convince Xcode to emit a duplicate symbol linker error?

Here's a different one from the usual confusion over duplicate symbol errors... :-)

I'm working on some legacy Mac code in an Xcode project that has the same global, "trace", defined in several different source files - for instance:

  • File1.c: SInt32 trace;
  • File2.c: Boolean trace;

etc. It's clear the original author meant them to have file-specific scope, but just neglected to prefix any of these lines with "static". That's fine, easy enough to fix.

But I'm kind of shocked the linker isn't flagging these! It looks to me like Xcode's linker (I presume gnu ld) only emits duplicate symbol warnings or errors for functions, that are linked into the code segment - but not global variables that are linked into the data segment. Instead, it silently conflates them, which is causing bugs.

So... how do I convince Xcode to emit link errors for duplicate global variables? Or get this information in some other way that can be a routine part of my build?

like image 555
Bob Murphy Avatar asked Mar 23 '11 20:03

Bob Murphy


1 Answers

Well, I thought I'd answered my own question... :-)

I posted earlier:

So if you're using Xcode with LLVM GCC 4.2, go to the build settings dialog, find the "LLVM GCC 4.2 - Code Generation" section, and check the "No Common Blocks" checkbox. This enables the compiler's "-fno-common" option, and changes the object file generation so that ld will choke and emit an error if you have two globals in different source files with the same name.

Unfortunately, that doesn't seem to solve all instances. It seems to work fine if all the globals have the same type.

But the example in the question is taken straight from the code, where a variable named "trace" is defined as a global in two different files with two different types. And that's still not caught by the build system when I check that checkbox.

like image 134
Bob Murphy Avatar answered Nov 15 '22 05:11

Bob Murphy