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. 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{
             MyClass(const string& text);
             const string& text()const;
             void setText(const string& text);
             int getLengthOfText()const;
              string m_text;

#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:  "
    string anotherStr = "Yet Another string";
    cout<<"Now instance text is:  "
    cout<<"Now length is:  "<<instance.getLengthOfText()<<endl;
    cout<<"press NEWLINE to continue";
    return 0;
7 Years
Discussion Span
Last Post by Aprentchacker

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.