【问题标题】:GSL multiple error handler definition error when linking链接时GSL多个错误处理程序定义错误
【发布时间】:2019-02-21 22:06:11
【问题描述】:

我目前正在尝试在另一个自写库 (Quaternion.c) 中使用我以前编写的库 (matrix.c),方法是使用“matrix.h”的标准方法通过头文件调用它" 文件,其中包含来自“matrix.c”文件的函数原型。

“matrix.c”可以正常工作,但是当我尝试在 MSYS2 MinGW64 中编译“Quaternion.c”时,使用:

gcc -c matrix.c 

gcc -c Quaternion.c 

gcc matrix.o Quaternion.o -o Quaternion -lgsl

我收到此错误:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Quaternion.o:Quaternion.c:(.text+0x0): multiple definition of `my_handler'; matrix.o:matrix.c:(.text+0x0): first defined here

collect2.exe:错误:ld 返回 1 个退出状态

我在 GSL“errono.h”中定义了一个自定义错误处理程序,我假设这是错误的来源:

void my_handler (const char * reason, const char * file,  int line, int gsl_errno){

  if(gsl_errno == 3 || gsl_errno == 7 || gsl_errno == 8 || gsl_errno == 10){
    printf("Fatal memory-based error %d, resetting\n", gsl_errno);
    exit(0);
    //Reset Chip
  }
  if(gsl_errno == 1 || gsl_errno == 2 || gsl_errno == 4){
    printf("User error %d (%s), ignoring calculation and moving forward\n", gsl_errno, reason);
    // Return/ignore calculation
  }
  else
    printf("Unexpected error %d, ignoring and moving forward\n", gsl_errno);
    // Return/ignore calculation
}

我无法解决这个错误,因为 gsl 库是我已经编辑过的单一来源。如果有人能解释为什么我的编译方法(我想就是这样)导致该处理程序的重新定义,我们将不胜感激。

【问题讨论】:

  • Quaterion 是错字吗?
  • @Yunnosch 是的,对不起。修复了

标签: c gcc error-handling compiler-errors gsl


【解决方案1】:

将代码引用移到其中一个 .c 文件中。
然后只提供.h文件中的原型。

void my_handler (const char * reason, const char * file,  int line, int gsl_errno);

否则,该函数会为每个编译的 .c 文件定义一次,并对该标头执行 #include。在你的情况下可能两次。

在这种情况下,重新包含保护不会有帮助,因为它仍然会在每个编译的 .c 文件中发生一次。但是有一个守卫仍然是一个好主意,我想你有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 2011-10-02
    • 2014-08-27
    • 2018-04-29
    • 2011-06-01
    • 1970-01-01
    • 2015-12-16
    • 2018-11-23
    相关资源
    最近更新 更多