繁体   English   中英

const extern C数组链接器多个定义

[英]const extern C-array linker multiple definitions

我有几个用于更新它们的源文件。 他们出于非常明显的原因拒绝编译(删除了所有不感兴趣的源代码):

// hashset.h
class HashSet {
    unsigned prime = 0; // Index to table size (c++11 syntax)
    unsigned long table_size () const { return prime_list [prime]; }
};

//hashet.cpp
const unsigned long prime_list [] = {53, 97, 193, 389, 769,
      1543, 3079, 6151, 12289, 24593, 49157, 98317};

从标头中的HashSet::table_size调用时, prime_list是未定义的。 我试图修复源,添加extern const unsigned long prime_list []; 此数组定义的标头和extern make clean && make我得到之后

g++ main.o dictionary.o hashset.o  -o spell
dictionary.o:(.rodata+0x4): multiple definition of `num_primes'
main.o:(.rodata+0x4): first defined here
dictionary.o:(.rodata+0x20): multiple definition of `prime_list'
main.o:(.rodata+0x20): first defined here
hashset.o:(.rodata+0x4): multiple definition of `num_primes'
main.o:(.rodata+0x4): first defined here
hashset.o:(.rodata+0x20): multiple definition of `prime_list'
main.o:(.rodata+0x20): first defined here
collect2: error: ld returned 1 exit status

正如我发现的那样,实际上如果只是prime_table被定义为

// hashset.cpp
extern const unsigned long prime_list [] = {53, 97, 193, 389, 769,
      1543, 3079, 6151, 12289, 24593, 49157, 98317};

并且将table_size ()的主体移到hashset.cpp (以避免编译错误),我得到了这些链接器错误。 没有extern我没有错误。 为什么会这样呢? 我对此数组只有一个定义,我不会以任何方式将其暴露给其他我的源文件(我现在不将extern const unsigned long prime_list [];放在标题中)。 如何对其进行乘法定义?

要明确的是,我现在的资料来源:

// hashset.h
class HashSet {
    unsigned prime = 0; // Index to table size (c++11 syntax)
    unsigned long table_size () const;
};

//hashet.cpp
extern const unsigned long prime_list [] = {53, 97, 193, 389, 769,
      1543, 3079, 6151, 12289, 24593, 49157, 98317};
unsigned long HashSet::table_size () const {
     return prime_list [prime];
}

现在, .cpp extern是不必要的,但是我只是想知道,这是怎么回事? 有没有一种方法可以将table_size()的主体table_size()到头文件中以使其内联?

upd prime_listprime_table是同一对象。 我在缩短此帖子的来源时错误地更改了名称; 现在修复。

删除外部。 应该没事的

为了将方法放入头文件,只需在头文件中用extern声明数组。

 extern const unsigned long prime_table [];

另外,您不会因为prime_table而收到错误,而是因为num_primes。

对我来说效果很好。

为项目tmpCheck构建配置调试**

制作所有建筑文件:../src/hashset.cpp调用:GCC C ++编译器g ++ -O0 -g3 -Wall -c -fmessage-length = 0 -MMD -MP -MF“ src / hashset.d” -MT“ src /hashset.d“ -o” src / hashset.o“” ../src/hashset.cpp“完成的构建:../src/hashset.cpp

构建文件:../src/tmpCheck.cpp调用:GCC C ++编译器g ++ -O0 -g3 -Wall -c -fmessage-length = 0 -MMD -MP -MF“ src / tmpCheck.d” -MT“ src / tmpCheck .d“ -o” src / tmpCheck.o“” ../src/tmpCheck.cpp“完成的建筑:../src/tmpCheck.cpp

构建目标:tmpCheck调用:GCC C ++链接器g ++ -o“ tmpCheck” ./src/hashset.o ./src/tmpCheck.o
完成的建筑目标:tmpCheck

构建完成**

 #ifndef HASHSET_H_
 #define HASHSET_H_

  extern const unsigned long prime_list [];
  // hashset.h
  class HashSet {
     unsigned prime ; // Index to table size (c++11 syntax)
   public:
      HashSet():prime(0){
        //
      }
      unsigned long table_size () const { return prime_list [prime]; }

  };
  #endif /* HASHSET_H_ */

   #include "hashset.h"
   //hashet.cpp
   const unsigned long prime_list [] = {53, 97, 193, 389, 769,
      1543, 3079, 6151, 12289, 24593, 49157, 98317};

我认为,如果@Adnan Akbar建议使用外部规范,则无法解决您的问题,那么可能是由于头文件的多次包含所致
因此,在这种情况下,有一个简单的解决方案可以解决您的问题:
使用条件翻译指令
#pragma在您的“ hashset.h”中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM