c++ - <函数名称> 的 undefined reference

标签 c++ linux c++11 undefined-reference

当我尝试编译程序(测试我的库)时,每个被调用的方法都有 undefined reference 。我已阅读“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;
}

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

最佳答案

问题在于

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}

关于c++ - <函数名称> 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765739/

相关文章:

c++ - 重载可变模板成员函数和可变模板函数

c - linux clone() 成功,但是 child 崩溃了

linux - 切换用户并输出变量内容

linux - : not a valid identifier bash export says

google-chrome - 从 native 主机向浏览器扩展发送消息时获取 "Error when communicating with the native messaging host."(Windows)

c++ - 纯虚拟和虚拟有什么区别

c++ - std::map 访问速度太慢?

c++ - std::array<int, 10> 作为类成员是零初始化的吗?

c++ - 重写 2 个重载继承方法之一 C++

c++ - 使用 <random> 将随机数生成限制在一个范围内