错误消息 "Undefined reference for std:string"

Error message "Undefined reference for std:string"

我有一些代码可以从参数中读取文件名并将该文件输出到命令行:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("Use: .exe [filename you want printed out]\n");
    }

    char* name = argv[1];
    string line;

    printf("filename:%s\n", name);

    ifstream myfile(name);

    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            cout << line << '\n';
        }
        myfile.close();
    }
}

然而,当我尝试编译我的代码时,它输出了以下错误:

hello.cpp:(.text+0x5a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
hello.cpp:(.text+0x93): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
hello.cpp:(.text+0xa2): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::is_open()'
hello.cpp:(.text+0xbf): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
hello.cpp:(.text+0xd4): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator bool() const'
hello.cpp:(.text+0xe9): undefined reference to `std::cout'
hello.cpp:(.text+0xee): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
hello.cpp:(.text+0xfb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
hello.cpp:(.text+0x10c): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::close()'
hello.cpp:(.text+0x11b): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
hello.cpp:(.text+0x12a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
hello.cpp:(.text+0x152): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
hello.cpp:(.text+0x166): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/cc0aneAi.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x1a9): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x1be): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc0aneAi.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

它告诉我什么?我该如何解决?

您还没有将 std::string 包含到您的代码中。它位于 <string> header。 <string.h> 是不包含 std::string.

的 C header