【问题标题】:Undefined reference to function in case of a library在库的情况下对函数的未定义引用
【发布时间】:2015-03-06 23:47:08
【问题描述】:

我正在将代码从 Visual Studio 移植到 Mingw GCC。该组件在 Visual Studio 中运行良好,但 Mingw GCC 抱怨未定义对函数的引用。我已经隔离了这种情况并在这里写下代码

文件:Rng.h

#pragma once

#ifdef RNG_EXPORTS
#define RNG_API  __declspec(dllexport)
#else
#define RNG_API __declspec(dllimport)
#endif

RNG_API unsigned long GetRandom(unsigned long range);

文件:Rng.cpp

#include "Rng.h"
#include "RngKiss.h"

static  TRngFile                    gRngFile;

unsigned long GetRandom(unsigned long range)
{
    gRngFile.generate_rnd();  //Linker Error : Undefined Reference to function.

    ....
}

文件:RngKiss.h

#ifndef __RNGKISS_H__
#define __RNGKISS_H__

#ifndef ULONG
typedef unsigned long ULONG;
#endif  //ULONG


typedef struct
{
    ULONG   w, x, y, z;
} TRngRecord;


typedef struct
{
    TRngRecord  current, seed;
    ULONG   generate_rnd(void);
} TRngFile;

#endif  

文件:RngKiss.cpp

#include "RngKiss.h"


ULONG   TRngFile::generate_rnd(void)
{
    ULONG d;
    return  d;
}

这是我的输出。

g++.exe -L..\..\..\mingw64\lib\boost -o bin\Debug\TestCodeBlocks.exe obj\Debug\main.o obj\Debug\Rng.o obj\Debug\RngKiss.o   
obj\Debug\Rng.o: In function `GetRandom(unsigned long)':
C:/Users/admin/TestCodeBlocks/Rng.cpp:8: undefined reference to `TRngFile::generate_rnd()'
collect2.exe: error: ld returned 1 exit status

关于我为什么会收到此链接器错误以及如何解决它的任何建议?

【问题讨论】:

    标签: c++ c visual-studio gcc


    【解决方案1】:

    您在mingw64 编译器中遇到了一个错误(或至少是一个怪癖),这是一个相当晦涩的问题 编译未命名的结构。我不知道你有什么版本,但我的mingw32 4.8.1 具有相同的行为:

    这些typedefs 在rngKiss.h:-

    typedef struct
    {
        ULONG   w, x, y, z;
    } TRngRecord;
    
    
    typedef struct
    {
        TRngRecord  current, seed;
        ULONG   generate_rnd(void);
    } TRngFile;
    

    你可能会认为他们分别定义了一个struct 类型叫做TRngRecord 和另一个叫做 TRngFile,但严格来说,他们将 TRngRecordTRngFile 定义为 struct 类型的别名 它们本身没有名字。

    区别应该只是概念上的。所以它是为了 微软的编译器和 Windows 上的TDM GCC 4.9.2,所以它是 Linux 上的 GCC 4.9.2 和 clang 3.5.1。

    然而,我们的 mingw 编译器似乎将 未命名 struct 的成员类型定义为 TRngFile 必须有静态链接。我的生成:

    Dump of file rngKiss.o
    
    File Type: COFF OBJECT
    
    COFF SYMBOL TABLE
    000 00000000 DEBUG  notype       Filename     | .file
        rngKiss.cpp
    002 00000000 SECT1  notype ()    Static       | __ZN8TRngFile12generate_rndEv
    ...   
    

    因此链接错误。而 TDM GCC 4.9.2 生成:

    Dump of file rngKiss.o
    
    File Type: COFF OBJECT
    
    COFF SYMBOL TABLE
    000 00000000 DEBUG  notype       Filename     | .file
        rngKiss.cpp
    002 00000000 SECT1  notype ()    External     | _ZN8TRngFile12generate_rndEv
    ...
    

    有 3 种解决方案:

    改变你的编译器,比如 TDM GCC。

    为您的 typedefed structs 命名:

    typedef struct TRngRecord
    {
        ULONG   w, x, y, z;
    } TRngRecord;
    
    
    typedef struct TRngFile
    {
        TRngRecord  current, seed;
        ULONG   generate_rnd(void);
    } TRngFile;
    

    或者最好的:

    删除typedefs,这是在 C++ 中命名类的一种奇怪且不必要的方式:

    struct TRngRecord
    {
        ULONG   w, x, y, z;
    };
    
    struct  TRngFile
    {
        TRngRecord  current, seed;
        ULONG   generate_rnd(void);
    }; 
    
    ....
    
    #include "rngKiss.h"
    ...
    static  TRngFile                    gRngFile;
    

    【讨论】:

      【解决方案2】:
      a few ideas come to mind.
      
      1) the '::' scope operator is most often used to access a method of a class.  
         (another common use is to reference a function in a namespace)
         However, I do not see any class definition header file.
         (normally, the class header file is the same name as the class)
      
      2) there has to be a prototype in a class header file 
         (usually this is part of the class declaration/interface) 
         for the generate_rnd() method.
      

      那么,类头在哪里?

      那么,方法原型在哪里呢?

      【讨论】:

        【解决方案3】:

        问题表明该函数在库中。

        这表明链接器语句缺少“-Lpath to the library directory”和/或“-ltruncated libary name”

        【讨论】:

        • cpp和头文件在同一个库中
        猜你喜欢
        • 2015-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-05
        相关资源
        最近更新 更多