'Error message "Undefined reference for std:string" [duplicate]
I have some code that reads the name of the file from arguments and outputs that file into the command line:
#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();
}
}
However, when I try to compile my code it outputs the following error:
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
What's it telling me? How can I fix it?
Solution 1:[1]
You haven't included std::string
into your code. It's located in the <string>
header. <string.h>
is a C header which doesn't contain std::string
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ted Klein Bergman |