Question

I'm trying to compile a large source tree with clang whose default settings for debug mode until now where -O3. This unfortunately optimizes away a lot of the crucial debug info, but when I set the Optimization level in xcode to anything less than -O2, say -O0, The linker starts giving duplicate symbol errors.

duplicate symbol __Z7CONTROLSs12CONTROL_TYPEff in:
    /Users/... /main.o
    /Users/... /ofxCocoa.o
    ...
ld: 8 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1

So Two questions:

  1. Why, in theory, should optimization levels change the linker output?
  2. Does anyone have any idea how I can remedy this problem? I can't really post the code, but helpful guesses will be gladly accepted.
Was it helpful?

Solution

duplicate symbol __Z7CONTROLSs12CONTROL_TYPEff

This usually happens when you define an inline function in a header file, but neglect to declare it with inline keyword.

Why, in theory, should optimization levels change the linker output?

When you build with optimization, the compiler actually does inline the function, and doesn't emit "out of line" definition, resulting in successful link.

You can verify this by running nm main.o | grep __Z7CONTROLSs12CONTROL_TYPEff -- with optimization on, I expect no output.

how I can remedy this problem

Add missing inline, or move the definition of the function out of header file, and into a regular C++ source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top