-6

当我尝试编译程序(测试我的库)时,我对每个调用的方法都有未定义的引用。我已经阅读了关于“ gcc undefined reference to ”的答案,但它没有帮助。PS 我使用: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;
}

生成文件目标:

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 文件被处理,这些符号就已经被拒绝了。您的命令行中有两次库,但由于库已被处理,第二次被忽略。

您应该始终将库(一次)放在编译器命令行的末尾:

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