Linking Libraries with Duplicate Class Names using GCC

前端 未结 4 826
野的像风
野的像风 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: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'])
    

提交回复
热议问题