ディレクトリに3つのファイルがあります。 ll.cc
〜libmylib.so
をビルドし、main.cc
〜myexe
をビルドします。./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 1
:Print
にこれらの2行を使用 場合は、エラーを報告しますときmain.cc
int min = std::min(101, kCount);
std::cout << min << std::endl;
Case 2
のリンクオブジェクト: の場合Print
でこの行を使用すると、成功するでしょう
std::cout << kCount << std::endl;
それもかかわらずCase1
やCase2
を使用してのOKも
const int kCount = 100;
に変更
static const int kCount = 100;
場合。
この問題の解決方法はわかりません。