0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    `#include ` is your friend in this case. – bloody Dec 06 '20 at 01:30
  • 2
    I'm voting to reopen the question. I don't agree with the duplicate. The duplicate explains what an `undefined reference` is, but to OP thought they already had imported everything. This is a small error that's hard to track down unless you know there's a difference between `` and ``. Which isn't completely obvious as most files that omit the `.h` extension are just the same files as C but surrounded by `namespace std`. – Ted Klein Bergman Dec 06 '20 at 01:39
  • @TedKleinBergman. Sorry, I thought by linking the question I would simply create a link to what helped me understand it as opposed to simply saying that the link answers my question. – Denis Shevchenko Dec 06 '20 at 01:58
  • @DenisShevchenko Sorry, my comment wasn't directed at you. The close votes came from other users so the comment is just a general comment to whoever reads this and have privileges to reopen it again. You haven't done anything wrong. – Ted Klein Bergman Dec 06 '20 at 01:59
  • @DenisShevchenko It was flagged as a duplicate, i.e. that the linked question contains the answer to your problem. I disagree with that. – Ted Klein Bergman Dec 06 '20 at 02:03
  • @TedKleinBergman, I disagree as well. How can I help it being unmarked as duplicate? – Denis Shevchenko Dec 06 '20 at 02:13
  • @DenisShevchenko Just wait. When people with more than 3000 reputation sees this they'll be able to cast a reopen vote. It currently has 2 so it only need one more. And it actually doesn't matter in this case; it's just a matter of principle. – Ted Klein Bergman Dec 06 '20 at 02:20

1 Answers1

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.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50