I am working with the above files and have precompiled MyClass header to an object file (MyClass.o). Now if I add this precompiled header to my project and try compiling the project I get errors
i.e. [Linker error] undefined reference to MyClass::<all member functions>.

I'm using Dev-c++ ver. 4.9.9.2 IDE and compiler.

Any pointers to my problems will be greatly appreciated, as I'm not sure if I understand precompiled headers.
If I include MyClass.cpp source it compiles ok. Now how can I distribute my header file without including its source code?

#ifndef _MYCLASS_H
#define _MYCLASS_H

#include <string>
using std::string;

class MyClass{
      public:
             MyClass(const string& text);
             const string& text()const;
             void setText(const string& text);
             int getLengthOfText()const;
      private:
              string m_text;
};
#endif



#include "MyClass.h"

MyClass::MyClass(const string& text)
                   : m_text(text){ }

const string& MyClass::text()const{
      return m_text;
}
void MyClass::setText(const string& text){
     m_text = text;
}
int MyClass::getLengthOfText()const {
    return m_text.length();
}




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

int main(){
    string myName = "Bill Gates";
    MyClass instance(myName);
    cout<<"instance text is:  "<<instance.text()<<endl;
    cout<<"Length of instance text is:  "
              <<instance.getLengthOfText()<<endl;
    string anotherStr = "Yet Another string";
    instance.setText(anotherStr);
    cout<<"Now instance text is:  "
               <<instance.text()<<endl;
    cout<<"Now length is:  "<<instance.getLengthOfText()<<endl;
    cout<<"press NEWLINE to continue";
    cin.get();
    return 0;
}

Recommended Answers

All 2 Replies

The term "precompiled headers" means something completely different than what you are trying to do. You are tring to create a library of functions/classes that can be used in other projects. All you really need in those other projects is the *.h and *.o (or *.obj) files.

Thanks Dragon for clearing my confusion. After this I played around with the IDE and managed to create a static library and this solved my compilation nightmare. Basical its about informing the linker where to find object files for the project.
Once again thanks for your insights.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.