Ameya Savale
Ameya Savale

Reputation: 441

ifstream linker error

I am trying to read in a file. I attempt to use ifstream in read() but I get the following error.

undefined reference to std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()' /home/ameya/Documents/computer_science/cs130B/prog2/prog2.cpp:24: undefined reference tostd::basic_ifstream >::~basic_ifstream()' prog2.o:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0' collect2: error: ld returned 1 exit status make: * [prog2] Error 1

It says undefined reference to ifstream but I included that at the top so, why am I getting that error? Thanks in advance

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ifstream>

using namespace std;

class DepthMap{
public:
  int merge(int numbers[]);
  int mergehelper(int left[], int right[]);
  void read();
};

int DepthMap::merge(int numbers[]){
  return -43;
}

int DepthMap::mergehelper(int left[], int right[]){
  return -43;
}

void DepthMap::read(){
  ifstream inputFile;
}

int main(int argc, char* argv[])
{
  DepthMap depth;

  printf("Here");
  return 0;
}

Here is my Makefile

CXX = g++
CXXFLAGS = -Wall

all: prog2

prog2: prog2.o

clean:
    rm -f prog2

Upvotes: 1

Views: 3687

Answers (3)

Troubadour
Troubadour

Reputation: 13431

You are using gcc to compile and link rather than g++. By using the latter it will make sure you link against libstdc++.so without having to explicitly add it.

Seeing your Makefile confirms the above for linking.

Although you define CXX to be g++ that is only used for the implicit rule that compiles the source file. The implicit rule for linking falls back to CC which will probably be gcc. See the Catalogue of Implicit Rules for GNU make.

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25725

#include <fstream> as it should be.

Your g++ seems to be broken. Why do you not install clang?

Here are some suggested corrections for your makefile:

CXX = g++
CXXFLAGS = -Wall

prog2: prog2.o
      g++ $(CXXFLAGS) prog2.o -o prog2
prog2.o: prog2.cpp
      g++ $(CXXFLAGS) prog2.cpp -o prog2.o
clean:
    rm -f prog2

Upvotes: 1

digitalfrenchfry
digitalfrenchfry

Reputation: 174

I believe what you're looking for is

#include <fstream>

Upvotes: 1

Related Questions