1

This is the input to g++ and the resulting error messages.

$ g++ main.cpp -o keyLogger
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): undefined reference to `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): undefined reference to `SaveFeatures::save(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::save(std::string)'
collect2: error: ld returned 1 exit status

I've checked and rechecked my syntax for the .h and .cpp for SaveFeatures class but havent been able to find an error. Any help would be welcome.

Main.cpp

#include <string>
#include "SaveFeatures.h"

using namespace std;

int main(){
    string fileName="saveTest.text";
    string saveContent="this is a test";
    SaveFeatures saveFeatures(fileName);
    saveFeatures.save(saveContent); 
}

SaveFeature.cpp

#include "SaveFeatures.h"
#include <string>
using namespace std;

SaveFeatures::SaveFeatures(string fileName){
    setFileName(fileName);
}

void SaveFeatures::setFileName(string fileName){
    if(fileName!=NULL){
        this.fileName=fileName;
    }
}
bool SaveFeatures::save(string content){
    if(fileName==NULL)return false;
    if (content==NULL)return false;
    FILE *file;
    file=fopen(fileName,"a");
    if(file!=NULL){
        fputs(content,file);
    }
        return true;            
}

string SaveFeatures::getFileName(){
    return fileName;
}

SaveFeatures.h

#ifndef SAVEFEATURES_H
#define SAVEFEATURES_H
#include <string>
using namespace std;

class SaveFeatures{
    public: 
        SaveFeatures(string fileName);      
        void setFileName(string fileName);      
        string getFileName();       
        bool save(string content);      
    private:
        string fileName;
        //need to make a method to determine if the fileName has  file extension    
};
#endif
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
hiEntropy
  • 15
  • 1
  • 3
  • 1
    See [this answer](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400). In your case, it is not just linking .o files, but compiling and linking in one step. You're still missing a file. – chris Mar 30 '15 at 04:39

1 Answers1

2

You will need to specify all the needed source files for your executable.

g++ main.cpp SaveFeature.cpp -o keyLogger
jxh
  • 69,070
  • 8
  • 110
  • 193