Linking Libraries with Duplicate Class Names using GCC

前端 未结 4 729
野的像风
野的像风 2020-12-17 18:43

Is there a way for GCC to produce a warning while linking libraries that contain classes with the same name? For example

Port.h

class Port {         


        
相关标签:
4条回答
  • 2020-12-17 19:15

    I don't see any options to do what you want either. Maybe think laterally and use a code documentation tool like Doxygen to generate documentation for all your classes and manually look for duplicates

    0 讨论(0)
  • 2020-12-17 19:17

    The following might be worth a try (I honestly don't know if it'll do what you want):

    --whole-archive

    For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

    I haven't tried it, but it sounds as if it'll pull in all the items in a library as if they were object files. You'll need to specify the option for each library.

    As Neil said, this won't give you class-level conflicts, but if there are class members with the same signature, this might make the linker tell you.

    0 讨论(0)
  • 2020-12-17 19:19

    No there isn't. The reason for this is that a class name is not a symbol as far as the linker is concerned. The C++ compiler will have used the class name to produce mangled function names, but the class name itself is gone by the time the linker gets involved.

    0 讨论(0)
  • 2020-12-17 19:40

    Alternatively you could use a script utilizing nm to find candidates, e.g. something like:

    import collections, subprocess, os, re, sys
    
    def filt(s):
      p = subprocess.Popen(['c++filt', s],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      return p.communicate()[0].strip()
    
    rx  = re.compile('^[\\d\\w]+ T [\\w]+', re.MULTILINE)
    sym = collections.defaultdict(set)
    
    for file in sys.argv[1:]:
      proc = subprocess.Popen(['nm', file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      for s in rx.findall(proc.communicate()[0]):
        sym[s.split()[2]].add(file)
    
    for s in filter(lambda x: len(sym[x])>1, sym):
        print 'Duplicate "%s" in %s' % (filt(s), str(sym[s]))
    

    foo:$ python dups.py *.a  
    Duplicate "Port::me()" in set(['Port.a', 'FakePort.a'])
    
    0 讨论(0)
提交回复
热议问题