Problem: Using self-defined libraries in Code:Blocks 10.05

Hello, everybody. Because of many reasons, I decided to use Code:Blocks. Its version is 10.05 (includes mingw) and my OS is Windows XP SP3
And I got a problem when trying to compile a program using self-defined libraries.
I have 3 files: main.cpp, mylib.cpp and mylib.h. (Of course, they are stored in same directories (or folder)).
Each of them has content as the following:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
// main.cpp
#include <iostream>
#include <string>
#include "mylib.h"

using namespace std;

int main ()
{
    string myName = "My Name";
    printName (myName);
    return 0;
}


mylib.cpp
1
2
3
4
5
6
7
8
9
10
11
// mylib.cpp
#include <iostream>
#include <string>
#include "mylib.h"

using namespace std;

void printName(string yourName)
{
    cout << yourName << endl;
}


mylib.h
1
2
3
4
5
6
7
8
9
10
11
// mylib.h
#ifndef MYLIB_H_INCLUDED
#define MYLIB_H_INCLUDED

#include <string>

using namespace std;

void printName(string yourName);

#endif 

When I built my project, I received an error massage:

In function 'main':
undefined reference to 'printName' (std::string)'

Can you give me some explainations and the solution for my problem.
Thanks in advance.
You need to link the mylib translation unit.
Code::Blocks will automatically do that when you add mylib.cpp to your project (Project/Add files).
When creating new files, it's best to do it using Code::Blocks via File/New - it will then ask you whether to add the file to your current project.

And you should never use "using namespace" in header files. It injects the entire std namespace into any translation unit that directly or indirectly includes this header file, whether the user wants it or not.
Thank you so much, Athar. It solved my problem.
Topic archived. No new replies allowed.