2017-08-30 4 views
3

ディレクトリに3つのファイルがあります。 ll.cclibmylib.soをビルドし、main.ccmyexeをビルドします。./libmylib.so: `submarinex :: LIB :: kCount 'への未定義の参照

使用して、これらがg ++エラーを報告し、

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 

を構築するためのコマンドしかし、ときビルド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 1Printにこれらの2行を使用 場合は、エラーを報告しますときmain.cc

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

Case 2のリンクオブジェクト: の場合Printでこの行を使用すると、成功するでしょう

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

それもかかわらずCase1Case2を使用してのOKも

const int kCount = 100; 

に変更

static const int kCount = 100; 

場合。

この問題の解決方法はわかりません。

答えて

0

にこの行を追加することができます。

したがって、変数はソースコードll.ccのどこかで定義する必要があります。

ll.hで

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

その後、あなたはLLでkCount変数を定義することができます。CC

#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のバージョン情報MINGW32シェルでコンパイル

$ 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) 

です。

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 

私はこれがあなたを助けてくれることを願っています。

よろしくお願いします。

0

std::min使用2つのparams const int&(ないint)、あなたは定義なしsubmarinex::LIB::kCountへの参照を渡す(あなただけ.hファイルで宣言)。この場合

、あなたは私はあなたのソースコード内のkCount変数のない定義は存在しないと思いますll.cc

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