为什么我在尝试构建 C++ 项目时收到此未定义的引用错误?

为什么我在尝试构建 C++ 项目时收到此未定义的引用错误?

为了修复另一个调试错误,我在构建命令中添加了 -std=c++11。现在我收到大量“未定义引用”错误,例如:

In function encrypt(std::string&, int)': 
/home/bob/workspace/New/Debug/../test.cpp:10: undefined reference to std::string::begin()' 
/home/bob/workspace/New/Debug/../test.cpp:10: undefined reference to std::string::end()' 
./test.o: In function main': 
/home/bob/workspace/New/Debug/../test.cpp:24: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'" '

我需要修复什么?这是我的代码

using namespace std;
#include <iostream>
#include <string>

void encrypt(std::string &iostr, int key) {
key %= 26;
int ch;

for (auto &it : iostr) {
ch = tolower(it);
if (!islower(ch)) {
continue;
}
ch += key;
if (ch > 'z') {
ch -= 26;
}
it = ch;
}
}

int main() {
string source;
int key = 1;
cout
<< "Paste cyphertext and press enter to shift each letter right 1";

getline(cin, source);
encrypt(source, key);

cout << source << "";



encrypt(source, key);
cout << source << endl;
cout << "Press ENTER to exit";
cin.ignore(cin.rdbuf()->in_avail() + 1);

return 0;

}

答案1

看起来需要安装 stdc++ dev 文件

sudo apt-get install libstdc++-4.8-dev

然后编译代码

g++ -o execname ./sourcefile.cpp -std=c++11

相关内容