-6

プログラムをコンパイルしようとすると(ライブラリのテスト)、呼び出されたメソッドごとに未定義の参照があります。「 gcc undefined reference to 」に関する回答を読みましたが、役に立ちません。PS I を使用: Debian 7.2.0 および C++11 標準。

#include <RFw/String.hpp>
#include <stdio.h>
using namespace RFw;

int main() {
Array<char> _arr_ (5);

_arr_[0] = 'b';
_arr_[1] = 'c';

printf("%c%c\n", _arr_[0], _arr_[2]);

printf(RFw::getVersion());

return 0;
}

Makefile ターゲット:

test:
    c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}

コンソール出力:

test.cpp:13:9: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
        printf(RFw::getVersion());
               ^~~~~~~~~~~~~~~~~
1 warning generated.
/tmp/test-lxdZF4.o: In function `main':
test.cpp:(.text+0x20): undefined reference to `RFw::Array<char>::Array(int)'
test.cpp:(.text+0x33): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x54): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x75): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x99): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0xff): undefined reference to `RFw::Array<char>::~Array()'
test.cpp:(.text+0x11a): undefined reference to `RFw::Array<char>::~Array()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::operator[](int) const'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::Array(int)'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::getLength() const'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Exception::onThrow()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::resize(int)'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::addElementOnEnd(char)'
./bin/libregemfw0.1-core.so: undefined reference to `vtable for RFw::Object'
./bin/libregemfw0.1-core.so: undefined reference to `typeinfo for RFw::Object'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Object::~Object()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::~Array()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Ошибка 1
4

1 に答える 1

2

問題はそれです

c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}

最初にライブラリを処理し、次に .cpp ファイルを処理します。ライブラリを処理するとき、参照されたシンボルは解決 (「リンク」) され、ライブラリ内の不要な未解決のシンボルはすべて破棄されます。つまり、.cpp ファイルが処理されるとすぐに、これらのシンボルは既に拒否されています。コマンド ラインにライブラリが 2 回ありますが、ライブラリが既に処理されているため、2 番目のライブラリは無視されています。

ライブラリは常にコンパイラ コマンド ラインの最後に (1 回) 配置する必要があります。

c++ test.cpp -I./include-core/ -o bin/test test.cpp -L./bin -l${core_NAME_ROOT}
于 2013-11-04T10:49:14.403 に答える