【问题标题】:./libmylib.so: undefined reference to `submarinex::LIB::kCount'./libmylib.so: 未定义对 `submarinex::LIB::kCount' 的引用
【发布时间】:2018-02-08 04:09:43
【问题描述】:

我在一个目录中有 3 个文件。我将构建ll.cclibmylib.so,并将main.cc 构建到myexe

使用这些命令构建

g++ -Wall -g -fPIC -std=c++11 ll.cc -shared -o libmylib.so
g++ -Wall -g -std=c++11 main.cc -L. -lmylib -o myexe

但是,g++编译时报错myexe

./libmylib.so: undefined reference to `submarinex::LIB::kCount'
collect2: error: ld returned 1 exit status

文件:

ll.h

namespace submarinex {

class LIB {
 public:
  void Print();

 private:
  static const int kCount = 100;
};

}  // namespace submarinex

ll.cc

#include "ll.h"

#include <algorithm>
#include <iostream>

namespace submarinex {

void LIB::Print() {
  int min = std::min(101, kCount);
  std::cout << min << std::endl;

  // std::cout << kCount << std::endl;
}

}  // namespace submarinex

main.cc

#include "ll.h"

int main(int argc, char **argv) {
  submarinex::LIB lib;
  lib.Print();

  return 0;
}

Case 1: 如果在Print中使用这两行,main.cc的链接对象会报错

  int min = std::min(101, kCount);
  std::cout << min << std::endl;

Case 2: 如果在Print中使用这一行,会成功

  std::cout << kCount << std::endl;

如果改变

 static const int kCount = 100;

 const int kCount = 100;

无论使用Case1Case2 都可以。

我不知道如何解决这个问题。

【问题讨论】:

    标签: c++ c++11 build compilation gcc4.9


    【解决方案1】:

    我认为您的源代码中没有定义 kCount 变量。

    因此,您的变量必须在源代码 ll.cc 中的某处定义。

    在ll.h中

    namespace submarinex {
        class LIB {
            private:
                const static int kCount =100;
            public:
                void Print();
        };
    }  // namespace submarinex
    

    然后,您可以在 ll.cc 中定义一个 kCount 变量

    #include "ll.h"
    
    #include <algorithm>
    #include <iostream>
    
    namespace submarinex 
    {
        const int LIB::kCount; //<== here
    
        void LIB::Print() {
    
            int min = std::min(101, kCount);
            std::cout << min << std::endl;
    
            // std::cout << kCount << std::endl;
        }   
    
    }  // namespace submarinex
    

    这是我的 gcc 版本信息

    $ gcc -v
    Using built-in specs.
    COLLECT_GCC=C:\DEV\COMP\msys32\mingw32\bin\gcc.exe
    COLLECT_LTO_WRAPPER=C:/DEV/COMP/msys32/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.3.0/lto-wrapper.exe
    Target: i686-w64-mingw32
    Configured with: ../gcc-6.3.0/configure --prefix=/mingw32 --with-local-prefix=/mingw32/local --build=i686-w64-mingw32 --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-native-system-header-dir=/mingw32/i686-w64-mingw32/include --libexecdir=/mingw32/lib --enable-bootstrap --with-arch=i686 --with-tune=generic --enable-languages=c,lto,c++,objc,obj-c++,fortran,ada --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw32 --with-mpfr=/mingw32 --with-mpc=/mingw32 --with-isl=/mingw32 --with-pkgversion='Rev1, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld --disable-sjlj-exceptions --with-dwarf2
    Thread model: posix
    gcc version 6.3.0 (Rev1, Built by MSYS2 project)
    

    使用 MinGW32 shell 编译。

    g++ -Wall -g -fPIC -std=c++11 -I. ll.cc -shared -o libmylib.a
    g++ -Wall -g -std=c++11 -I. -L. -lmylib main.cc -o myexe
    

    希望对你有帮助。

    问候,

    【讨论】:

      【解决方案2】:

      std::min 使用两个参数const int&amp;(不是int)并且您传递了对submarinex::LIB::kCount 的引用而没有定义(您刚刚在.h 文件中声明)。

      在这种情况下,您可以在ll.cc中添加这一行

      namespace submarinex {
          static const int LIB::kCount = 100; //define it here
          void LIB::Print() {
              std::min(101, kCount);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 2018-03-07
        • 1970-01-01
        • 1970-01-01
        • 2020-02-11
        • 2021-09-29
        • 2011-03-27
        相关资源
        最近更新 更多